Pages: [1]   Go Down
Author Topic: simple and best way to shift LED on digital pins with a button  (Read 527 times)
0 Members and 1 Guest are viewing this topic.
INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 382
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 382
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought if something like this but unable to put it in form... smiley-razz

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

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 382
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

whats the code that you are running on your board
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 549
Posts: 27427
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
}
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 382
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

whats the code that you are running on your board

I am running no code yet...
Logged

INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 382
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@ 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.. smiley-sad

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

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 549
Posts: 27427
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Salem, Oregon
Offline Offline
Sr. Member
****
Karma: 7
Posts: 310
Friends don't let friends use Strings
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If so then I do not want that.. smiley-sad

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

Code:
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)
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 549
Posts: 27427
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, call delay anytime button press is detected.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 382
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: February 21, 2012, 10:52:40 pm by Joy » Logged

Pages: [1]   Go Up
Jump to: