Hello,
I'm sorry if this has been covered but I couldn't find it.
I modified the 'TimeSerialDateStrings' to include a 16x2 I²C LCD Display.
It seems to display okay for a minute, but then seems to get erroneous data causing it to display incorrect information.
It would start off displaying:
10:38:16 Mon
Apr 08, 2013
but after a minute it would change to something seemingly random like:
21:11:26 Fri
Aug 29, 2104
and it doesn't do this once. I haven't been able to get a time interval of when these errors occur, but if I could get some help fixing them, it would be appreciated. I have also seen it display years that contain 5 digits. That may have been an alignment issue tho.
/*
* TimeSerialDateStrings.pde
* example code illustrating Time library date strings
*
* This sketch adds date string functionality to TimeSerial.pde
*
*/
#include <Time.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(0); //I²C
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
void setup() {
lcd.begin(16,2);lcd.setBacklight(HIGH);
Serial.begin(9600);
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
}
void loop(){
if(Serial.available() )
{
processSyncMessage();
}
if(timeStatus()!= timeNotSet)
{
digitalClockDisplay();
}
//delay(1000); //Disabled because I am not sending time to the Serial Monitor.
//I'm sending it to an LCD and the 1 second delay causes it to apear like it skips 1 second every 11 seconds even tho it really doesn't.
}
void digitalClockDisplay(){
// digital clock display of the time
lcd.setCursor(2,0);
printDigits(hour());
lcd.setCursor(4,0);
lcd.print(":");
lcd.setCursor(5,0);
printDigits(minute());
lcd.setCursor(7,0);
lcd.print(":");
lcd.setCursor(8,0);
printDigits(second());
lcd.print(" ");
lcd.print(dayShortStr(weekday()));
lcd.print(" ");
lcd.setCursor(2,1);
lcd.print(monthShortStr(month()));
lcd.print(" ");
printDigits(day());
lcd.print(", ");
lcd.print(year());
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
//lcd.print(":"); //seemed to be getting in the way, nut I may be wrong
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
lcd.clear();
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
//Serial.print(TIME_REQUEST,BYTE);
//return 0; // the time will be sent later in response to serial mesg
}