Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
  2. #include <Wire.h>
  3. #include "RTClib.h"
  4.  
  5. RTC_PCF8523 rtc;
  6.  
  7. char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  8.  
  9. void setup () {
  10.  
  11. while (!Serial) {
  12. delay(1); // for Leonardo/Micro/Zero
  13. }
  14.  
  15. Serial.begin(57600);
  16. if (! rtc.begin()) {
  17. Serial.println("Couldn't find RTC");
  18. while (1);
  19. }
  20.  
  21. if (! rtc.initialized()) {
  22. Serial.println("RTC is NOT running!");
  23. // following line sets the RTC to the date & time this sketch was compiled
  24. // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  25. // This line sets the RTC with an explicit date & time, for example to set
  26. // January 21, 2014 at 3am you would call:
  27. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  28. }
  29. }
  30.  
  31. void loop () {
  32. DateTime now = rtc.now();
  33.  
  34. Serial.print(now.year(), DEC);
  35. Serial.print('/');
  36. Serial.print(now.month(), DEC);
  37. Serial.print('/');
  38. Serial.print(now.day(), DEC);
  39. Serial.print(" (");
  40. Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  41. Serial.print(") ");
  42. Serial.print(now.hour(), DEC);
  43. Serial.print(':');
  44. Serial.print(now.minute(), DEC);
  45. Serial.print(':');
  46. Serial.print(now.second(), DEC);
  47. Serial.println();
  48.  
  49. Serial.print(" since midnight 1/1/1970 = ");
  50. Serial.print(now.unixtime());
  51. Serial.print("s = ");
  52. Serial.print(now.unixtime() / 86400L);
  53. Serial.println("d");
  54.  
  55. // calculate a date which is 7 days, 12 hours and 30 seconds into the future
  56. DateTime future (now + TimeSpan(7,12,30,6));
  57.  
  58. Serial.print(" now + 7d + 30s: ");
  59. Serial.print(future.year(), DEC);
  60. Serial.print('/');
  61. Serial.print(future.month(), DEC);
  62. Serial.print('/');
  63. Serial.print(future.day(), DEC);
  64. Serial.print(' ');
  65. Serial.print(future.hour(), DEC);
  66. Serial.print(':');
  67. Serial.print(future.minute(), DEC);
  68. Serial.print(':');
  69. Serial.print(future.second(), DEC);
  70. Serial.println();
  71.  
  72. Serial.println();
  73. delay(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement