Introduction: Arduino Alarm Clock

Create a practical useful alarm clock. The Arduino will be used as a main computer for the alarm clock to calculate the time and set alarms. There will be buttons that allow navigation of a menu and will be used for setting the alarm and changing the time. There will also be a buzzer used as the alarm sound and can be turned off using the buttons.

Step 1: Materials

Things you will need

  1. x1 Ardiuno Duemilanove
  2. x1 Ardiuno K
  3. x1 Piezo Buzzer
  4. x12 Wires
  5. x2 Breadboard
  6. Tape
  7. Cardboard

Step 2: Plug Things In

Put in the Ardiuno Keypad Shield into the x2 Breadboards and connect digital pins 4,5,6,7,8,9,10, analog pin 0, power, and ground to the Ardiuno Duemilanove. As for your Piezo Buzzer, connect it to digital pin 3 and other ground to the Ardiuno Duemilanove. If you have any trouble with the wiring, check the picture above.

Step 3: Code the Ardiuno

For this step you will need to find the Ardiuno Time Library


http://playground.arduino.cc/code/time

*Ignore the formatting (ノ◕ヮ◕)ノ*:・゚✧

Use this code for the clock function:

===================================================================================


#
include < Time.h >

//Sample using LiquidCrystal library #include < LiquidCrystal.h >

/*******************************************************

This program will test the LCD panel and the buttons Mark Bramwell, July 2010

********************************************************/

// select the pins used on the LCD panel LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons int lcd_key = 0; int adc_key_in = 0; long seconds = 0; boolean clock = false; boolean set = true; boolean menu = false; boolean alarm = false; boolean alarming = false; boolean alarmSet = false; int page = 0; int years = 1970; int months = 1; int days = 1; int hours = 0; int minutes = 0; int secondsSet = 0; int delaying = 0; int selected = 0; int alarmPage = 0; int alarmHour = 0; int alarmMinute = 0; int alarmSecond = 0;# define btnRIGHT 0# define btnUP 1# define btnDOWN 2# define btnLEFT 3# define btnSELECT 4# define btnNONE 5 // read the buttons int read_LCD_buttons()

