]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Time/Examples/TimeRTC/TimeRTC.pde
first commit
[arduino] / books / pdummies / Libraries / Time / Examples / TimeRTC / TimeRTC.pde
1 /*
2  * TimeRTC.pde
3  * example code illustrating Time library with Real Time Clock.
4  * 
5  */
6
7 #include <Time.h>  
8 #include <Wire.h>  
9 #include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
10
11 void setup()  {
12   Serial.begin(9600);
13   setSyncProvider(RTC.get);   // the function to get the time from the RTC
14   if(timeStatus()!= timeSet) 
15      Serial.println("Unable to sync with the RTC");
16   else
17      Serial.println("RTC has set the system time");      
18 }
19
20 void loop()
21 {
22    digitalClockDisplay();  
23    delay(1000);
24 }
25
26 void digitalClockDisplay(){
27   // digital clock display of the time
28   Serial.print(hour());
29   printDigits(minute());
30   printDigits(second());
31   Serial.print(" ");
32   Serial.print(day());
33   Serial.print(" ");
34   Serial.print(month());
35   Serial.print(" ");
36   Serial.print(year()); 
37   Serial.println(); 
38 }
39
40 void printDigits(int digits){
41   // utility function for digital clock display: prints preceding colon and leading 0
42   Serial.print(":");
43   if(digits < 10)
44     Serial.print('0');
45   Serial.print(digits);
46 }
47 \r