simple and best way to shift LED on digital pins with a button

I am thinking of a simple program where I have 5 LED connected to arduino'd digital pins..
and a button connected to another digital pin of arduino...

Now I would like to press the button to shift the glow of the led

initial state LED 1 = HIGH

press button LED 1 = LOW LED 2 HIGH

press button LED 2 = LOW LED 3 HIGH

I thought if something like this but unable to put it in form... :stuck_out_tongue:

if (buttonState == HIGH)
{
S++
digitalWrite(s , HIGH);
}

be clear what are you trying to do
what do you want

5 LEDs connected on digital pins 2, 3, 4, 5, 6

pushbutton connected to digital pin 12

now when I power up the arduino
LED 1 is on.

every time I press the button the next LED will glow and the previous LED will shut off

whats the code that you are running on your board

void loop(){
if (digitalRead (button == LOW){  // assumes you have pin declared as INPUT with internal pullup enabled
LEDstate= LEDstate +1;  // cycle thru the LED modes
if (LEDstate == 5){  // reset it last LED
LEDstate = 1;
delay (50);  // debounce delay, crude
}
}
switch(LEDstate){
case 1:
digitalWrite (LED1, HIGH);  // assumes HIGH output to LED Anode, cathode to resistor to ground
digitalWrite (LED5, LOW);  // assume you also set all outputs LOW during void setup
break;
case 2:
digitalWrite (LED2, HIGH);
digitalWrite (LED1, LOW);
break;
case 3:
digitalWrite (LED3, HIGH);
digitalWrite (LED2, LOW);
break;
case 4:
digitalWrite (LED4, HIGH);
digitalWrite (LED3, LOW);
break;
case 5:
digitalWrite (LED5, HIGH);
digitalWrite (LED4, LOW);
break;
}
}

cutebuddy6:
whats the code that you are running on your board

I am running no code yet...

@ CrossRoads

I am little confused with both your and my code..
That if the button is kept pressed will the LEDs keep shifting..???

If so then I do not want that.. :frowning:

I want that when the button is pressed once and then until it is been released and pressed once again there should be no shift...

Yes, press once, the LED state is changed. Same case x: keeps repeating until button is pressed again.

Joy:
If so then I do not want that.. :frowning:

Well, the code would just need a minor tweak then to do what you want. How about this:

boolean pressed = false;

void loop()
{
  if (digitalRead (button) == LOW){  // assumes you have pin declared as INPUT with internal pullup enabled
    if (!pressed) {
      LEDstate= LEDstate +1;  // cycle thru the LED modes
      if (LEDstate == 5){  // reset it last LED
        LEDstate = 1;
      }
      delay (50);  // debounce delay, crude
      pressed = true;
    }
  }
  else
    pressed = false;

  switch(LEDstate){
  case 1:
    digitalWrite (LED1, HIGH);  // assumes HIGH output to LED Anode, cathode to resistor to ground
    digitalWrite (LED5, LOW);  // assume you also set all outputs LOW during void setup
    break;
  case 2:  
    digitalWrite (LED2, HIGH);
    digitalWrite (LED1, LOW);
    break;
  case 3:
    digitalWrite (LED3, HIGH);
    digitalWrite (LED2, LOW);
    break;
  case 4:
    digitalWrite (LED4, HIGH);
    digitalWrite (LED3, LOW);
    break;
  case 5:
    digitalWrite (LED5, HIGH);
    digitalWrite (LED4, LOW);
    break;
  }
}

Hope this helps,

Brad.

PS I assume that that delay(50) should always be called? There may have been a slight error in the original code (it only did delay when LEDstate hit 5)

yes, call delay anytime button press is detected.

I was saying that I do not want to use delay...

And truly speaking I am writing this program to change display modes on an LCD...

like
when case 1
LCD prints
x
when case 2
LCD print
y
when case 3
LCD prints
z

like this...