Kids Clock V2.0

The KidsClock v2.0

A big problem with my first version of the KidsClock was that despite my efforts to track time accurately, it was drifting a few minutes everyday. This is because the Arduino does not come with a built-in real time clock like full scale computers.

To fix that problem I added a real time clock chip, the DS1307, to my circuit. Using this chip and some libraries from the Arduino community have make the code a lot  simpler.

The KidsClock v2.0 Circuit

The KidsClock v2.0 Circuit

The ds1307 circuit has a battery that allows your Arduino to be unpluged without losing time. This is a great improvement over the previous version.

The libraries I use can be found at:
http://code.google.com/p/arduino-time/

To set the time of the RTC to the local time I used this sample project:
http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock

Following is the code of the application. The clock red led turns on at 20:00h and the green led at 6:30 in the morning using the Alarm library.

/*
 * kidsclock2.pde
 */

#include
#include
#include
#include 

#define RED_LED 8
#define GREEN_LED 11
#define BOARD_LED 13

// 6:30 am every day
#define mh 6
#define mm 30

// 20:00 pm every day
#define eh 20
#define em 0

int boardLedState = HIGH;

void setup()
{
  Serial.begin(9600);    

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time"); 

  Alarm.alarmRepeat(mh,mm,0, MorningAlarm);
  Alarm.alarmRepeat(eh,em,0,EveningAlarm);  

  InitLEDs();
}

void InitLEDs()
{
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BOARD_LED, OUTPUT); 

  if( (hour() == mh && minute() >= mm ) ||
      (hour() == eh && minute() < em) ||       (hour() > mh && hour() < eh) )
  {
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, HIGH);
  }
  else
  {
    digitalWrite(RED_LED, HIGH);
    digitalWrite(GREEN_LED, LOW);
  }
}

void MorningAlarm()
{
  Serial.println("Alarm: - Morning");
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, HIGH);
}

void EveningAlarm()
{
  Serial.println("Alarm: - Evening");
  digitalWrite(RED_LED, HIGH);
  digitalWrite(GREEN_LED, LOW);
}

void  loop()
{
  boardLedState = !boardLedState;
  digitalWrite( BOARD_LED, boardLedState);

  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Tags: , , , ,

Post a Reply

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

Top