{ adc_key_in = analogRead(0); // read the value from the sensor // my buttons when read are centered at these valies: 0, 144, 329, 504, 741 // we add approx 50 to those values and check to see if we are close if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result // For V1.1 us this threshold if (adc_key_in < 50) return btnRIGHT; if (adc_key_in < 250) return btnUP; if (adc_key_in < 450) return btnDOWN; if (adc_key_in < 650) return btnLEFT; if (adc_key_in < 850) return btnSELECT;

// For V1.0 comment the other threshold and use the one below: /* if (adc_key_in < 50) return btnRIGHT; if (adc_key_in < 195) return btnUP; if (adc_key_in < 380) return btnDOWN; if (adc_key_in < 555) return btnLEFT; if (adc_key_in < 790) return btnSELECT; */

return btnNONE; // when all others fail, return this... }

void setup() { pinMode(2, OUTPUT);

Serial.begin(9600); lcd.begin(16, 2); // start the library lcd.setCursor(2, 0);

}

void loop() { if (alarmHour == hour() && alarmMinute == minute() && alarmSecond == second() && alarmSet == true) { alarming = true;

} if (alarming == false) {

digitalWrite(2, LOW); } else { digitalWrite(2, HIGH);

switch (lcd_key) // depending on which button was pushed, we perform an action {

case btnSELECT: { delaying++; if (delaying > 20) { delaying = 0; lcd.clear(); alarming == true; } break; }

} } lcd_key = read_LCD_buttons(); if (set == true) { if (page == 0) { lcd.setCursor(0, 0); lcd.print("What's the year?"); lcd.setCursor(6, 1); lcd.print(years);

} if (page == 1) { lcd.setCursor(0, 0); lcd.print("What's the month?"); lcd.setCursor(6, 1); if (months == 1) { lcd.setCursor(0, 1);

lcd.print(" January "); } else if (months == 2) { lcd.setCursor(0, 1); lcd.print(" February "); } else if (months == 3) { lcd.setCursor(0, 1); lcd.print(" March ");

} else if (months == 4) { lcd.setCursor(0, 1); lcd.print(" April "); } else if (months == 5) { lcd.setCursor(0, 1); lcd.print(" May ");

} else if (months == 6) {

lcd.setCursor(0, 1); lcd.print(" June "); } else if (months == 7) {

lcd.setCursor(0, 1); lcd.print(" July "); } else if (months == 8) {

lcd.setCursor(0, 1); lcd.print(" August "); } else if (months == 9) {

lcd.setCursor(0, 1); lcd.print(" September "); } else if (months == 10) {

lcd.setCursor(0, 1); lcd.print(" October "); } else if (months == 11) { lcd.setCursor(0, 1); lcd.print(" November "); } else if (months == 12) { lcd.setCursor(0, 1); lcd.print(" December "); } } if (page == 2) { lcd.setCursor(0, 0); lcd.print("What's the day? "); lcd.setCursor(7, 1); if (days < 10) { lcd.print("0" + String(days)); } else { lcd.print(days); } } if (page == 3) { lcd.setCursor(0, 0); lcd.print("What's the hour? "); lcd.setCursor(7, 1); if (hours < 10) { lcd.print("0" + String(hours)); } else { lcd.print(hours); } } if (page == 4) { lcd.setCursor(0, 0); lcd.print("What is minute? "); lcd.setCursor(7, 1); if (minutes < 10) { lcd.print("0" + String(minutes)); } else { lcd.print(minutes); } } if (page == 5) { lcd.setCursor(0, 0); lcd.print("What is second? "); lcd.setCursor(7, 1); if (seconds < 10) { lcd.print("0" + String(seconds)); } else { lcd.print(seconds); } } switch (lcd_key) { case btnRIGHT: { //lcd.print("RIGHT "); break; } case btnLEFT: { // lcd.print("LEFT "); break; } case btnUP: { //Year if (page == 0) { delaying++; if (delaying > 8) { delaying = 0; years++; if (years > 2099) { years = 1970; } }

} //Months else if (page == 1) { delaying++; if (delaying > 10) { delaying = 0; months++; if (months > 12) { months = 1; } } } //Days else if (page == 2) { delaying++; if (delaying > 8) { delaying = 0; days++; if ((months == 1) || (months == 3) || (months == 5) || (months == 7) || (months == 8) || (months == 10) || (months == 12)) { if (days > 31) { days = 1; } } else if ((months == 4) || (months == 6) || (months == 9) || (months == 11)) { if (days > 30) { days = 1; } } else if (months == 2) { if (days > 28) { days = 1; } } }

} //Hours else if (page == 3) { delaying++; if (delaying > 10) { delaying = 0; hours++; if (hours > 23) { hours = 0; } } } //Minute else if (page == 4) { delaying++; if (delaying > 10) { delaying = 0; minutes++; if (minutes > 59) { minutes = 0; } } } else if (page == 5) { delaying++; if (delaying > 10) { delaying = 0; seconds++; if (seconds > 59) { seconds = 0; } } } break; } case btnDOWN: { //Year if (page == 0) { delaying++; if (delaying > 8) { delaying = 0; years--; if (years < 1970) { years = 2099; } } } //Month else if (page == 1) { delaying++; if (delaying > 8) { delaying = 0; months--; if (months < 1) { months = 12; } } } else if (page == 2) { delaying++; if (delaying > 10) { delaying = 0; days--; if ((months == 1) || (months == 3) || (months == 5) || (months == 7) || (months == 8) || (months == 10) || (months == 12)) { if (days < 1) { days = 31; } } else if ((months == 4) || (months == 6) || (months == 9) || (months == 11)) { if (days < 1) { days = 30; } } else if (months == 2) { if (days < 1) { days = 28; } } } } //Hours else if (page == 3) { delaying++; if (delaying > 10) { delaying = 0; hours--; if (hours < 0) { hours = 23; } } } //Minutes else if (page == 4) { delaying++; if (delaying > 10) { delaying = 0; minutes--; if (minutes < 0) { minutes = 59; } } } else if (page == 5) { delaying++; if (delaying > 10) { delaying = 0; seconds--; if (seconds < 0) { seconds = 59; } } } } break;

case btnSELECT: lcd.clear();

{ if (page == 0) { delaying++; if (delaying > 20) { delaying = 0; page = 1; } } if (page == 1) { delaying++; if (delaying > 15) { delaying = 0; page = 2; } } if (page == 2) { delaying++; if (delaying > 15) { delaying = 0; page = 3; } } if (page == 3) { delaying++; if (delaying > 15) { delaying = 0; page = 4; } } if (page == 4) { delaying++; if (delaying > 15) { delaying = 0; page = 5; } } if (page == 5) { delaying++; if (delaying > 15) { delaying = 0; page = 0; setTime(hours, minutes, seconds, days, months, years); set = false; clock = true;

} } break; } case btnNONE: { //lcd.print("NONE "); break; } } } //============================================================================== else if (menu == true) { if (selected == 0) { lcd.setCursor(6, 0); lcd.print("Menu"); lcd.setCursor(0, 1); lcd.print("1:Set time *"); }

if (selected == 1) { lcd.setCursor(0, 0); lcd.print("1:Set time"); lcd.setCursor(0, 1); lcd.print("2:Set alarm *"); } if (selected == 2) { lcd.setCursor(0, 0); lcd.print("2:Set alarm "); lcd.setCursor(0, 1); lcd.print("3:Back *"); } switch (lcd_key) // depending on which button was pushed, we perform an action { case btnRIGHT: { //lcd.print("RIGHT "); break; } case btnLEFT: { // lcd.print("LEFT "); break; } case btnUP: { lcd.clear(); delaying++; if (delaying > 10) { delaying = 0; selected--; if (selected < 0) { selected = 2; } } break; } case btnDOWN: { lcd.clear(); delaying++; if (delaying > 10) { delaying = 0; selected++; if (selected > 2) { selected = 0; } }

break; } case btnSELECT: {

if (selected == 0) { delaying++; if (delaying > 20) { delaying = 0; menu = false; lcd.clear(); set = true; selected = 0; } } else if (selected == 1) { delaying++; if (delaying > 20) { delaying = 0; menu = false; alarm = true; lcd.clear(); selected = 0; } } else if (selected == 2) { delaying++; if (delaying > 210) { delaying = 0; menu = false; clock = true; lcd.clear(); selected = 0; } } break; } case btnNONE: {

break; } } } //============================================================================== else if (alarm == true) { if (alarmPage == 0) { lcd.setCursor(0, 0); lcd.print("What Hour?");

