Programming a simple button LED circuit with Arduino

1.9k views Asked by At

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);
          }
    }
} 
3

There are 3 answers

0
LS_ᴅᴇᴠ On

It would be easier if you draw a I/O map:

switchL switchR |  lR   lG   mB   rG   rR
   0       0    |  1    0    0    0    1
   0       1    |  1    0    0    1    0
   1       0    |  0    1    0    0    1
   1       1    |  0    0    1    0    0

and just write outputs as function of inputs:

digitalWrite(3, !switchL            );
digitalWrite(4,  switchL && !switchR);
digitalWrite(5,  switchL &&  switchR);
digitalWrite(6, !switchL &&  switchR);
digitalWrite(7,             !switchR);

If you prefer keep using nested IFs, you must always keep in mind that each output is a function of both inputs.

0
sanch On

I have a dim understanding of the functions you were using, however it was your answer that lead me to solve my issue. All I needed to was add another conditional in two of my ifs.

    if (switchL == HIGH && switchR == LOW) { //When the left button is pressed but the right button is not
  digitalWrite(4, HIGH);
  digitalWrite(3, LOW);
} else {
  digitalWrite(4, LOW);
  digitalWrite(3, HIGH); 
}
 if (switchR == HIGH && switchL == LOW) { //When the right button is pressed but the left button is not
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);       
} else {
  digitalWrite(6, LOW);
  digitalWrite(7, HIGH); 
}
0
Madivad On

You tried to overthink it. First off, in your 2nd last if test, you're setting D5 HIGH and then immediately testing if it's HIGH. It can't be anything else, therefore the else in the final if will never be actioned.

All I would do is move your tricky bit (the double button test) to the top of your code, and then test each of the other buttons within the else of that above test. Now that reads harder than it has to. Here's the code:

void loop() {
  switchL = digitalRead(2);
  switchR = digitalRead(8);

    // first test if both buttons are pressed
    if (switchL == HIGH && switchR == HIGH){
      digitalWrite(5, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
    } else {
          // now that's out the way, we test for everything else as a whole here
          // first test switchL
        if (switchL == HIGH) {
          digitalWrite(4, HIGH);
          digitalWrite(3, LOW);
        } else {
          digitalWrite(4, LOW);
          digitalWrite(3, HIGH); 
        } // end if switchL
          // then test switchR
        if (switchR == HIGH) {
          digitalWrite(6, HIGH);
          digitalWrite(7, LOW);       
        } else {
          digitalWrite(6, LOW);
          digitalWrite(7, HIGH); 
        } //end if switchR
    }     //end else of both high
}