Go Down

Topic: controlling multiple LEDs with 1 button (turns LEDs on in sequence) (Read 3309 times) previous topic - next topic



I'm a newbie at Arduino and I'm struggling with my project..
I want the one button to activate 10 LED in sequence. Each LED will turn on after 15 secs

push the button..
1st LED turns on after 15 secs
2nd LED turns on after 30 secs
and so on..

I got to the point where I can make the first LED turns on 15 secs after the button is pushed
but when I try to add the 2nd LED I'm missing something.. it doesn't turn on or anything..

Any suggestion would help.
Thank you!


I'm missing something

Yes, you are. Something like some code for us to look at. Using the # icon, please.
The art of getting good answers lies in asking good questions.


Code: [Select]
#define LED  13   // the pin for the LED
#define LED 12  //defining LED 12 here makes the whole thing doesn't work..not sure how to define
#define BUTTON 7  // the input pin where the
                 // pushbutton is connected
int val = 0;     // val will be used to store the state
                 // of the input pin
int old_val = 0; // this variable stores the previous
                 // value of "val"
int state = 0;   // 0 = LED off and 1 = LED on

unsigned long startTime = 0;

void setup() {
  pinMode(13, OUTPUT);   // tell Arduino LED is an output
  pinMode(7, INPUT); // and BUTTON is an input
void loop(){
  val = digitalRead(7); // read input value and store it

  // check if there was a transition
  if ((val == HIGH) && (old_val == LOW)){
    state = 1 - state;
    startTime = millis();
delay(10); }
  old_val = val; // val is now old, let's store it
  if (state == 0 && (millis() - startTime) < 10000) {
    digitalWrite(13, LOW); // turn LED OFF
  } else {
    digitalWrite(13, HIGH);

if (state == 0 && (millis() - startTime) < 20000) {
    digitalWrite(12, LOW); // turn LED OFF
  } else {
    digitalWrite(12, HIGH);


Thank you PaulS.

I know there are still other problems with the code as well..
The LED turns on when the button is pushed for the first time
Then it waits 10 secs after the second push to turn on..
(i still have to change the time to 15000 in the code)


The test for val == HIGH and old_val == LOW suggests that you are using external pulldown resistors, yet no mention is made of how the switches are wired.

I'd suggest that you ditch the external pulldown resistors, if you indeed used any, and use the internal pullup resistors. Connect one side of the switch to the digital pin, and connect the other side to ground.

The switch will then read HIGH when not pressed and LOW when pressed.

Put each { on a new line. Use Tools + Auto Format to properly indent the code. Use some white space to separate logical blocks of code.

You have some code that deals with state == 0, but nothing that deals with state == 1. I'd suggest that compound if statements are not the way to go. Nested if statements are.

Code: [Select]
if(state == 1)
   if(millis() - startTime >= 10000)
   // Similar test for time

The else blocks can be empty. Their presence, with or without code, at least indicates that you considered the need for them, and decided that there was nothing to do in that block.
The art of getting good answers lies in asking good questions.

Go Up