/*
An open-source clock for Arduino. This project will be enhanced on a regular basis
(cc) by Rob Faludi
http://www.faludi.com
http://creativecommons.org/license/cc-gpl
*/
#define vers = "1.01"
int second=0, minute=0, hour=0, weekday=1; // declare time variables
// these time variables are declared globally so they can be used ANYWHERE in your program
void setup() {
hour = 12;
weekday = 2;
Serial.begin(9600); // start up serial communications
}
void loop() {
static unsigned long lastTick = 0; // set up a local variable to hold the last time we moved forward one second
// (static variables are initialized once and keep their values between function calls)
// move forward one second every 1000 milliseconds
if (millis() - lastTick >= 1000) {
lastTick = millis();
second++;
serialOutput();
}
// move forward one minute every 60 seconds
if (second >= 60) {
minute++;
second = 0; // reset seconds to zero
}
// move forward one hour every 60 minutes
if (minute >=60) {
hour++;
minute = 0; // reset minutes to zero
}
// move forward one weekday every 24 hours
if (hour >= 24) {
weekday++;
hour = 0; // reset hours to zero
}
// reset weekdays on Saturday
if (weekday >= 8) {
weekday = 1;
}
}
void printWeekday (int dayNum) {
// print a weekday, based on the day number
switch (dayNum) {
case 1:
Serial.print ("Sunday");
break;
case 2:
Serial.print ("Monday");
break;
case 3:
Serial.print ("Tuesday");
break;
case 4:
Serial.print ("Wednesday");
break;
case 5:
Serial.print ("Thursday");
break;
case 6:
Serial.print ("Friday");
break;
case 7:
Serial.print ("Saturday");
break;
}
}
void serialOutput() {
// this function creates a clock you can read through the serial port
// your clock project will have a MUCH more interesting way of displaying the time
// get creative!
printWeekday(weekday); // picks the right word to print for the weekday
Serial.print(", "); // a comma after the weekday
Serial.print(hour, DEC); // the hour, sent to the screen in decimal format
Serial.print(":"); // a colon between the hour and the minute
Serial.print(minute, DEC); // the minute, sent to the screen in decimal format
Serial.print(":"); // a colon between the minute and the second
Serial.println(second, DEC); // the second, sent to the screen in decimal format
}
// this utility function blinks the an LED light as many times as requested
void blinkLED(byte targetPin, int numBlinks, int blinkRate) {
for (int i=0; i < numBlinks; i++) {
digitalWrite(targetPin, HIGH); // sets the LED on
delay(blinkRate); // waits for a blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}