I have two push buttons and five LEDS wired up. In the circuit it the order is push button, five LEDs and push button. The order of the LEDs from left to right (starting after the first push button) red, green, blue, green, and red. I use the blue (middle) LED to differentiate the left and right sides. When the left push button is pressed, its respective green button turns on, and when the button is released, the red button comes on. The same functionality for the right side as well. So What I want to do is when both buttons are pushed, the greens light stay off and the blue light comes on. However when both buttons are pressed, both green lights come on as well as the blue light. Programming error of a circuitry problem? Here is my code:
//Using Arduino UNO
int switchL = 0; //Left button
int switchR = 0; //Right button
void setup() { //LED from left to right
pinMode(3, OUTPUT); //Red
pinMode(4, OUTPUT); //Green
pinMode(5, OUTPUT); //Blue
pinMode(6, OUTPUT); //Green
pinMode(7, OUTPUT); //Red
Serial.begin(9600);
}
void loop() {
switchL = digitalRead(2);
switchR = digitalRead(8);
if (switchL == HIGH) {
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
} else {
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
}
if (switchR == HIGH) {
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
} else {
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
}
if (switchL == HIGH && switchR == HIGH){
digitalWrite(5, HIGH);
if (digitalRead(5) == HIGH) {
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
} else {
digitalWrite(5, LOW);
}
}
}
It would be easier if you draw a I/O map:
and just write outputs as function of inputs:
If you prefer keep using nested
IF
s, you must always keep in mind that each output is a function of both inputs.