If Statement Not Working - Programming Questions - Arduino Forum
Maybe your like
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 highwhy 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. dannable July 14, 2015, 2:03pm 2 pinMode (12, OUTPUT};should be
pinMode (12, OUTPUT); electroscientistX July 14, 2015, 2:14pm 3dannable:
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?
dannable July 14, 2015, 2:17pm 4Print out the values of va and vb using serial monitor and see if they are what you expect.
tammytam 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.
econjack July 14, 2015, 3:15pm 6Also, 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.
Related topics
| 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 |
Tag » Arduino If Statement Not Working
-
If And Else Statements, If Statement Not Working - Arduino Forum
-
[SOLVED] If Statement Not Working With String - Arduino Forum
-
2nd If Statement Not Working - Arduino Forum
-
If Statement Does Not Work! What Am I Doing Wrong? - Arduino Forum
-
If Statement Not Working - Programming Questions - Arduino Forum
-
If Statement Not Working - Programming Questions - Arduino Forum
-
If Statement Not Working..? - Programming Questions - Arduino Forum
-
IF Statement Doesn't Work Properly - Arduino Forum
-
Not Getting Proper Output In Arduino (if Statement) - Stack Overflow
-
If Statements Not Working As Expected On ESP32 - Stack Overflow
-
Arduino IDE: Conditional(if-else-if) Statements - STEMpedia
-
Serial Communication, If Statement Not Working
-
2.7 Understanding If / Else And While Statement In Arduino