I have a code when you press one button, the first ledPin turns of. When you press the second button ledPin2 turns off. When you press the third button, nothing turns off but when you press button one and three, ledPin, ledPin2, and ledPin3 are supposed to turn off. Everything works except the and when trying to combine buttons to turn off leds. This is what I have done:
Also, unless you have external pullup resistors connected to your buttons, you want to declare them as INPUT_PULLUP, not INPUT.
//Initialize Variables
const int ledPin = 5; // choose the pin for the LED
const int ledPin2 = 6;
const int ledPin3 = 7;
const int inPin = 2; // pin for button 1
const int inPin2 = 3; // pin for button 2
const int inPin3 = 4; // pin for button 3
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT_PULLUP); // declare pushbutton as input
pinMode(ledPin2, OUTPUT); // declare LED as output
pinMode(inPin2, INPUT_PULLUP); // declare pushbutton as input
pinMode(ledPin3, OUTPUT); // declare LED as output
pinMode(inPin3, INPUT_PULLUP); // declare pushbutton as input
}
void loop()
{
//Read button output
int val1 = digitalRead(inPin); // read input value
int val2 = digitalRead(inPin2); // read input value
int val3 = digitalRead(inPin3);
if (val1 == HIGH)
{ // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else
{
digitalWrite(ledPin, HIGH); // turn LED ON
}
if (val2 == HIGH)
{ // check if the input is HIGH (button released)
digitalWrite(ledPin2, LOW); // turn LED OFF
} else
{
digitalWrite(ledPin2, HIGH); // turn LED ON
}
// read input value
if (val3 == HIGH && val1 == HIGH)
{ // check if the input is HIGH (button released)
digitalWrite(ledPin3, LOW); // turn LED OFF
}
else
{
digitalWrite(ledPin2, HIGH);// turn LED ON
digitalWrite(ledPin, HIGH);
}
}