HELP Problem: else if

This program that I just wrote doesn’t work. Does anyone know why it doesn’t work? Arduino says that there is something wrong with the ‘if/else if’ construction.

void setup() {
pinMode (A0, OUTPUT);
pinMode (2, INPUT);
pinMode (1, INPUT);
digitalWrite (1,HIGH);

Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(10000);

if(sensorValue<9);
{digitalWrite (2,HIGH);
delay (50);
}

else
{digitalWrite (2, LOW);
}
}

Luke_0162:
This program that I just wrote doesn't work. Does anyone know why it doesn't work? Arduino says that there is something wrong with the 'if/else if' construction.

When you say "Arduino says" I suspect you mean that you get an error message when you try to compile the program. If so, please post the error message.

And "doesn't work" gives us no information from which to help you. You need to tell us what the program is expected to do and what it actually does - in as much detail as possible.

And, to make it easy for people to help you please use the code button </>
codeButton.png

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

...R

You have a semicolon after your if() statement. That terminates the statement. The brackets that follow do nothing really useful other than group the following statements. Then the else statement is encountered, but is not associated with the if()

Thank u guys! I got this (see code) now and it got verified. So I hope it will work! (I can’t test it now, because I am working on this electrical circuit at school)

void setup() {
pinMode (A0, OUTPUT);
pinMode (2, INPUT);
pinMode (1, INPUT);
digitalWrite (1,HIGH);
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(10000);

if(sensorValue<9)
{digitalWrite (2,HIGH);
delay (50);
}

else if (sensorValue>9)
{digitalWrite (2, LOW);
}
}

What should your program do if sensorValue IS 9? Right now, it does nothing.

if(sensorValue<9)
  {
    digitalWrite (2,HIGH);
    delay (50); 
  }

else 
{
   digitalWrite (2, LOW); 
}

If it’s not < 9 then it as to be >9 you don’t need to test again except if you want to test ==

If it’s not < 9 then it as to be >9

No. This is NOT necessarily true. 9 is not less than 9. 9 is NOT greater than 9.

PaulS:
No. This is NOT necessarily true. 9 is not less than 9. 9 is NOT greater than 9.

I mean < 9, >= 9 that what i mean after gg

You guys are totally right. I changed ‘>9’ to ‘>=9’. :slight_smile:

This is what I got now:

void setup() {
pinMode (A0, OUTPUT);
pinMode (2, INPUT);
pinMode (1, INPUT);
digitalWrite (1,HIGH);
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(10000);

if(sensorValue<9)
{digitalWrite (2,HIGH);
delay (50);
}

else if (sensorValue>=9)
{digitalWrite (2, LOW);
}
}

pinMode (A0, OUTPUT);
...
...
int sensorValue = analogRead(A0);

Novel.
Also not sure why you’re fiddling with the pull-ups resistors on pin 2.

if(sensorValue<9)
  {digitalWrite (2,HIGH);
  delay (50); 
}

else if (sensorValue>=9)

I think we can safely conclude that if a value is not less than 9, it is greater than or equal to 9, without the need to test it.

Also, always add comments in your sketches.

Learn to format your code, press in the IDE.

Next, get rid of delay(), look into using the Blink Without Delay technique.

Luke_0162:
You guys are totally right. I changed '>9' to '>=9'. :slight_smile:

This is what I got now:

Why won't you post your code properly like others have done?

Make it as easy as possible for people to help you.

...R