Project #8-Hourglass-Flashing light Alternative

This is my first post, and I am just learning programming, and am looking for some help.
I am working on the Hourglass project #8 in the starter kit. The project consists of 6 LEDs that turn on in succession at a given interval. (ie The first LED turns on after 10 seconds and stays on, the next LED turns on after an additional 10 seconds and so on until all 6 LEDs are on.) The program is reset via a tilt switch and then the process starts over.
The book asks you to try and make something occur once all the LEDs are on, as an indicator that the time is up. I have added some code so that the 6 LEDs will flash for a quarter second each in succession. This works relatively well, except after each LED flashing cycle there is 1 interval pause before the next flashing cycle occurs. I would like to eliminate this pause, so that the LEDs simply flash without the interval pause until the program is reset. Here is the code:

const int switchPin=8;  //pin eight is named switchPin the pin that will reset the hourglass
unsigned long previousTime=0;  //creates a variable that stores when the last LED was turned on
int switchState=0;  //creates a variable that stores the switch state which is initially set to open
int previousSwitchState=0;  //creates a variable that stores the last switch state and is initially set to zero, these switchstate variables will be used to compare the state of the switch to know when to rset the leds
int led=2;  //creates a variable called led and is initially associated with pin 2
long interval=10000;  //creates a variable called interval which is set at 10000 milliseconds (10 sec), this will be the interval between leds coming on

void setup(){
  for(int x=2;x<8;x++){
    pinMode(x,OUTPUT);  //sets pins 2 to 7 as outputs to control the LEDs turning on and off
  pinMode(switchPin,INPUT);  //sets the switchpin to be input to act as a reset switch for the program

void loop(){
  unsigned long currentTime=millis();  //creates a variable called current time that tracks the length of time since the last reset
      //if the currentTime minus the previousTime is greater than the interval (ie if a minute has passed) than the new previousTime will be the currentTime Value and the LED will turn on and the next LED will be at the ready
      for(int x=2;x<8;x++){
        digitalWrite(x,HIGH);  //when the final LED turns ON than LEDs will Flash successively until reset
  switchState=digitalRead(switchPin);  //reads the state of the tilt switch
    for(int x=2;x<8;x++){
      digitalWrite(x,LOW);  //if the switch opens all the LEDs will turn off
    led=2;  //this sets it so the first LED will be the first to turn on once the switch closes
    previousTime=currentTime;  //this updates the time so the intervals stay proper
  previousSwitchState=switchState;  //this sets the switchstate back to closed without the need to compare

I believe the issue has to do with the lines:


because of that i have to wait 1 interval before it checks to see if the LED>7
Does anyone know how I can get it to stop checking the time once the LED>7 and simply keep flashing the lights until I reset it via the tilt switch?
Any and all help is greatly appreciated!
Thanks for taking the time to read my post!

Hi 2fjeff

I had the same problem.
I solved it when i took the if function for led>7 out of the other if function:

if(jetztZeit - vorherZeit > interval){   //Sobald die Variable die Intervallänge erreicht
    vorherZeit = jetztZeit;                //wird der Vergleichswert hochgesetzt   
    digitalWrite(led,HIGH);                //und die aktuelle LED angesteuert
    led++;                                 //die aktuelle LED wird auf die nächste geschaltet                   
    if(led > 7){
      for(int y = 2;y<8;y++){
       for(int y = 2;y<8;y++){

This way the programm wont stop comparing, but the blinking function will not wait for it.

Hope that helped

Thank You so much for replying to my post, Danke Shoen!!

Your solution has worked perfectly! As I understand it, by taking the second "if" statement out of the brackets of the first "if" statement it now operates independently of the first statement. Is this correct? Thanks for your help, I am still trying to understand how the microcontroller actually processes the instructions.

Bitte schön

I think you understood it. The commands in the first if statment are only executed every interval. After taking it out, the second statement is working continuously.

Now to the bigger task of changing the LEDs directions. Havent figured that out yet...

worked perfectly!


Thanks a lot for this! Helped me too

But how could you make the 6 leds blink together?