How simplify switch case logic ?

Hello this is my program that turn on LEDS in sequences according to number of push button presses, i managed to make the program using switch - case. but i think it’s too long and not efficient.

byte led[] = {8,9,10,11}; //led pin as array
byte push_button= 12; //push button pin
byte button_condition,count = 0; //button and condition
int i; // number of iteration in for loop declaring array
void setup() {
  //Declaring pin as output
  for (i=0;i<4;i++){
    pinMode(led[i],OUTPUT);
  }
    //Push button as input
    pinMode(push_button,INPUT);
}

void loop() {
  //Reading push button
  button_condition = digitalRead(push_button);

  //Counting number of press
  if (button_condition == 1){
    count++;
    delay(300);
  }

  //Switch-case start according to count variable
  switch (count){
    //Turning on only LED 1
    case 1:
      digitalWrite(8,HIGH);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      break;
    //Turning on only LED 2
    case 2:
      digitalWrite(8,LOW);
      digitalWrite(9,HIGH);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      break;
    //Turning on only LED 3
    case 3:
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
      digitalWrite(10,HIGH);
      digitalWrite(11,LOW);
      break;
    //Turning on only LED 4
    case 4:
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,HIGH);
      break;
     //Turning off all LED, and reset the count to 0
     default:
      digitalWrite(8,LOW);
      digitalWrite(9,LOW);
      digitalWrite(10,LOW);
      digitalWrite(11,LOW);
      count = 0;
      break;
  }
}

is there any way i can simplify it ?

If you never need more than one LED illuminated then you could turn them all off immediately before the SWITCH/CASE and then each CASE would just need the line of code to turn the appropriate LED on

Another approach (without needing SWITCH/CASE) would be to use the count variable to identify the LED pin - something like this

ledToLight = count + 7;
// code to turn all the LEDs off
digitalWrite(ledToLight, HIGH);

...R

Robin2:
If you never need more than one LED illuminated then you could turn them all off immediately before the SWITCH/CASE and then each CASE would just need the line of code to turn the appropriate LED on

Another approach (without needing SWITCH/CASE) would be to use the count variable to identify the LED pin - something like this

ledToLight = count + 7;

// code to turn all the LEDs off
digitalWrite(ledToLight, HIGH);




...R

what “+7” do in code?

It adds seven.

Your LED pins are in an array.
Exploit that.

Thank u guys, now my code is simple and clean
now, how can i turning on the led in pattern ?
ex : 1-2-3-4-3-2-1

lucky_pr:
Thank u guys, now my code is simple and clean
now, how can i turning on the led in pattern ?
ex : 1-2-3-4-3-2-1

By making the variable ‘count’ follow a pattern.

aarg:
By making the variable 'count' follow a pattern.

using what ? i reading some example codes somewhere, it uses modulus operator (%). what do you think ?

lucky_pr:
using what ?

Mathematics.

Do a site search on ‘night rider’ or ‘cylon’. There might be something there you can use.

lucky_pr:
using what ? i reading some example codes somewhere, it uses modulus operator (%). what do you think ?

i suspect you are over-thinking the problem

If you want something to count up and down then all you need is something like this

if (count >= 4) {
  increment = -1;
}
if (count <=1) {
   increment = 1;
}
if (button_condition == 1) {
  count += increment;
}

…R

lucky_pr:
Thank u guys, now my code is simple and clean
now, how can i turning on the led in pattern ?
ex : 1-2-3-4-3-2-1

What have you tried so far?