3 * example code illustrating Time library with Real Time Clock.
5 * RTC clock is set in response to serial port time message
6 * A Processing example sketch to set the time is included in the download
7 * On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone)
12 #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
17 while (!Serial) ; // Needed for Leonardo only
18 setSyncProvider(RTC.get); // the function to get the time from the RTC
19 if (timeStatus() != timeSet)
20 Serial.println("Unable to sync with the RTC");
22 Serial.println("RTC has set the system time");
27 if (Serial.available()) {
28 time_t t = processSyncMessage();
30 RTC.set(t); // set the RTC and the system time to the received value
34 digitalClockDisplay();
38 void digitalClockDisplay(){
39 // digital clock display of the time
41 printDigits(minute());
42 printDigits(second());
46 Serial.print(month());
52 void printDigits(int digits){
53 // utility function for digital clock display: prints preceding colon and leading 0
60 /* code to process time sync messages from the serial port */
61 #define TIME_HEADER "T" // Header tag for serial time sync message
63 unsigned long processSyncMessage() {
64 unsigned long pctime = 0L;
65 const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
67 if(Serial.find(TIME_HEADER)) {
68 pctime = Serial.parseInt();
70 if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
71 pctime = 0L; // return 0 to indicate that the time is not valid