Following code is to control two LEDs for on and off by two dedicated push buttons. I have used if() for each condition and I was not comfortable writing this code, as I could feel, it can be better with switch...case, because if we increase more buttons, it will be a messy solution for that. However, I couldn't get optimized switch...case for it. Can some one suggest the nice switch transformation for this if().

int buttonState1 = 0; // variale for reading pushbutton1 status
int buttonState2 = 0;  // variable to reading push button2 status 
//set pin numbers
int button1 = 2; // push button no. 1
int button2 =3;  // push button no. 2
int Led1 = 4; // led 1
int Led2 =5;  // led 2

void setup() {
  Serial.begin(9600); // serial begins at 9600
  // defining pin modes for led and buttons 
  pinMode(button1, INPUT); 
  pinMode(button2, INPUT); 
  pinMode (Led1, OUTPUT);
  pinMode (Led2, OUTPUT);  
};

void loop() {
 buttonState1 = digitalRead(button1); // reading pushbutton 1 in the variable buttonstate1 
 buttonState2 = digitalRead(button2); // reading pushbutton 2 in the variable buttonstate2 
 // condintion for led 1 ON, if pushbutton1 is pressed
 if (buttonState1 == HIGH) {            
  digitalWrite(Led1, HIGH);     
  }
  // condition for led2 ON, if pushbutton2 is pressed.
 if (buttonState2 ==HIGH){

  digitalWrite(Led2, HIGH);

  }
  // both led are off if no push button is pressed
  else{
    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
  }

};
New contributor
Birender Brar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Sidenote. The else only applies to the second if statement. So if you release button 1 while button 2 is still pressed, led 2 won't go off. – Gerben Dec 7 at 14:28
  • 1
    You could also do replace the entire code with, digitalWrite(Led1, digitalRead(button1)); digitalWrite(Led2, digitalRead(button2)); . Could you expand a bit on what you want to achieve, as that would limit the numerous different approaches you could take. If you want to read a large number of buttons, and change their accompanying leds, you could use an array for the button pin numbers, and an array. Then use a for loop to read each of the buttons one by one, and run some code. That would reduce the amount of "duplicate" code. – Gerben Dec 7 at 14:31

A switch case does not seem like it would be an effective way of controlling your LEDs. It would make more sense (and would probably be easier) to create a function instead to control the LEDs.

void buttonToggle(int buttonPin, int ledPin) {
  int buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

You would then need to replace your if statement with calls to the function (EX buttonToggle(Button1, Led1);

If you want to use a dedicated push button for every led, then it is not handy to use a switch .. case statement. This is because first you have to construct the switch-variable based on the readings of the switches before you can use it (the switch variable) for de case statements.

When you combine the reading of the switches with the setting of the leds you get a nice Loop-section:

    void loop() {
      digitalWrite(Led1, digitalRead(button1));
      digitalWrite(Led2, digitalRead(button2));
      digitalWrite(Led3, digitalRead(button3));
      digitalWrite(Led4, digitalRead(button4));
      digitalWrite(Led5, digitalRead(button5));
      // and so on
    }

Your Answer

Birender Brar is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.