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 inclided in the download
11 #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
16 setSyncProvider(RTC.get); // the function to get the time from the RTC
17 if(timeStatus()!= timeSet)
18 Serial.println("Unable to sync with the RTC");
20 Serial.println("RTC has set the system time");
25 if(Serial.available())
27 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_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
62 #define TIME_HEADER 'T' // Header tag for serial time sync message
64 time_t processSyncMessage() {
65 // return the time if a valid sync message is received on the serial port.
66 while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
67 char c = Serial.read() ;
69 if( c == TIME_HEADER ) {
71 for(int i=0; i < TIME_MSG_LEN -1; i++){
73 if( c >= '0' && c <= '9'){
74 pctime = (10 * pctime) + (c - '0') ; // convert digits to a number