Within hours of my first Freeduino build (from a kit), I'm really pumped about the platform. Since my family Scrabble game takes a long time, I thought I'd build a simple timer for encouraging us to make our moves ![]()
I took the stock board and added two red LEDs - in pins 9 and 10. The on-board LED already being active in pin 13. In my code I set three different times: one for continuous flashing of the LED on 13, what I call the heartbeat.
I set a second time for when to warn the game players that things are expiring soon. When this time is reached, the LED in pin 9 starts to blink along with the heartbeat LED.
The third time is set for when the game is over or expired. This lights the final LED and stops the other LEDs from flashing anymore. Here's my code, hope it helps someone else trying to figure out how to do this kind of recursive time checking without using delay(). I'm sure there are lots of areas to improve - please comment if you see them. I'm sure I don't need all this recursive stuff and can probably do away with have my recordkeeping variables too ![]()
/* Game Timer
*
* Uses three time variables - one for a flashing LED that acts as a heartbeat
* One for a warning that turns on after a period of time
* Another that turns on when all the time has expired
*
* Based on http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
* which allows you to not use the delay() function, so some code
* can continue to run all the time without pausing
*
*/
int ledPin = 13; // LED connected to digital pin 13 - heartbeat LED
int ledPin2 = 10; // LED connected to pin 10 - warning LED
int ledPin3 = 9; // LED connected to pin 9 - expiry LED - when lit, game is over
int value = HIGH; // default value for heartbeat LED at startup
long previousMillis = 0; // will store last time LED 1 was updated
long previousMillis2 = 0; // will store last time LED 2 was updated
long previousMillis3 = 0; // will store last time LED 3 was updated
long timeHeartbeat = 500; // time between flashes of LED 1, as a sign of life (milliseconds)
long timeWarning = 5000; // time before warning light starts flashing (milliseconds)
long timeExpired = 10000; // time at which last LED turns on (milliseconds)
boolean beating = true; // status of each process for easy boolean checks
boolean warning = false; // status of each process for easy boolean checks
boolean expired = false; // status of each process for easy boolean checks
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pins as output
pinMode(ledPin2, OUTPUT); // sets the digital pins as output
pinMode(ledPin3, OUTPUT); // sets the digital pins as output
digitalWrite(ledPin, HIGH); // turn on heartbeat LED at startup
digitalWrite(ledPin2, LOW); // turn OFF warning LED at startup
digitalWrite(ledPin3, LOW); // turn OFF expiry LED at startup
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED.
// Check to see if it is time to flash the HEARTBEAT
if (millis() - previousMillis > timeHeartbeat){
// if the LED is off turn it on and vice-versa,
// making it flash at the interval set in timeHeartbeat
if (value == LOW)
value = HIGH;
else
value = LOW;
if (beating == true){
digitalWrite(ledPin, value);
}
if ((warning == true) and (expired == false)) {
digitalWrite(ledPin2, value);
}
// Check to see if it is time to turn on the WARNING LED
if (millis() - previousMillis2 > timeWarning) {
warning = true; // set flag so warning LED will start flashing with heartbeat
digitalWrite(ledPin2, HIGH);
// Check to see if it is time to turn on the EXPIRED LED
// Once this LED is turned on, it stays on until reset
if(millis() - previousMillis3 > timeExpired){
expired = true; // set flag so warning LED won't flash anymore
beating = false; // set flag so heartbeat LED won't flash anymore
digitalWrite(ledPin3, HIGH); // set final state of LED
digitalWrite(ledPin2, HIGH); // set final state of LED
digitalWrite(ledPin, HIGH); // set final state of LED
previousMillis3 = millis(); // remember the last time we blinked the LED
}
previousMillis2 = millis(); // remember the last time we blinked the LED
}
previousMillis = millis(); // remember the last time we blinked the LED
}
}