So I feel bad asking this. I have searched google for an hour and can't find an answer to this. At least not one I can understand so I'm asking here. Feel free to make fun of me for it. ;D
I want to press two push buttons and that makes an LED come on. Both buttons must be pressed and held for the LED to come on. And when I release either button the LED goes off. So basically two push buttons in series, but I want to do it in the program instead of wiring it. This is so I can better understand how to program series vs parallel. So first I wrote a program that simply made an LED come on with one button. It works no problem. But I don't know how to add the second button. This is what I tried and of course it doesn't work.
I'm really having a hard time understanding if, else and if else. Even though people on here have been nice enough to try and explain it to me but I still don't get it.
Yeah I knew that. I'm a little dyslexic. Ok so I fixed that but still a no go.
The error I get is as follows.
Arduino: 1.8.11 (Linux), Board: "Arduino Uno"
/tmp/arduino_modified_sketch_26763/BareMinimum.ino: In function 'void loop()':
BareMinimum:16:3: error: expected primary-expression before 'else'
else if (digitalRead(0) == HIGH)
^~~~
BareMinimum:21:3: error: expected primary-expression before 'else'
else if (digitalRead(0) == LOW)
^~~~
exit status 1
expected primary-expression before 'else'
Phobos84:
I want to press two push buttons and that makes an LED come on. Both buttons must be pressed and held for the LED to come on. And when I release either button the LED goes off. So basically two push buttons in series, but I want to do it in the program instead of wiring it.
Take a look at the use of the logical "and" to make sure two conditions are both met at the same time. && - Arduino Reference
You can use either the symbols && or the actual word "and".
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // if BOTH the switches read HIGH
// statements
}
if (digitalRead(2) == HIGH and digitalRead(3) == HIGH) { // if BOTH the switches read HIGH
// statements
}
Pin 0 is not a good choice for general i/o because of its use for Serial.
Of note: I see you're using pinMode INPUT, and buttons are active HIGH. That implies you're wiring them between Vcc and pin, with a pull-down resistor.
The more common method is to wire them between pin and GND, no external resistor, and use pinMode INPUT_PULLUP. Now the button inputs are LOW when pressed, HIGH when unpressed, so you have to change the program a bit.