DC Motor PWM Speed Control with Ultrasonic Distance – electroSome https://electrosome.com/topic/dc-motor-speed-ultrasonic-distance-hc-sr04-pic/feed/ Sat, 18 Mar 2023 05:00:06 +0000 https://bbpress.org/?v=2.6.9 en-US https://electrosome.com/topic/dc-motor-speed-ultrasonic-distance-hc-sr04-pic/#post-12265 <![CDATA[DC Motor PWM Speed Control with Ultrasonic Distance]]> https://electrosome.com/topic/dc-motor-speed-ultrasonic-distance-hc-sr04-pic/#post-12265 Tue, 15 Mar 2016 03:28:16 +0000 Kimie Hakimie hi and good day to you, i am working on pic16f877a , have interfaced push-button to trigger the ultrasonic sensor hc-sr04 and the range will control the motor speed by using the pwm function. i am very new to the programming mikroC and i really hope you can guide me. As now i just learn how to interface push button with my led. I tried to combine the code how to interfacing ultrasonic and lcd and it didn’t work, i am very sorry, here my code:

void main()
{
  TRISA.F0 = 1; //Configure 1st bit of PORTD as input
  TRISA.F1 = 1;
  TRISA.F2 = 1;
  TRISB.F5 = 0; //Configure 1st bit of PORTB as output
  TRISB.F6 = 0;
  TRISB.F7 = 0;
  TRISA.F3 = 0;
  PORTB.F5 = 1; //LED OFF
  PORTB.F6 = 1; //LED OFF
  PORTB.F7 = 1; //LED OFF
  PORTA.F3 = 1; //BUZ OFF
  ADCON1 = 7;

  do
  {
    // LED 1
    if(PORTA.F0 == 0) //If the switch is pressed
       PORTA.F3 = 0; //BUZ ON
    Delay_ms(100); //1 Second Delay
     PORTA.F3 = 1; //BUZ OFF

    {
      Delay_ms(150); //Switch Debounce
      if(PORTA.F0 == 0)//If the switch is still pressed
      {
        PORTB.F5 = !PORTB.F5; // LED out port
      }
    }

    // LED 2
  
    if(PORTA.F1 == 0) //If the switch is pressed
    if(PORTA.F0 == 0) //If the switch is pressed
       PORTA.F3 = 0; //BUZ ON
     Delay_ms(100); //1 Second Delay
     PORTA.F3 = 1; //BUZ OFF
    {
      Delay_ms(150); //Switch Debounce
      if(PORTA.F1 == 0)//If the switch is still pressed
      {
        PORTB.F6 = !PORTB.F6; // LED out port
      }
    }


    // LED 3
    if(PORTA.F2 == 0) //If the switch is pressed
    if(PORTA.F0 == 0) //If the switch is pressed
      PORTA.F3 = 0; //BUZ ON
    Delay_ms(100); //1 Second Delay
    PORTA.F3 = 1; //BUZ OFF
    {
      Delay_ms(150); //Switch Debounce
      if(PORTA.F2 == 0)//If the switch is still pressed
      {
        PORTB.F7 = !PORTB.F7; // LED out port
      }
    }
  }while(1);
}
]]>
https://electrosome.com/topic/dc-motor-speed-ultrasonic-distance-hc-sr04-pic/#post-12269 <![CDATA[Reply To: DC Motor PWM Speed Control with Ultrasonic Distance]]> https://electrosome.com/topic/dc-motor-speed-ultrasonic-distance-hc-sr04-pic/#post-12269 Wed, 16 Mar 2016 00:49:15 +0000 Ligo George Sorry, I don’t understand what are you doing and I haven’t seen any ultrasonic distance measuring section in your program.

One of the main mistake I found in your code is, you are not using open and closing braces of if statement correctly. Please correct it and recheck your code.

]]>