Inspired by the electrical projects I’ve been doing at work, I recently got an Arduino because I started to get some ideas for what I can do with a microcontroller. As I get to those, I’ll be updating the site with my progress.
Until then, I’m working with the project book that came with the starter kit I purchased and one of the projects is a digital hourglass: it keeps track of time in a way that doesn’t stop the program, and shows how much time has passed by lighting LEDs. The project is designed to use intervals of 10 min, turning on all LEDs once an hour has passed. When writing the program, I came across an issue with the code. Howevery, every example of this project has the same logic in their code, leaving me wondering if my implementation is somehow off.
There are 6 LEDs connected from pins 2 through 7. A variable led
is used to cycle between the pins to determine which LED to turn on.
The code, as shown in the project is
void loop(){ unsigned long currentTime = millis(); //check how long the program has been running //check if the desired amount of time has passed if(currentTime - previousTime > interval){ previousTime = currentTime; //save current time as checkpoint digitalWrite(led, HIGH); //turn on the current LED pin led++; if(led==7){ //do something to indicate all 6 LEDs have been turned on } //some more code to check if the reset switch has been activated</code> }
As the program runs, it checks how long since the last LED was turned on (currentTime - previousTime > interval
). It then lights an LED at led
, which starts off as 2. The program then increments led
by 1 to get it ready for the LED at the next pin. Then the program checks if led
is equal to 7, the idea being if pin 7 is turned on, then all 6 LEDs are on, and the full hour has passed so something should be done to get the user’s attention.
My issue is that, as written, the program thinks an hour passes when it does not.
After 10 minutes, LED 1 is turned on and led=3
.
After 20 minutes, LED 2 is turned on and led=4
.
After 30 minutes, LED 3 is turned on and led=5
.
After 40 minutes, LED 4 is turned on and led=6
.
After 50 minutes, LED 5 is turned on and led=7
.
Now that led==7
, the program does the user-attention-grabby-bit without reaching the full hour.
There are two ways to fix this:
- Change the value
led
has to reach in order to start up the user attention bit.
By changing the evaluation fromif(led == 7)
toif(led == 8)
, then we add onto our list above.
After 60 minutes, LED 6 is turned on andled=8
. Now we can do the attention grabber. - Change the order the program checks and increments
led
.
Currently,led
is incremented and then the program takes a look at it. This means the program never takes a look at the variable while it represents the pin that turned on, it’s looking into the future for which pin turns on next.
If we set the increment AFTER the check, then the if statement checks againstled
while it is still equal to the pin activated, and we’re confident the program is seeing the pin turned on as they turn on.
That’s the issue I came across and how I choose to solve it. Let me know if you came across a similar problem doing this project, or how you got it to work with the code they had written.
EDITED 5/7/2018: Redid the code block after learning how to do them properly.