0

Hi guys I am new to this language and i just tested the code to control a servo motor Can anybody help me please ? here is the code

void setup() {

  servo_test.attach(3);      
  pinMode(9, INPUT);

}

void loop() {

 if ( digitalRead (9 == HIGH)) {

   for(angle = 0; angle < 180; angle += 5)    to 180 degrees 
  {                                  
    servo_test.write(angle);                
    delay(5);                       
  } 

  delay(4000);
  for(angle = 180; angle>=1; angle-=5)      to 0 degrees 
  {                                
    servo_test.write(angle);               specified angle
    delay(5);                       
  } 

    delay(4000);
}

}

It is just like the arduino completely ignore the if statement and start the loop wheter pin 9 is in high or low state even if i wire it directly to gnd

  • 2
    Try digitalRead(9)==HIGH – SBF Mar 2 at 17:28
0

In your code this line:

 if ( digitalRead (9 == HIGH)) {

...is likely passing the function "digitalRead()" the value zero as "9 == HIGH" is likely false.

Likely what you want to do is check if the value returned by the function "digitalRead()" is high. Try:

 if ( digitalRead (9) == HIGH) {

...instead.

  • Yeah, that line is wrong. That code evaluates as if (digitalRead(0)), which returns the value of pin 0. Not sure how LOW is defined in the Arduino IDE. If it's 0 then you'll get false if pin 0 is ==LOW and true if the pin is ==HIGH. – Duncan C Mar 2 at 17:50
  • so Arduino SE becomes a typo correction site. this answer is not useful – Juraj Mar 2 at 18:20
  • @Juraj I don't think it's a typo. It's a newbie's syntax error that compiles. I'm sure the answer was quite useful to the OP. – Duncan C Mar 2 at 18:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.