Open-source Arduino Clock

This project is open-source code for an Arduino-based clock released under the GPL Creative Commons license. It’s a work-in-progress, and contributions are definitely welcome.

Nick Hardeman at Parsons used it to make his Morning Monster alarm clock.

/*
An open-source clock for Arduino. This project will be enhanced on a regular basis
(cc) by Rob Faludi
https://www.faludi.com
http://creativecommons.org/license/cc-gpl
*/
#define vers = "1.01"

#define ledPin 13
#define secondPin 4
#define minutePin 5
#define hourPin 6
#define weekdayPin 7

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() {

blinkLED(ledPin, 4, 100); // blink an LED at the start of the program, to show the code is running
Serial.begin(9600); // start up serial communications
pinMode(secondPin, INPUT); //pins for normally closed switches to set the time
pinMode(minutePin, INPUT);
pinMode(hourPin, INPUT);
pinMode(weekdayPin, INPUT);
digitalWrite(secondPin, HIGH); // writing an input high turns on pull-up resistors
digitalWrite(minutePin, HIGH);
digitalWrite(hourPin, HIGH);
digitalWrite(weekdayPin, HIGH);
}

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();
serialOutput();
second++;
}

// move forward one minute every 60 seconds
if (second > 59) {
minute++;
second = 0; // reset seconds to zero
}

// move forward one hour every 60 minutes
if (minute > 59) {
hour++;
minute = 0; // reset minutes to zero
}

// move forward one weekday every 24 hours
if (hour > 23) {
weekday++;
hour = 0; // reset hours to zero
}

// reset weekdays on Saturday
if (weekday > 7) {
weekday = 1;
}

checkButtons(); // runs a function that checks the setting buttons
}

void checkButtons() {
static boolean secPressed=false, minPressed=false, hourPressed=false, wkdayPressed=false; //track button state

if (digitalRead (secondPin)==LOW && secPressed == false) { // if a normally closed switch is pressed
second++; // advance by one second
secPressed = true; // note the pressed state
}
if (digitalRead (secondPin)==HIGH) secPressed = false; // reset the state when the button is released

if (digitalRead (minutePin)==LOW && minPressed == false) {
minute++;
minPressed = true;
}
if (digitalRead (minutePin)==HIGH) minPressed = false;

if (digitalRead (hourPin)==LOW && hourPressed == false) {
hour++;
hourPressed = true;
}
if (digitalRead (hourPin)==HIGH) hourPressed = false;

if (digitalRead (weekdayPin)==LOW && wkdayPressed == false) {
weekday++;
secPressed = true;
if (digitalRead (weekdayPin)==HIGH) wkdayPressed = false;
}
}

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);
}
}

 

 

14 comments on “Open-source Arduino Clock
  1. Joe says:

    So does this work without a crystal and all that crap?

  2. Franklin says:

    Hi,
    I am new in programming but interested in your project.Pls after compiling or verifying there is error ‘It’ is not declare in the scope ( for (int i=0; i&It;numBlinks; i++) {) {in the last but of the sketch.Can u kindly help me ?
    thanks

  3. I’d like to make a suggestion. Instead of using a big switch, i would add this line below the hour/minute/weekday declarations:

    String weekdays[] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

    and replace printWeekday(weekday); with Seria.print(weekdays[weekday-1]);

    In the project i used, i even modified the weekday starting value, so that Sunday is 0, not 1, to simplify the code even more

  4. Bordies says:

    it keeps giving em the error “expected primary-expression befor “;” token” in this line at the bottom of the code.

    for (int i=0;i<;numBlinks; i++) {

    What's wrong?

  5. Uno newbies says:

    I think this clock will stop working after 50 days when millis() will overflow

  6. cp1024 says:

    So did it go kaput after the 50 days?

  7. Samwell says:

    Cool project! There is code for an arduino based real time clock that uses the DS1307 chip so it wont reset after 50 days at: http://www.lucidtronix.com/tutorials/29

  8. Hi,
    I’m working on a distributed control algorithm for quadrotors. Just by skimming through your code I see that the fastest you are writing to the serial port (i.e. to the Xbee) is every 1 second. And that is one packet. From their spec sheet Xbee’s (S2) can handle 250kbs. But that is per frame. The question I’m interested is how many packets can you write per second. If I have 200 bit frames can I write that say (approx) 1000 times a second to the Xbee? Can Xbee supprot 1000HZ on the serial port? If not, in your experience have you come across something that can handle this sampling rate?

    Regards,
    Remus C Avram

  9. Anonymous says:

    A nice improvement is to add a battery backup at the Arduino’s power in fitting, so as to make it fully blackout-proof. Even better, you get to choose the size of the batteries.

    Most alarm clocks with battery backup have a serious design flaw. It remembers the time, but if the blackout is right at the time the alarm is supposed to go off, it won’t. Almost as bad, clock radios with this feature default to a buzzer that is pathetically too weak.

  10. Burak Sen says:

    Hello, I want to use LCD, and I need a wiring shematic of this project, can anyone help me?

  11. JammingJack says:

    I need my clock to sound an alarm every hour ..what do you guys think I should add to this code..any ideas plz

7 Pings/Trackbacks for "Open-source Arduino Clock"
  1. […] The clock code is based on the open-source-arduino-clock by Rob Faludi. I’ve made many changes, but the time counting the basically the same. So if […]

  2. […] Open-source Arduino Clock at Rob Faludi (tags: arduino clock howto DIY programming projects) […]

  3. […] alla School of Visual Arts di Manhattan e alla New York University, nel suo blog offre il codice per far funzionare una sveglia, al quale fra l’altro chiunque può contribuire … Chiaro? Un professore di New York che […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*