alarm clock

hi,

I am new to arduino and wanted to make an alarm clock with arduino duemilanove. I have read through some examples and posts in the forum. I found out that I just need to change the serial.print to lcd.print in datetime example. I tried and it's not working. May I know why? I connected my LCD according to LiquidCrystal example in arduino website

#include <DateTime.h>
#include <DateTimeStrings.h>
#include <LiquidCrystal.h>

#define TIME_MSG_LEN 11 // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER 255 // Header tag for serial time sync message

const int numRows = 2;
const int numCols = 16;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
Serial.begin(19200);
}

void loop(){
getPCtime(); // try to get time sync from pc
if(DateTime.available()) { // update clocks if time has been synced
unsigned long prevtime = DateTime.now();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;
DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock

// send our time to an app listening on the serial port
lcd.print( TIME_HEADER,BYTE); // this is the header for the current time
lcd.println(DateTime.now());
}
}

void getPCtime() {
// if time available from serial port, sync the DateTime library
while(Serial.available() >= TIME_MSG_LEN ){ // time message
if( Serial.read() == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
char c= Serial.read();
if( c >= '0' && c <= '9')
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
DateTime.sync(pctime); // Sync DateTime clock to the time received on the serial port
}
}
}

void digitalClockDisplay(){
// digital clock display of current time
lcd.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);

}

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

As a starter, if you use the example 'Helloworld' in the LiquidCrystal Library, does that work ?.

Lets us know if you have a working LCD or not.

/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the 
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 25 July 2009
 by David A. Mellis
 
 
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
}

You need to set the time for the display to start:
Change
#define TIME_HEADER 255 // Header tag for serial time sync message
To
#define TIME_HEADER 'T' // Header tag for serial time sync message

And use serial monitor to send the following time value:

T1254139200

This should start the clock at midday today

Here is an LCD clock I did a while ago that may give you some ideas on how to change the time with buttons

#include <LiquidCrystal.h>
#include <DateTime.h>

#define DEBOUNCE_TIME  40 // switch must be pressed at least this number of ms
#define CLOCK_ROW  0
#define CLOCK_COL  0
#define NO_SWITCH  255  

#define btnSET 14
#define btnINC 15
#define btnDEC 16

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

void blinkingCursor(boolean enable){
  if(enable)
    lcd.command(0xf);  // blink cursor
  else   
    lcd.command(0xc); // just enable the display
}

void setup(){
  Serial.begin(9600);
  pinMode(btnSET ,INPUT);
  pinMode(btnINC ,INPUT);
  pinMode(btnDEC ,INPUT);
  digitalWrite(btnSET, HIGH); // turn pull-ups on
  digitalWrite(btnINC, HIGH);
  digitalWrite(btnDEC, HIGH);
  lcd.begin(16,2);
  lcd.print("Press Set btn to set clock");
}

void loop(){
  if(DateTime.available()) { // update clocks if time has been synced
    digitalClockDisplay( );   // update digital clock
  }
  if(swPressed(btnSET)){
    if(DateTime.available()== false)
      DateTime.sync(1230768000); // if clock was never set,  start at Jan 1 2009
    DateTime.available();
    setTime();  
  }
  delay(100);
}

void digitalClockDisplay(){
  // digital clock display of current date and time
  lcd.setCursor( CLOCK_COL, CLOCK_ROW);  lcd.clear();   
  if(DateTime.Hour < 10)
    lcd.print(' ');
  lcd.print(DateTime.Hour,DEC);
  printDigits(DateTime.Minute);
  printDigits(DateTime.Second);
}

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

void setTime(){
  byte  second, minute, hour;

  digitalClockDisplay();
  if(swPressed(btnSET)) {
    lcd.setCursor( CLOCK_COL, CLOCK_ROW);
    blinkingCursor(true) ; // flash cursor
    while(swPressed(btnSET)) //wait for SET button to be released
      ;
  }    
  hour   = getValue(DateTime.Hour,0, 23, CLOCK_COL, CLOCK_ROW);  
  minute = getValue(DateTime.Minute,0, 59, CLOCK_COL+3, CLOCK_ROW);  
  second = getValue(DateTime.Second,0, 59, CLOCK_COL+6, CLOCK_ROW);  
  DateTime.sync(DateTime.makeTime(second, minute, hour, DateTime.Day, DateTime.Month, DateTime.Year));  //sets the time
  blinkingCursor(false) ; 
}


