Countdown led grid

I'm trying to write the code for a 10 segment led bar so that every 1 min an led turns off. It will be a 10min timer.

I can't get the code to work. It will turn off the first led after 60000mills or (1min) but the next just doesn't turn off.

Also I need to know the best way to start this timer because I want it to turn on and only run when inputs 14 and 15 are read high.


unsigned long interval=3000;  // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
unsigned long ledtimer1 = millis() + 60000;
unsigned long ledtimer2 = millis() + 120000;
unsigned long ledtimer3 = millis() + 180000;
unsigned long ledtimer4 = millis() + 240000;
unsigned long ledtimer5 = millis() + 300000;
unsigned long ledtimer6 = millis() + 360000;
unsigned long ledtimer7 = millis() + 420000;
unsigned long ledtimer8 = millis() + 480000;
unsigned long ledtimer9 = millis() + 540000;
unsigned long ledtimer10 = millis() + 600000;

int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 7;
int led6 = 8;
int led7 = 9;
int led8 = 10;
int led9 = 11;
int led10 = 12;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
 pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT); 
pinMode(led10, OUTPUT); 

digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
digitalWrite(led8, HIGH);
digitalWrite(led9, HIGH);
digitalWrite(led10, HIGH);


// the loop routine runs over and over again forever:
void loop() {
if ((unsigned long)(millis() - previousMillis) == ledtimer1) {
 previousMillis = millis();
 digitalWrite(led4, LOW); 
  if ((unsigned long)(millis() - previousMillis) == ledtimer2) {
 previousMillis = millis();
 digitalWrite(led8, LOW);  

First off change == to >=

if I want it to be at increments of 1min don't I need to stick with the 60,000 intervals?

I guess I'm not undersatnding correctly.

I will change it just would like to know the reason behind it because that states the first led will turn off anywhere from 60,000 on up....then the next at 120 on up and so fourth.....wont they interfere with each other?


Here's what I think: ledtimer1 is less than ledtimer2. The first test in loop will always evaluate as true well before the second test evaluates as true. The sketch will dutifully set led4 to LOW - an event that you won't notice, because led4 was already LOW - and set previousMillis to the current value of millis(). The second test will never evaluate as true, because the first test will update previousMillis before ledtimer2 times out.

I recommend that you consider using only one test in loop(), and one led timer that's always set to the same value. The values of the pins are happily sequential, so you can specify the pin number with a variable, and increment it each time that the led timer times out. Maybe something like this untested code:

int led = led1;   // point to the first LED
const unsigned long ledtimer = 600;   // 0.6 seconds for test; 60000 for 60 seconds

void loop() {
  if ((unsigned long)(millis() - previousMillis) >= ledtimer) {  
    previousMillis = millis();
    digitalWrite(led, LOW);
    led++;    // point to the next LED
  if (led > led10) {   // turned off the last LED?
    while(1){}   // Yes, wait forever; otherwise, keep going

If the led pins weren't sequential, you could accomplish the same thing by loading the led pin numbers into an array, and bumping the array index rather than the pin number.

The sketch has separate timer values for each LED. An alternative is to refrain from updating the value of previousMillis. That will let ledtimer2 elapse, and, even though the sketch will turn off many LEDs multiple times, you should get the results you want.