Posts Tagged ‘clock’
Kids Clock 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 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);
}
Kids Clock
Ok, that one is not rocket science but it realy realy worth the 20$ Arduino micro controller I used to make that kids clock.
The idea here is to make sure my 4 years old kid will not wake up at 3:30AM because he has confused the small needle with big needle. The code is real simple … red LED on before wake up time … green LED on after wake up time.
Following is the code … of course that could have been a lot more complicated but this is doing the job just fine. Stay tuned for future enhancements.
#include <avr/interrupt.h>
#include <avr/io.h>
#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT
#define RED_LED 8
#define GREEN_LED 11
#define BOARD_LED 13
#define NIGHT_TIME 36000 // 10*60*60
int led13 = HIGH;
long counter = 0;
long stepStack = 0;
long seconds = 0;
// Aruino runs at 16 Mhz, so we have 1000 Overflows per second...
// 1/ ((16000000 / 64) / 256) = 1 / 1000
ISR(TIMER2_OVF_vect) {
RESET_TIMER2;
counter++;
if(!(counter%1000))
{
// enqueue step message
stepStack++;
}
};
void startup()
{
int led = 0;
for(int i=0; i<5; i++)
{
digitalWrite(RED_LED, led);
digitalWrite(GREEN_LED, led^1);
led ^= 1;
delay(500);
}
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
}
void setup()
{
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BOARD_LED, OUTPUT);
//Timer2 Settings: Timer Prescaler /64,
TCCR2A |= (1<<CS22);
TCCR2A &= ~((1<<CS21) | (1<<CS20));
// Use normal mode
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
// Use internal clock - external clock not used in Arduino
ASSR |= (0<<AS2);
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);
RESET_TIMER2;
sei();
startup();
}
void loop()
{
if(stepStack)
{
// Toggle the LED
led13 ^= 1;
digitalWrite(13, led13);
seconds ++;
stepStack--;
}
if( seconds > NIGHT_TIME )
{
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
}
}




