Feedback!

Alarm Clock

Views: 13787 Difficulty: 3 Status: Complete
Clock_alaram_big

Use the Wearable Wayfinder and the MCP79410 Real time clock chip to make an Arduino alarm clock.

Program the Wearable Wayfinder to be your very own DIY alarm clock. Use the Wayfinder's on board MCP79410 chip to keep track of the time even when the power is off (that's why we need the small lithium coin cell battery). In this tutorial, we use the the color clock code to show the time, but we also add an alarm which the user can set to trigger an electronic melody on the piezo buzzer.

Alarm Clock

Clock_alaram_big
Wearable Wayfinder with big simple alarm clock!

Video of DS1307 Alarm Clock, The DIY Wristwatch

Listen and Watch to color based (almost musical) wearable alarm clock.

Arduino Code for DS1307 Alarm Clock with TFT LCD Screen

This code is very similar to the code presented in our color clock tutorial, except now we have added alarm functionality. We use a piezo buzzer to sound the alarm, and a thumbwheel potentiometer to adjust the alarm time. The heart of our alarm code is:
  if (alarm_on){
    ww.draw_string(2, alarm_y,"Alarm is ON", ww.stroke.color_16(), 1);
    if (alarm_time.hour() == cur_time.hour()
      && alarm_time.minute() == cur_time.minute()){
         ww.playNote(names[cur_note++], 300);
         if (cur_note == 8) cur_note = 0;
     }
  } else {
    analogWrite(speakerPin, 0);
    ww.draw_string(2, alarm_y,"Alarm is OFF", ww.stroke.color_16(), 1);
  }
In the this code we check if the alarm is on, and display its state on the screen. Then if the alarm is on we check the current time against the alarm set time, if the hours (modulo 12 to account for military time of the DS1307) and minutes match. If the alarm time matches the current time we play a note and increment the note index so that we will hear an (off-key) musical scale until the minute changes or the alarm is turned off.
The check_buttons() function is where the alarm state and alarm time can be set. The check_buttons function goes like this:
void check_buttons(){
 if ( digitalRead(ww.btn1_pin) == HIGH && millis() - last_press > 800){
   tft.fillRect(0, alarm_y, 128, 62, ST7735_WHITE);  
   alarm_on = !alarm_on;
   last_press = millis();
 }
 if (digitalRead(ww.btn2_pin) == HIGH && millis() - last_press > 800){
   set_minute = !set_minute;
   tft.drawLine(80, alarm_y+8, 110, alarm_y+8, ST7735_WHITE);
   last_press = millis();
 }
 int val = analogRead(potPin);
 if (abs(val - old_val) > 1){
   tft.fillRect(64, alarm_y, 128, 12, ST7735_WHITE);  
   if(set_minute){
     val = map(val,0,1024,0,60);
     alarm_time = DateTime(cur_time.year(), cur_time.month(), cur_time.day(), 
                          alarm_time.hour(), val, 0 );
   } else { //set hour 
     val = map(val,0,1024,0,24);
     alarm_time = DateTime(cur_time.year(), cur_time.month(), cur_time.day(), 
                          val, alarm_time.minute(), 0 );
   } 
 }
 old_val = analogRead(potPin);
}
First, we check the buttons. If btnPin0 is pressed we toggle the alarm state and clear the screen. If btnPin1 is pressed we toggle between setting the minutes of the alarm time and setting the hour. The we check the potentiometer thumbwheel. If it has been twisted then the if statement:
 if (abs(val - old_val) > 1)
will evaluate to true. Then we will update either alarm time minute or the alarm time hour by mapping the thumbwheel reading ( a number between 0 and 1023) to either a minute (0-59) or an hour (0-12).
This code relies on several libraries: All available at the base Wearable Wayfinder Tutorial.
/* LucidTronix Wearable Wayfinder Color Clock
 * For instructions, details and schematic, See:
 * http://www.lucidtronix.com/tutorials/29
 */

#include <WearableWayfinder.h>
#include <Adafruit_GFX.h>   stroke // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <Wire.h>
#include <SPI.h>
#include <Color.h>
#include <HMC5883L.h>
#include <MCP79410.h>
#include <I2C.h>
#include <MMA8453Q.h>

