Switch case help with 6 pushbuttons but only 5 LEDs

Hi again guys. I’m just trying to understand the concept of switch case statements. I’m sure this would not be the best way to turn off the LED’s, i’m just trying to understand switch case statements for a single pushbutton. Would this work theoretically?

void loop(){
  
  if (buttonstate1 = HIGH){
    LED1 = HIGH;
    buttonstate6 = HIGH;
  }
  if (buttonstate2 = HIGH){
    LED2 = HIGH;
    buttonstate6 = HIGH;
  }
  if (buttonstate3 = HIGH){
    LED3 = HIGH;
    buttonstate6 = HIGH;
  }
  if (buttonstate4 = HIGH){
    LED4 = HIGH;
    buttonstate6 = HIGH;
  }
  if (buttonstate5 = HIGH){
    LED5 = HIGH;
    buttonstate6 = HIGH;
  }
  
  switch (buttonstate6){
    
    case 1:
    LED1 = LOW;
    break;
    
    case 2:
    LED2 = LOW;
    break;
    
    case 3:
    LED3 = LOW;
    break;
    
    case 4:
    LED4 = LOW;
    break;
    
    case 5:
    LED5 = LOW;
    break;
  }
}

Well, no. Not even theoretically.

You have several problems:

if (buttonstate1 = HIGH) -- the test for equality is ==. should read if (buttonstate1 == HIGH)

HIGH is, I believe, defined to be 1.

Your code sets buttonstate6 = HIGH inside of every if statement. I can't see where you set it to anything else but assuming you declared buttonstate6 with the usual: int buttonstate6;

that would initialize buttonstate6 to 0. Assuming one of your 'if' statements is satisfied it will be set to HIGH (or 1)

Now your switch statement tests the value of buttonstate 6. As I showed you that will be either 0 or 1. Your switch doesn't test for 0 and cases 2,3,4,&5 won't ever be satisfied.

Thanks for the replay RoyK! Would this example work?

void loop(){
  
  if (buttonstate1 == HIGH){
    LED1 = HIGH;
    int buttonstate6;
  }
  if (buttonstate2 == HIGH){
    LED2 = HIGH;
    int buttonstate6;
  }
  if (buttonstate3 == HIGH){
    LED3 = HIGH;
    int buttonstate6;
  }
  if (buttonstate4 == HIGH){
    LED4 = HIGH;
    int buttonstate6;
  }
  if (buttonstate5 == HIGH){
    LED5 = HIGH;
    int buttonstate6;
  }
  
  switch (buttonstate6){
    
    case 0:
    buttonstate6 == LOW;
    break;    

    case 1:
    LED1 = LOW;
    break;
    
    case 2:
    LED2 = LOW;
    break;
    
    case 3:
    LED3 = LOW;
    break;
    
    case 4:
    LED4 = LOW;
    break;
    
    case 5:
    LED5 = LOW;
    break;
  }
}

No,it wouldn't work.

You keep defining a new buttonstate6, then it goes out of scope, and is lost.

Didn't you try compiling it?

Here:

case 0:
    buttonstate6 == LOW;
    break;

You compare the non-existent buttonstate6 against the constant LOW, then discard the result. I don't think you wanted to do that.

I have not compiled anything because I’m just trying to understand the case switch statement. What i’m thinking is that a person has 6 different pushbuttons. 5 of those pushbuttons turn on a corresponding led. the Sixth pushbutton will check which of the 5 pushbuttons was pressed and turn off the corresponding LED.

the Sixth pushbutton will check which of the 5 pushbuttons was pressed and turn off the corresponding LED

I think you're over-thinking the problem.

I don't think you need a switch/case (unless you really want one), you need a couple of arrays.

the switch case is explained in the tutorial section - http://www.arduino.cc/en/Reference/SwitchCase -

the code you try to write looks is (I think), the variable remembers which LED is set ON and turns of OFF after a second.

void loop() {
int ledSwitchedON = 0;  // no led switched on

  // TODO read buttonstates

  if (buttonstate1 == HIGH){
    digitalWrite(LED1, HIGH);
    ledSwitchedON = 1;
  }
  if (buttonstate2 == HIGH){
    digitalWrite(LED2, HIGH);
    ledSwitchedON = 2;
  }
  if (buttonstate3 == HIGH){
    digitalWrite(LED3, HIGH);
    ledSwitchedON = 3;
  }

  delay(1000);
  switch (ledSwitchedON)
  {
    case 0: break;    
    case 1: digitalWrite(LED1, LOW); break;
    case 2: digitalWrite(LED2, LOW); break;
    case 3: digitalWrite(LED3, LOW); break;
  }
}

PLease spent some time in the tutorial and reference section of the arduino to learn the basics of the language. It is not very hard, not very simple either but spending some days going through the examples are worth the time.

succes, Rob

I don’t see the buttons are ever read:
buttonstatex = digitalRead(buttonx);

robtillaart is right. Before any of us read the classics we had to spend time learning to read with Dick and Jane. Start simple. Spend time with the tutorials. Copy a simple project. Get it working. Make a change - did it work? Yes? why? No? Why not? It won't take very long for you to catch on.