If Statement Not Working - Programming Questions - Arduino Forum

if statement not working Projects Programming July 14, 2015, 2:00pm 1

why i cant specify two pins for output? also, if statements are not working properly here.

void setup() {   pinMode (13, OUTPUT); pinMode (12, OUTPUT};   } void loop() {   int a;   int b;     a = analogRead(A1);   b = analogRead(A2);   float va = a * (5.0/1023.0);   float vb = b * (5.0/1023.0);   if (va > 4)     digitalWrite(13, HIGH);   else if (vb > 4)     digitalWrite(12, HIGH);   else     digitalWrite(13, LOW);     digitalWrite(12, LOW);   } expected output: when A1 get 5v, led on 13 go high, 12 go low when A2 get 5v, led on 13 go low, 12 go high observed output: when A1 get 5v, led on 13 go high, 12 go high when A2 get 5v, led on 13 go high, 12 go high

why it is happening? also, im getting an error

Arduino: 1.6.3 (Windows 7), Board: "Arduino Uno" Build options changed, rebuilding all sketch_jul14a.ino: In function 'void setup()': sketch_jul14a.ino:5:20: error: expected ')' before '}' token sketch_jul14a.ino:5:20: error: expected ';' before '}' token sketch_jul14a.ino: At global scope: sketch_jul14a.ino:6:4: error: expected declaration before '}' token Error compiling.   This report would have more information with   "Show verbose output during compilation"   enabled in File > Preferences. July 14, 2015, 2:03pm 2 pinMode (12, OUTPUT};

should be

pinMode (12, OUTPUT); July 14, 2015, 2:14pm 3

dannable:

pinMode (12, OUTPUT};

should be

pinMode (12, OUTPUT);

thanks i did it now, no errors! but still expected and observed out[ut is different why?

July 14, 2015, 2:17pm 4

Print out the values of va and vb using serial monitor and see if they are what you expect.

July 14, 2015, 2:28pm 5   if (va > 4)     digitalWrite(13, HIGH);   else if (vb > 4)     digitalWrite(12, HIGH);   else     digitalWrite(13, LOW);     digitalWrite(12, LOW);

Your indentation assumes you want those 2 last lines to be executed within the else?

If that is your intent, you need to bracket that block, otherwise only a single line will be called as part of that else:

if (va > 4) digitalWrite(13, HIGH); else if (vb > 4) digitalWrite(12, HIGH); else { digitalWrite(13, LOW); digitalWrite(12, LOW); }

Also in the if() and the else if(), only 1 pin is set, the other pin will be whatever you left it as.

July 14, 2015, 3:15pm 6

Also, since va and vb are float data types, it's better style to write:

if (va > 4.0)                            // Note trailing zero digitalWrite(13, HIGH); else if (vb > 4.0) digitalWrite(12, HIGH); else { digitalWrite(13, LOW); digitalWrite(12, LOW); }

as it documents that the if expression is on a floating point number.

Topic Replies Views Activity
"if" command not working as it should Programming 5 117 July 18, 2025
If else statment and what to do if the syntax is giving me a stantax error PLC IDE 7 164 December 13, 2024
What am i doing wrong Programming 9 101 December 8, 2024
If Statement problem Programming 33 340 October 27, 2024
If inside a if? Programming 11 321 January 7, 2023
Unfortunately, your browser is unsupported. Please switch to a supported browser to view rich content, log in and reply.

Tag » Arduino If Statement Not Working