lcd.setCursor(7, 1); if (alarmHour < 10) { lcd.print("0" + String(alarmHour)); } else { lcd.print(alarmHour); } } else if (alarmPage == 1) { lcd.setCursor(0, 0); lcd.print("What Minute?");

lcd.setCursor(7, 1); if (alarmMinute < 10) { lcd.print("0" + String(alarmMinute)); } else { lcd.print(alarmMinute); } } else if (alarmPage == 2) { lcd.setCursor(0, 0); lcd.print("What Second?");

lcd.setCursor(7, 1); if (alarmSecond < 10) { lcd.print("0" + String(alarmSecond)); } else { lcd.print(alarmSecond);

} }

lcd_key = read_LCD_buttons(); // read the buttons switch (lcd_key) // depending on which button was pushed, we perform an action { case btnRIGHT: { //lcd.print("RIGHT "); break; } case btnLEFT: { // lcd.print("LEFT "); break; } case btnUP: { if (alarmPage == 0) { delaying++; if (delaying > 10) { delaying = 0; alarmHour++; if (alarmHour > 23) { alarmHour = 0; } } } else if (alarmPage == 1) { delaying++; if (delaying > 10) { delaying = 0; alarmMinute++; if (alarmMinute > 59) { alarmMinute = 0; } } } else if (alarmPage == 2) { delaying++; if (delaying > 10) { delaying = 0; alarmSecond++; if (alarmSecond > 59) { alarmSecond = 0; } } } break; } case btnDOWN: { if (alarmPage == 0) { delaying++; if (delaying > 10) { delaying = 0; alarmHour--; if (alarmHour < 0) { alarmHour = 23; } } } else if (alarmPage == 1) { delaying++; if (delaying > 10) { delaying = 0; alarmMinute--; if (alarmMinute < 0) { alarmMinute = 59; } } } else if (alarmPage == 2) { delaying++; if (delaying > 10) { delaying = 0; alarmSecond--; if (alarmSecond < 0) { alarmSecond = 59; } } } break; } case btnSELECT: {

