Can this be written shorter and more efficient?

I adapted the code below from the basic arduino button sketch. What I was attempting is to see how many different light modes I can make with only two switches. The code below works perfect.

My question is about how to shorten the code and make it easier on me and the reader. If there is a better way I would appreciate any advice. For example maybe function declaration. Examples would be ideal.

Thank You

const int buttonPin2 = 2;     // the number of the pushbutton pin
const int buttonPin3 = 3;     // the number of the pushbutton pin
const int buttonPin4 = 4;     // the number of the pushbutton pin
const int ledPin2 =  10;      // the number of the LED pin
const int ledPin3 =  11;      // the number of the LED pin
const int ledPin4 =  12;      // the number of the LED pin
// variables will change:
int buttonState2 = 0;         // variable for reading the pushbutton status
int buttonState3 = 0;         // variable for reading the pushbutton status
int buttonState4 = 0;         // variable for reading the pushbutton status
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin2, OUTPUT);  
  pinMode(ledPin3,OUTPUT);
  pinMode(ledPin4,OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT); 
  pinMode(buttonPin3,INPUT); 
  pinMode(buttonPin4,INPUT); 
}

void loop(){
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);

  // check if the pushbutton is pressed.
  if (buttonState2 == HIGH) {     
    // do something to the LEDs:    
    digitalWrite(ledPin2, HIGH); 
    digitalWrite(ledPin3,LOW); 
    digitalWrite(ledPin4,LOW);  
  } 
  if(buttonState3 == HIGH) {
    // do something to the LEDs:    
    digitalWrite(ledPin2,LOW); 
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4,LOW);
  }
  if(buttonState4 ==HIGH){
    // do something to the LEDs:    
    digitalWrite(ledPin2,LOW);
    digitalWrite(ledPin3,LOW);
    digitalWrite(ledPin4,HIGH);
  }
  if(buttonState2 ==HIGH && buttonState3 == HIGH && buttonState4==HIGH) {
    // do something to the LEDs:    
    digitalWrite(ledPin2, HIGH);  
    digitalWrite(ledPin3, HIGH); 
    digitalWrite(ledPin4,HIGH);
    delay(500);
    digitalWrite(ledPin2, LOW);  
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4,LOW);
    delay(500); 
  }
  if(buttonState2 ==LOW && buttonState3 == LOW && buttonState4==LOW){
    // do something to the LEDs:    
    digitalWrite(ledPin2, HIGH);  
    digitalWrite(ledPin3, LOW); 
    digitalWrite(ledPin4,LOW);
    delay(500);
    digitalWrite(ledPin2, LOW);  
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4,LOW);
    delay(500); 
    digitalWrite(ledPin2, LOW);  
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4,HIGH);
    delay(500); 
  }
}
const int buttonCount = 3;

const int buttonPins[buttonCount] = {2, 3, 4};   // the number of the pushbutton pin
const int ledPins[buttonCount] = {10, 11, 12};      // the number of the LED pin
// variables will change:
int buttonStates[buttonCount};

void setup() {
  // initialize the LED pin as an output:
   for (int i = 0; i < buttonCount; i++)
      {
      pinMode(ledPins[i] OUTPUT);  
      pinMode(buttonPins[i], INPUT); 
      }
}

void loop()
  {
    for (int i = 0; i < buttonCount; i++)
      {
      buttonStates[i] = digitalRead(buttonPins[i]);
      }

  // Generate a value from 0 to 7 based on which switches are closed
  int value =  buttonStates[0] + 2* buttonStates[1] + 4* buttonStates[2];

  switch (value)
     {
  case 1:  // Only button[0] is on
    digitalWrite(ledPins[0], HIGH); 
    digitalWrite(ledPins[1], LOW); 
    digitalWrite(ledPins[2], LOW); 
    break;

  case 2: // Only button[1] is on
    digitalWrite(ledPins[0], LOW); 
    digitalWrite(ledPins[1], HIGH); 
    digitalWrite(ledPins[2], LOW); 
    break;

   case 4: // Only button[2] is on
    digitalWrite(ledPins[0], LOW); 
    digitalWrite(ledPins[1], LOW); 
    digitalWrite(ledPins[2], HIGH); 
    break;

 case 7: // All three switches are on
    digitalWrite(ledPins[0], HIGH); 
    digitalWrite(ledPins[1], HIGH); 
    digitalWrite(ledPins[2], HIGH); 
    delay(500);
    digitalWrite(ledPins[0], LOW); 
    digitalWrite(ledPins[1], LOW); 
    digitalWrite(ledPins[2], LOW); 
    delay(500); 
    break;

  case 0: // All three switches off
    digitalWrite(ledPins[0], HIGH); 
    digitalWrite(ledPins[1], LOW); 
    digitalWrite(ledPins[2], LOW); 
    delay(500);
    digitalWrite(ledPins[0], LOW); 
    digitalWrite(ledPins[1], HIGH); 
    digitalWrite(ledPins[2], LOW); 
    delay(500); 
    digitalWrite(ledPins[0], LOW); 
    digitalWrite(ledPins[1], LOW); 
    digitalWrite(ledPins[2], HIGH); 
    delay(500); 
    break;

  }
}