// for leo:
Adafruit_ST7735  tft = Adafruit_ST7735(SS, 9, 8);
WearableWayfinder ww = WearableWayfinder(&tft);

int speakerPin = 6;
int potPin = 0;
int cur_sec = 0;
int cur_min = 0;
int millisecond_six = 0;
unsigned int last_sec = 0;
unsigned int last_press = 0;
int old_val = 0;
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int cur_note = 0;

boolean alarm_on = false;
boolean set_minute = false;
int time_y = 4;
int alarm_y = 38;


DateTime cur_time, alarm_time;
void setup(){  
  Wire.begin(); // Start the I2C interface.
  ww.initialize();
  //ww.set_date_time(__DATE__, __TIME__);
  alarm_time = ww.now();
}
void loop(){
  cur_time = ww.now();
  ww.show_time(2, time_y,3);
  ww.show_time(80, alarm_y, alarm_time);
  ww.show_date(30, 130);
  if(set_minute) tft.drawLine(96, alarm_y+8, 106, alarm_y+8,  Color(0,255,0).color_16());
  else {
    tft.drawLine(80, alarm_y+8, 90, alarm_y+8, ww.background.color_16());
    if (alarm_time.hour()>9) tft.drawLine(80, alarm_y+8, 90, alarm_y+8, Color(255,0,0).color_16());
    else tft.drawLine(85, alarm_y+8, 90, alarm_y+8, Color(255,0,0).color_16());
  }
  check_buttons();
  if (alarm_on){
    ww.draw_string(2, alarm_y,"Alarm is ON", ww.stroke.color_16(), 1);
    if (alarm_time.hour() == cur_time.hour()
      && alarm_time.minute() == cur_time.minute()){
         ww.playNote(names[cur_note++], 300);
         if (cur_note == 8) cur_note = 0;
     }
  } else {
    analogWrite(speakerPin, 0);
    ww.draw_string(2, alarm_y,"Alarm is OFF", ww.stroke.color_16(), 1);
  }
}  

void check_buttons(){
 if ( digitalRead(ww.btn1_pin) == HIGH && millis() - last_press > 800){
   tft.fillRect(0, alarm_y, 128, 62, ST7735_WHITE);  
   alarm_on = !alarm_on;
   last_press = millis();
 }
 if (digitalRead(ww.btn2_pin) == HIGH && millis() - last_press > 800){
   set_minute = !set_minute;
   tft.drawLine(80, alarm_y+8, 110, alarm_y+8, ST7735_WHITE);
   last_press = millis();
 }
 int val = analogRead(potPin);
 if (abs(val - old_val) > 1){
   tft.fillRect(64, alarm_y, 128, 12, ST7735_WHITE);  
   if(set_minute){
     val = map(val,0,1024,0,60);
     alarm_time = DateTime(cur_time.year(), cur_time.month(), cur_time.day(), 
                          alarm_time.hour(), val, 0 );
   } else { //set hour 
     val = map(val,0,1024,0,24);
     alarm_time = DateTime(cur_time.year(), cur_time.month(), cur_time.day(), 
                          val, alarm_time.minute(), 0 );
   } 
 }
 old_val = analogRead(potPin);
}

Parts

Title Description # Cost Link Picture
Wearable Wayfinder An Arduino on steroids! Full color LCD screen, accelerometer, compass, and real time clock loaded onto a wearable gadget! 1 $69.95 Link 3screens2
Permalink: http://lucidtronix.com/tutorials/42
A wearable real-time clock with vibrant color display....
The Wearable Wayfinder is a totally programmable, beautiful little computer for your wrist packed wit...
Connecting the MCP79410 real-time clock chip and our Heart Matrix you can show the time with love....
Standalone programmable Heart-Shaped LED display....
Coupling the HMC5883L Compass with our color library turns the Wearable Wayfinder into a handy naviga...
The MCP79410 breakout board gives easy access to this cute little timekeeper....
From simple rules emerge mysterious and intricate patterns....
Build your own clock with the MCP79410 chip and 4 7-Segment LED modules....