if (alarmPage == 0) { delaying++; if (delaying > 20) { delaying = 0; alarmPage = 1; } } if (alarmPage == 1) { delaying++; if (delaying > 20) { delaying = 0; alarmPage = 2; } } if (alarmPage == 2) { delaying++; if (delaying > 20) { delaying = 0; alarmPage = 0; alarm = false; clock = true; alarmSet = true; lcd.clear(); } }

break; } case btnNONE: {

break; } } } //============================================================================== else if (clock == true) {

lcd.setCursor(2, 0); String monthWord = ""; String days = ""; if (day() < 10) { days = "0" + String(day()); } else { days = String(day()); } if (month() == 1) { monthWord = "Jan"; } else if (month() == 2) { monthWord = "Feb"; } else if (month() == 3) { monthWord = "Mar"; } else if (month() == 4) { monthWord = "Apr"; } else if (month() == 5) { monthWord = "May"; } else if (month() == 6) { monthWord = "Jun"; } else if (month() == 7) { monthWord = "Jul"; } else if (month() == 8) { monthWord = "Aug"; } else if (month() == 9) { monthWord = "Sep"; } else if (month() == 10) { monthWord = "Oct"; } else if (month() == 11) { monthWord = "Nov"; } else if (month() == 12) { monthWord = "Dec"; } lcd.print(days + ":" + monthWord + ": " + String(year())); lcd.setCursor(2, 1); // move cursor to second line "1" and 9 spaces over // if ((seconds/3600)%12<10){ // lcd.print(0); // } // lcd.print(String((seconds/3600)%12)+":"); // // if ((seconds/60)%60<10){ // lcd.print(0); // } // lcd.print(String((seconds/60)%60)+":"); // // if (seconds%60<10){ // lcd.print(0); // } // lcd.print(second()); // display seconds elapsed since power-up // // if(((seconds/43200)%2)== 0){ // lcd.print(" AM"); // } // else{ // lcd.print(" PM"); // } // seconds = millis()/1 hourFormat12(); if (hour() < 10) { lcd.print(String(0) + String(hour()) + ":"); } else { lcd.print(String(hour()) + ":"); } if (minute() < 10) {

lcd.print(String(0) + String(minute()) + ":"); } else { lcd.print(String(minute()) + ":"); } if (second() < 10) { lcd.print(String(0) + String(second())); } else { lcd.print(String(second())); } if (isAM()) { lcd.print(" AM"); } if (isPM()) { lcd.print(" PM"); } //lcd.setCursor(0,1)0; // move to the begining of the second line lcd_key = read_LCD_buttons(); // read the buttons

switch (lcd_key) // depending on which button was pushed, we perform an action { case btnRIGHT: { //lcd.print("RIGHT "); break; } case btnLEFT: { // lcd.print("LEFT "); break; } case btnUP: { if (alarming == true) { alarming = false; } break; } case btnDOWN: { // lcd.print("DOWN "); break; } case btnSELECT: { delaying++; if (delaying > 20) { delaying = 0; menu = true; lcd.clear();

clock = false;

} break; } case btnNONE: {

break; } } } }

===================================================================================



Controls:

Select - for general enter button

Up and Down - for up and down

Up to turn off the clock

Step 4: Encase and Housing

You can do this step however you want but how we did this was we first encased the ardiuno in tape to stop shifting and unplugging by accident. We then gave it a cardboard cover all around the project. The project is now finished.

Step 5: Done!

You're now done. Now you have an practical and easy to build Ardiuno alarm clock.