TimeRTC.ino 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * TimeRTC.pde
  3. * example code illustrating Time library with Real Time Clock.
  4. *
  5. */
  6. #include <TimeLib.h>
  7. #include <Wire.h>
  8. #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
  9. void setup() {
  10. Serial.begin(9600);
  11. while (!Serial) ; // wait until Arduino Serial Monitor opens
  12. setSyncProvider(RTC.get); // the function to get the time from the RTC
  13. if(timeStatus()!= timeSet)
  14. Serial.println("Unable to sync with the RTC");
  15. else
  16. Serial.println("RTC has set the system time");
  17. }
  18. void loop()
  19. {
  20. if (timeStatus() == timeSet) {
  21. digitalClockDisplay();
  22. } else {
  23. Serial.println("The time has not been set. Please run the Time");
  24. Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");
  25. Serial.println();
  26. delay(4000);
  27. }
  28. delay(1000);
  29. }
  30. void digitalClockDisplay(){
  31. // digital clock display of the time
  32. Serial.print(hour());
  33. printDigits(minute());
  34. printDigits(second());
  35. Serial.print(" ");
  36. Serial.print(day());
  37. Serial.print(" ");
  38. Serial.print(month());
  39. Serial.print(" ");
  40. Serial.print(year());
  41. Serial.println();
  42. }
  43. void printDigits(int digits){
  44. // utility function for digital clock display: prints preceding colon and leading 0
  45. Serial.print(":");
  46. if(digits < 10)
  47. Serial.print('0');
  48. Serial.print(digits);
  49. }