Alarm Clock Code - Arduino Mega, 16x2 LCD, Switchs

Using the latest Time library, does anyone have any open-source alarm clock code they wish to share utilizing the quartz crystal on the Arduino Mega? Looking for one with a 16x2 LCD and buttons to set clock and alarm. No need to re-invent the wheel if something already exists.

I have seen the following so far after a little searching: http://www.faludi.com/code/open-source-arduino-clock/ - No alarm or LCD sequences http://www.instructables.com/id/Solid-Wood-Digital-Clock/ - Code is far too complicated to modify http://nessming.org/blog/arduino-and-snorrrhea/ - No alarm set by buttons http://jmsarduino.blogspot.com/2009/05/alarm-clock-board-built.html - Assembly code and wrong-type of board http://www.arkadian.eu/pages/51/my-arduino-clock - Interfaces with other devices http://www.kencasting.com/?p=313 - Code is far too complicated to modify

Thank you! :)

That last link looks promising - why do you say "too complicated"?

That one uses a ds1307 RTC, an accelerometer to change between items, IR sensor, etc. It seems like so much to cut out that of code built around those functions. Maybe I am just being lazy and need to hack that code up and just add-in the functions for some momentary switches.

Here is what I have thus far for code, looking for some thoughts/insight on the commented section for reading the momentary SPST switches before trying to make that work for the alarm section too…

#include <LCD4Bit.h>       //LCD library
  #undef int()               //For stdio
  #include <stdio.h>         //For sprintf
  
  LCD4Bit lcd = LCD4Bit(2);  //2 lines
  
                             //Initial time set
  int sec=30;          
  int min=55;
  int hour=22;
  
  int amin=56;               //Initial alarm set
  int ahour=22;
  
  char time[9];              //Time string for LCD
  
  int check=0;               //Button set value
  
                             //Set button variables
  int lastButtonState = LOW; 
  int lastButtonState2 = LOW; 
  int lastButtonState3 = LOW;
  long lastDebounceTime = 0; 
  long debounceDelay = 50;  
  
  boolean alarmSet = false;  //Alarm on/off value
  
  void setup() {             //Initialization
    pinMode(13, OUTPUT);     //Arduino debug LED
    pinMode(3, OUTPUT);      //Piezo buzzer
    pinMode(22, INPUT);      //Set button
    pinMode(26, INPUT);      //Alarm time button
    pinMode(26, INPUT);      //Alarm on/off button
    
    Serial.begin(9600);      //Serial for debugging
    
    lcd.init();              //LCD prep
    lcd.clear();
  }
  
  char* inctime() {          //Time calc & print
  
    sec=sec+1;
    
    if(sec==60) {
      min=min+1;
      sec=0;
    }  
    if(min==60) {
      hour=hour+1;
      min=0; 
    }
    if(hour==24) {
     hour=0; 
    }
  
    if(sec%2==0) {                                  //True if sec is even number
      sprintf(time, "%2d:%2d:%2d", hour, min, sec); //Blink the ':'
    } 
    else {
      sprintf(time, "%2d %2d %2d", hour, min, sec); //Don't blink ':'
    }
  
    return time;
  }
  
  void checkbuttons() {
    Serial.println("Entering checkbuttons()");
    
    int reading = digitalRead(22);                                 //Set time
    int reading2 = digitalRead(26);                                //Alarm time
    int reading3 = digitalRead(30);                                //Alarm on/off
    
    //Switch to set the time
    if (reading == LOW && lastButtonState == HIGH && (lastButtonState != LOW && lastButtonState2 != LOW)) {
      Serial.println("Set");
      lcd.cursorTo(2, 0);                                          //Place cursor line 2 char 0
      lcd.printIn("Set TIME");
      
      //Read momentary switches
      
      //Set, Increment, Decrement
      //do
      //{
      //read increment and decrement momentary buttons to set hour {
        //if increment was pressed, increment by 1, delay 40-60ms, update lcd each cycle
        //if decrement was pressed, decrement by 1, delay 40-60ms, update lcd each cycle
      //}
      //while (setPressed == false);
      
      //delay debounce set time (40-60ms)
      
      //do
      //{
      //read increment and decrement momentary buttons to set min
        //if increment was pressed, increment by 1, delay 40-60ms, update lcd each cycle
        //if decrement was pressed, decrement by 1, delay 40-60ms, update lcd each cycle
      //}
      //while (setPressed == false);
      
      //delay until Set toggle is set to off
      // while (newRead == HIGH) {
        // int newRead = digitalRead(22);
        //
        // if (newRead == LOW) {
        //  break;
        // }
        // else {
        //  delay(50);
        // }
      // }
     
    }
    
    if (reading == HIGH && lastButtonState == LOW) {               
      Serial.println("Set OFF");
    }
    
    //Switch to set the alarm time
    if (reading2 == LOW && lastButtonState2 == HIGH && (lastButtonState != LOW && lastButtonState2 != LOW)) {
      Serial.println("Alarm Set");
    }
    
    if (reading2 == HIGH && lastButtonState2 == LOW) {
      Serial.println("Alarm OFF");
    }
    
    //Switch to enable/disable the alarm
    if (reading3 == LOW && lastButtonState3 == HIGH) {
      Serial.println("Alarm Enabled");
      alarmSet = true;
    }
    else {
      Serial.println("Alarm Disabled"); 
      alarmSet = false;
      lcd.cursorTo(2, 0);                                          //Place cursor line 2 char 0
      lcd.printIn("Alarm OFF");
    }
    
    lastButtonState = reading;                                     //Set press last state
    lastButtonState2 = reading2;                                   //Alarm set last state
    lastButtonState3 = reading3;                                   //Alarm on/off last state
    
    Serial.println("Exiting checkbuttons()");
    
    return;
  }
  
  void loop() {  
    unsigned long msbegin=millis();
    unsigned long msafter;
    digitalWrite(13, HIGH);                         //Light the debug LED
   
    lcd.cursorTo(1, 0);                             //Place cursor line 1 char 0
    lcd.printIn(inctime());                         //Print time
    digitalWrite(13, LOW);                          //Unlight the debug LED
   
    while (alarmSet == true) {                      //Only run if the alarm is on
      lcd.cursorTo(2, 0);                           //Place cursor line 2 char 0
      lcd.printIn("Wakeup at:");
      
      if (ahour == hour && amin == min) {           //Alarm goes off 60s, checking hour & min
                                                    //Vanishing alarm effect
        lcd.cursorTo(2, sec%9);                     //Rest of integer division sec/9
        lcd.printIn(" ");                           //Moving space sign
        
        digitalWrite(3, HIGH);                      //Buzzer on
      }
     
      lcd.cursorTo(2, 11);                            //Place cursor line 2 char 11
      sprintf(time, "%2d:%2d", ahour, amin);          //Print alarm setting time
      lcd.printIn(time);                            
    }
    
    digitalWrite(3, LOW);                           //Buzzer off
    
    msafter = millis();
    
      if (msafter < msbegin) {                      //Millis() reset after 9h due to overflow
       delay(1000);
      }
      else {
        delay(1000-(msafter-msbegin));
      }
      
      checkbuttons();                               //Did you toggle the alarm or set switch?
    
  }

