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 2013
9 * A Processing example sketch to automatically send the messages is inclided in the download
10 * On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone)
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 while (!Serial) ; // Needed for Leonardo only
22 setSyncProvider( requestSync); //set function to call when sync required
23 Serial.println("Waiting for sync message");
27 if (Serial.available()) {
30 if (timeStatus()!= timeNotSet) {
31 digitalClockDisplay();
33 if (timeStatus() == timeSet) {
34 digitalWrite(13, HIGH); // LED on if synced
36 digitalWrite(13, LOW); // LED off if needs refresh
41 void digitalClockDisplay(){
42 // digital clock display of the time
44 printDigits(minute());
45 printDigits(second());
49 Serial.print(month());
55 void printDigits(int digits){
56 // utility function for digital clock display: prints preceding colon and leading 0
64 void processSyncMessage() {
66 const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
68 if(Serial.find(TIME_HEADER)) {
69 pctime = Serial.parseInt();
70 if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
71 setTime(pctime); // Sync Arduino clock to the time received on the serial port
78 Serial.write(TIME_REQUEST);
79 return 0; // the time will be sent later in response to serial mesg