Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 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. #if defined(ARDUINO_ARCH_SAMD)
  6. // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
  7. #define Serial SerialUSB
  8. #endif
  9.  
  10. RTC_DS1307 rtc;
  11.  
  12. char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  13.  
  14. void setup () {
  15.  
  16. #ifndef ESP8266
  17. while (!Serial); // for Leonardo/Micro/Zero
  18. #endif
  19.  
  20. Serial.begin(57600);
  21. Wire.begin(D3, D4);
  22. if (! rtc.begin()) {
  23. Serial.println("Couldn't find RTC");
  24. while (1);
  25. }
  26.  
  27. if (! rtc.isrunning()) {
  28. Serial.println("RTC is NOT running!");
  29. // following line sets the RTC to the date & time this sketch was compiled
  30. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  31. // This line sets the RTC with an explicit date & time, for example to set
  32. // January 21, 2014 at 3am you would call:
  33. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  34. }
  35. }
  36.  
  37. void loop () {
  38. DateTime now = rtc.now();
  39.  
  40. Serial.print(now.year(), DEC);
  41. Serial.print('/');
  42. Serial.print(now.month(), DEC);
  43. Serial.print('/');
  44. Serial.print(now.day(), DEC);
  45. Serial.print(" (");
  46. Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  47. Serial.print(") ");
  48. Serial.print(now.hour(), DEC);
  49. Serial.print(':');
  50. Serial.print(now.minute(), DEC);
  51. Serial.print(':');
  52. Serial.print(now.second(), DEC);
  53. Serial.println();
  54.  
  55. Serial.print(" since midnight 1/1/1970 = ");
  56. Serial.print(now.unixtime());
  57. Serial.print("s = ");
  58. Serial.print(now.unixtime() / 86400L);
  59. Serial.println("d");
  60.  
  61. // calculate a date which is 7 days and 30 seconds into the future
  62. DateTime future (now + TimeSpan(7,12,30,6));
  63.  
  64. Serial.print(" now + 7d + 30s: ");
  65. Serial.print(future.year(), DEC);
  66. Serial.print('/');
  67. Serial.print(future.month(), DEC);
  68. Serial.print('/');
  69. Serial.print(future.day(), DEC);
  70. Serial.print(' ');
  71. Serial.print(future.hour(), DEC);
  72. Serial.print(':');
  73. Serial.print(future.minute(), DEC);
  74. Serial.print(':');
  75. Serial.print(future.second(), DEC);
  76. Serial.println();
  77.  
  78. Serial.println();
  79. delay(3000);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement