Go Down

Topic: simple and best way to shift LED on digital pins with a button (Read 552 times) previous topic - next topic

Joy

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

Joy

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

Code: [Select]
if (buttonState == HIGH)
{
S++
digitalWrite(s , HIGH);
}


cutebuddy6


Joy

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

cutebuddy6


CrossRoads

Code: [Select]
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;
}
}
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Joy


Joy

@ 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.. :(

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...

CrossRoads

Yes, press once, the LED state is changed. Same case x: keeps repeating until button is pressed again.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Brad Burleson


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


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

Code: [Select]
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)

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Joy

#11
Feb 22, 2012, 04:06 am Last Edit: Feb 22, 2012, 04:52 am by Joy Reason: 1
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...

Go Up