Switch case

Hello, i need help in my project, i'm trying to use switch case with push button and to repeat the the case i'm at it until i step to the next case. Should i use while loop in switch case to repeat the case or there is no need ?
I want to light up 3 leds in first case, second case to make them blink together and the default to be all turned off. How should i write the code ?
Thanks in advance !!

No, let loop() do the looping. Look at the example "BlinkWithoutDelay" in the IDE. It will show you how to structure your code. You can expand that example do deal with your three cases (ON, BLINK, OFF).

Give it a shot and if you get stuck, read the sticky note at the top of this forum about how to post your code using code tags. Then, post your code and explain where you are stuck.

if switch is run in the setup it is run once
if it is run in the loop it is run multiple times
now the "break" in each "case stops the switch process to the "}" so your loop can continue, and reset.
be sure to reset switch variable and any case variables for each run. unless you want the same result each time!

example:
// default 0 and any other number in the case switch will turn off all LED's
void loop () {
for (int LEDon = 0; LEDon <4; LEDon++) {
switch LEDon {
case 1:
digitalWrite( 2, HIGH); // set pin high or light LED on digital pin 2
break;
case 2:
digitalWrite( 3, HIGH); // set pin high or light LED on digital pin 3
break;
case 3:
digitalWrite( 4, HIGH); // set pin high or light LED on digital pin 4
break;
case 4:
digitalWrite( 5, HIGH); // set pin high or light LED on digital pin 5
break;
default:
digitalWrite( 2, LOW); // set pin LOW or turn off LED on digital pin 2
digitalWrite( 3, LOW); // set pin LOW or turn off LED on digital pin 3
digitalWrite( 4, LOW); // set pin LOW or turn off LED on digital pin 4
digitalWrite( 5, LOW); // set pin LOW or turn off LED on digital pin 5
break;
}
}
}

RichSr40:
if switch is run in the setup it is run once
if it is run in the loop it is run multiple times
now the "break" in each "case stops the switch process to the "}" so your loop can continue, and reset.
be sure to reset switch variable and any case variables for each run. unless you want the same result each time!

example:
// default 0 and any other number in the case switch will turn off all LED's
void loop () {
for (int LEDon = 0; LEDon <4; LEDon++) {
switch LEDon {
case 1:
digitalWrite( 2, HIGH); // set pin high or light LED on digital pin 2
break;
case 2:
digitalWrite( 3, HIGH); // set pin high or light LED on digital pin 3
break;
case 3:
digitalWrite( 4, HIGH); // set pin high or light LED on digital pin 4
break;
case 4:
digitalWrite( 5, HIGH); // set pin high or light LED on digital pin 5
break;
default:
digitalWrite( 2, LOW); // set pin LOW or turn off LED on digital pin 2
digitalWrite( 3, LOW); // set pin LOW or turn off LED on digital pin 3
digitalWrite( 4, LOW); // set pin LOW or turn off LED on digital pin 4
digitalWrite( 5, LOW); // set pin LOW or turn off LED on digital pin 5
break;
}
}
}

Interesting.

Of course switch LEDon should be switch (LEDon)

for (int LEDon = 0; LEDon  < 4; LEDon++)
{
  switch (LEDon)
  {
    case 1:
      digitalWrite( 2, HIGH); //  set pin high or light LED on digital pin 2
      break;
    case 2:
      digitalWrite( 3, HIGH); //  set pin high or light LED on digital pin 3
      break;
    case 3:
      digitalWrite( 4, HIGH); //  set pin high or light LED on digital pin 4
      break;
    case 4:
      digitalWrite( 5, HIGH); //  set pin high or light LED on digital pin 5
      break;
    default:
      digitalWrite( 2, LOW); //  set pin LOW or turn off LED on digital pin 2
      digitalWrite( 3, LOW); //  set pin LOW or turn off LED on digital pin 3
      digitalWrite( 4, LOW); //  set pin LOW or turn off LED on digital pin 4
      digitalWrite( 5, LOW); //  set pin LOW or turn off LED on digital pin 5
      break;
  } //END of  switch/case

} //END of   for (int LEDon = 0; LEDon  < 4; LEDon++)

Now on a 16MHz controller this does happen very fast :wink: .

Nehme:
How should i write the code ?

Preliminaries : Detect when the debounced switch undergoes a change of state. Then, increment a counter, switch examines counter. Voila!

Look around in IDE -> file/examples/digital/