Go Down

Topic: Project #8-Hourglass-Flashing light Alternative (Read 3058 times) previous topic - next topic

2fjeff

Hi
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:

Code: [Select]
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(currentTime-previousTime>interval){
    previousTime=currentTime;
      //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
    digitalWrite(led,HIGH);
    led++;
    if(led>7){
      for(int x=2;x<8;x++){
        digitalWrite(x,HIGH);  //when the final LED turns ON than LEDs will Flash successively until reset
        delay(250);
        digitalWrite(x,LOW);
      }
    }
  }
  switchState=digitalRead(switchPin);  //reads the state of the tilt switch
  if(switchState!=previousSwitchState){ 
    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:
Code: [Select]
if(currentTime-previousTime>interval){
    previousTime=currentTime;

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!

BadMrFresli

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:

Code: [Select]
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++){
        digitalWrite(y,HIGH);
      }
      delay(500); 
       for(int y = 2;y<8;y++){
        digitalWrite(y,LOW);
      }
      delay(500);
  }


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

Hope that helped

2fjeff

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. 

BadMrFresli

#3
Feb 08, 2015, 09:45 pm Last Edit: Feb 09, 2015, 05:38 pm by BadMrFresli
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...

janvier123


Henricus

Thanks a lot for this! Helped me too

But how could you make the 6 leds blink together?

Go Up