3 * example code illustrating Time library set through serial port messages.
5 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
6 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2010
9 * A Processing example sketch to automatically send the messages is inclided in the download
14 #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
15 #define TIME_HEADER 'T' // Header tag for serial time sync message
16 #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
20 setSyncProvider( requestSync); //set function to call when sync required
21 Serial.println("Waiting for sync message");
25 if(Serial.available() )
29 if(timeStatus()!= timeNotSet)
31 digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
32 digitalClockDisplay();
37 void digitalClockDisplay(){
38 // digital clock display of the time
40 printDigits(minute());
41 printDigits(second());
45 Serial.print(month());
51 void printDigits(int digits){
52 // utility function for digital clock display: prints preceding colon and leading 0
59 void processSyncMessage() {
60 // if time sync available from serial port, update time and return true
61 while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
62 char c = Serial.read() ;
64 if( c == TIME_HEADER ) {
66 for(int i=0; i < TIME_MSG_LEN -1; i++){
68 if( c >= '0' && c <= '9'){
69 pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
72 setTime(pctime); // Sync Arduino clock to the time received on the serial port
79 Serial.print(TIME_REQUEST,BYTE);
80 return 0; // the time will be sent later in response to serial mesg