Hi there. i'm using arduino uno and i found that the code below does not work. Seems like "if" do not work inside "if" statements. it is working when i push the first button. but when i push the second button it seem does not read the command. how do i solve this problem dear community.
This code (assuming there is digitalRead somewhere) would detect the first button (ButtonStateMAN) only if it is pressed (and held) before pressing the second (ButtonStateAUTO).
Once the ButtonStateAUTO is detected to be pressed, no more checks are made to see if the ButtonStateMAN is held. The only thing being checked is the value of the previous check which may or may not be still relevant.
Shpaget:
This code (assuming there is digitalRead somewhere) would detect the first button (ButtonStateMAN) only if it is pressed (and held) before pressing the second (ButtonStateAUTO).
Once the ButtonStateAUTO is detected to be pressed, no more checks are made to see if the ButtonStateMAN is held. The only thing being checked is the value of the previous check which may or may not be still relevant.
ya.. probably. because if I push 2nd button followed by 1st button, it did read the command, but not as i want. it came out "auto","manauto" repeatedly. how i solve this problem.
Yeah, entire code and schematic would help, but in the meantime...
This is quick and dirty way to do it. I haven't tested it with buttons but it should work.
if (ButtonStateAUTO == HIGH){
for (int i = 0; i < 1000; i++){
delay (2); //This delay gives you a window of about 2 seconds to push the second button.
ButtonStateMAN = digitalRead (button2);
if (ButtonStateMAN == HIGH){
Serial.println("manauto");
delay (1000); //This delay is here because you had it in your posted code
break; //Once the ButtonStateMAN is detected to be pressed the for loop is exited and ButtonStateMAN is no longer checked
}
}
}
Shpaget:
Yeah, entire code and schematic would help, but in the meantime...
This is quick and dirty way to do it. I haven't tested it with buttons but it should work.
if (ButtonStateAUTO == HIGH){
for (int i = 0; i < 1000; i++){
delay (2); //This delay gives you a window of about 2 seconds to push the second button.
ButtonStateMAN = digitalRead (button2);
[code]if (ButtonStateMAN == HIGH){
Serial.println("manauto");
delay (1000); //This delay is here because you had it in your posted code
break;
//Once the ButtonStateMAN is detected to be pressed the for loop is exited and ButtonStateMAN is no longer checked
}
}
}[/code]
THANKS A LOT.... the code is working. i just need to add while(1) here
if (ButtonStateMAN == HIGH){
while(1)
Serial.println("manauto");
delay (1000); //This delay is here because you had it in your posted code
break;
...to make sure it does not read the upper command line (it loops forever inside the if statements)