unsigned int swPressed(byte sw){
  // returns duration button pressed in ms, returns 0 if not pressed for DEBOUNCE period 
  // this logic only supports one button pressed at a time
  static byte prevswitch = NO_SWITCH;
  static unsigned long startTime;
  unsigned int dur;

  dur = 0;
  if(digitalRead(sw) == LOW){
    if(sw != prevswitch ){  
      startTime = millis();
      prevswitch = sw;     
      delay(DEBOUNCE_TIME); 
      if(digitalRead(sw) == LOW)
        dur = DEBOUNCE_TIME;
    }
    else if (sw == prevswitch ){
      Serial.println(millis() - startTime);
      if( millis() - startTime > 60000)
        dur = 60000; // this prevents overflowing return value
      else{
        dur = millis() - startTime;     
        if( dur < DEBOUNCE_TIME)
          dur = 0;  // return 0 if duration less than debounce time
      }
    }  
  }
  else if (sw == prevswitch ){  // was previous switch released?  
    prevswitch = NO_SWITCH;  // yes, so clear switch indicator
  } 
  return dur;
}

byte getValue(byte value, byte min, byte max,byte col, byte row){  
  //returns a value from min to max based on INC and DEC button presses 
  // displays the value at the given column and row

    //  lcd.setCursor(col,row); 
  //  lcd.print("--"); // print underline below selected digits
  //  lcd.command(0x4); // don't advance cursor
  while(1)   {
    int adj;
    lcd.setCursor(col,row); 
    if( value < 10)
      lcd.print('0');    
    lcd.print(value,DEC); //Print the current value            
    lcd.setCursor(col+1,row); 
    if(adj > 1000)
      delay(100);
    else
      delay(500);  

    adj = swPressed(btnINC);      
    if(adj){ 
      if(value >= max)
        value = min;
      else   
        value++;  
    }
    else{
      adj = swPressed(btnDEC);
      if(adj) 
        if(value == min)
          value = max;
        else   
          value--;        
    }
    if(swPressed(btnSET)){
      while(swPressed(btnSET))
        ; // wait for SET button to be released
      return value;   
    }
  }   
}

hi,

pluggy, i tried and it works.

mem, i tried and it printed the time and date in the serial monitor. i did the same way in my code, my lcd printed out the time. But, what is time_header and why i need to send a value to it? what does the value means?

the information that is sent is a standard way of encoding date and time, sometimes called unix time (see Unix time - Wikipedia)

This time format is supported by most operating systems and programming languages. The download for the DateTime library includes a Processing sketch that gets the time from your computer and sends this to the Arduino sketch.

The header is there to provide a way for the sketch to recognize the start of the time packet.

If you don't care about setting the clock from your computer you can simply use the button code to set the clock.

I believe

 lcd.begin(16, 2);

is needed in setup with later versions of the LiquidCrystal library. Mem's example and the Hello World example have it but yours doesn't - Is this the problem ?

thanks everyone,

i can display the time with my LCD provided i sent the unix time in serial monitor.

mem, is there a way which i can synchronize my pc time automatically, without using button? i have the processing sketch in datetime folder, do i need to compile it?

another question, have anyone of you try to connect you board with external power source? i connected a 9V battery to my board, but after i unplug the usb cable, my device doesnt work according to what it is programmed.

The processing sketch waits for a mouse click before sending the sync message. You can modify it if you want to send an update at regular intervals. See www.Processing.org if you have not used Processing before.

What board do you have, a Duemilanove should automatically switch power source.

i using arduino mega for control servos, which having problem with the power source. i using duemilanove for displaying time on LCD.

Both boards should switch power source automatically. Are you sure your 9v battery is OK

the external source problem is solved. it just take longer time for loading. but, i still stuck at my alarm. any suggestion on real time clock?

Do you want help with adding alarm functionality into the code posted above or are you looking to use an external real time clock?

i am looking for external real time clock. i think i can handle alarm code. Still, i couldnt synchronize my pc time with my board.

If you changed the header from 255 to the character 'T' then you need to change it back to 255 to use with the Processing sketch.

A search for Arduino real time clock (or rtc) should turn up plenty of suggestions