2 buttons one LED.

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.

void setup() {
  
 pinMode(7, OUTPUT);

 pinMode(2, INPUT);
 pinMode(0, INPUT);

 digitalWrite(7, LOW);

}

void loop() {

  
  if(digitalRead(2) == HIGH)
  else if (digitalRead(0) == HIGH)
  
  {digitalWrite(7, HIGH);}
  
  if (digitalRead(2) == LOW)
  else if (digitalRead(0) == LOW)
  
  {digitalWrite(7, LOW);}

  
}

It's "else if", not "if else". :astonished:

Paul__B:
It's "else if", not "if else". :astonished:

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'

And I just realized that I put this thread in the wrong spot. Wow am I batting 0

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.

cattledog:
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.

Thank you so much! That is exactly what I was looking for.

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.

Try it out!