[/quote]

Even smaller:

const int buttonCount = 3;

const int buttonPins[buttonCount] = {2, 3, 4};   // the number of the pushbutton pin
const int ledPins[buttonCount] = {10, 11, 12};      // the number of the LED pin
// variables will change:
int buttonStates[buttonCount};

void setup() {
  // initialize the LED pin as an output:
   for (int i = 0; i < buttonCount; i++)
      {
      pinMode(ledPins[i] OUTPUT);  
      pinMode(buttonPins[i], INPUT); 
      }
}

void loop()
  {
    for (int i = 0; i < buttonCount; i++)
      {
      buttonStates[i] = digitalRead(buttonPins[i]);
      }

  // Generate a value from 0 to 7 based on which switches are closed
  int value =  buttonStates[0] + 2* buttonStates[1] + 4* buttonStates[2];

  switch (value)
     {
  case 1:  // Only button[0] is on
  case 2:  // Only button[1] is on
  case 4:  // Only button[2] is on
   for (int i = 0; i < buttonCount; i++)
      digitalWrite(ledPins[i], buttonStates[i]); 
    break;

 case 7: // All three switches are on
   for (int i = 0; i < buttonCount; i++)
     digitalWrite(ledPins[i], HIGH); 
   delay(500);
   for (int i = 0; i < buttonCount; i++)
     digitalWrite(ledPins[i], LOW); 
   delay(500); 
   break;

  case 0: // All three switches off
       for (int i = 0; i < buttonCount; i++)
          for (int j = 0; j < buttonCount; j++)
             {
             digitalWrite(ledPins[j], j==i); 
             delay(500);
             }
    break;

  }
}

[/quote]
[/quote]

Thanks johnwasser and KE7GKP.

// Generate a value from 0 to 7 based on which switches are closed
  int value =  buttonStates[0] + 2* buttonStates[1] + 4* buttonStates[2];

Why is it written like this?

So that you get a number with each switch representing a different binary bit in the number. That way you can tell what combinations of switches are being pressed at any time.

note:

Just noticed that the OP misses some button combinations in his code. Probably that is on purpose, but it is implicit.

By adding a - default: // do nothing - in the switch(value) block or the missing cases (3,5 ,6) John's code would make that explicit.

roybenmo:

// Generate a value from 0 to 7 based on which switches are closed

int value =  buttonStates[0] + 2* buttonStates[1] + 4* buttonStates[2];




Why is it written like this?

Because it’s shorter than and equivalent to:

int value = 0;
if (buttonStates[0])
    value += 1;
if (buttonStates[1])
    value += 2;
if (buttonStates[2])
    value += 4;

FYI: HIGH == 1 == true and LOW == 0 == false
I could have used <<1 and <<2 instead of *2 and *4 but many beginners aren’t familiar with shift operators.

// Generate a value from 0 to 7 based on which switches are closed
  int value =  buttonStates[0] + 2* buttonStates[1] + 4* buttonStates[2];

  switch (value)
     {
  case 1:  // Only button[0] is on
  case 2:  // Only button[1] is on
  case 4:  // Only button[2] is on
   for (int i = 0; i < buttonCount; i++)
      digitalWrite(ledPins[i], buttonStates[i]); 
    break;

Why is case 1 button[0] not button[1]? and why is case 4 not called case 3?

How would I write it if I had 8 cases?
on on on
on on off
on off on
off on on
off off on
off on off
on off off
off off off

Why is case 1 button[0] not button[1]?

Because when button 0 is on the numeric value is 1.
When button 1 is on the numeric value is two.

How would I write it if I had 8 cases?

on on on -> case 7
on on off -> case 6
on off on -> case 5
off on on -> case 3
off off on -> case 1
off on off -> case 2
on off off -> case 4
off off off -> case 0
Assuming the on/off of these are written from left to right as button 2, button 1, button 0
It is called binary notation.