Thanks! :slight_smile:

I got this far:

    int reading = digitalRead(22);                                 //Set time
    int reading2 = digitalRead(26);                                //Alarm time
    int reading3 = digitalRead(30);                                //Alarm on/off
    
    //Switch to set the time
    if (reading == LOW && lastButtonState == HIGH && (lastButtonState != LOW && lastButtonState2 != LOW)) {

Sorry, but reading, reading2, and reading3 are meaningless names. I have no idea what pin 22 is connected to. Presumably a switch of some sort, but what is the purpose of the switch?

The multiple conditions in the if test would be better as nested ifs:

if(reading == LOW && lastButtonState == HIGH)
{
   if(lastbuttonState != LOW && lastButtonState2 != LOW)
   {
      // Do something
   }
}

The same comments about readingn apply to lastButtonStateN. We can assume that the state is applicable to a button, so that part of the name is superfluous. Something like lastSetState would be more meaningful.

One final comment. You got something against ++?

min=min+1;

Real programmers write that as:

min++;

Wow, nooB jus got burned pretty bad...o wait, that's me.

I'll take the hint, bye-bye arduino forums. :-(

Meaningful names make it easier to follow the logic of the program. If you don't want to follow the suggestions given here, that's fine. But, you did ask for a code review.

Real programmers write that as: Code:

min++;

I would disagree. Real programmers write code that they (and other people) find easy to understand, even if it means typing a few extra characters. The compiler won't mind...

Andrew