naivxnaivet

Real Time Clock (DS3231 RTC)

Nov 8th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include<Wire.h>
  2. #include "RTClib.h"
  3. RTC_DS3231 rtc;
  4.  
  5.  
  6. unsigned long previousMillis = 0; // will store last time LED was updated
  7. const long interval = 1000;
  8.  
  9. void setup() {
  10. Serial.begin(9600);
  11. // put your setup code here, to run once:
  12. if (! rtc.begin()) {
  13. Serial.println(F("Couldn't find RTC"));
  14. while (1);
  15. }
  16. if (rtc.lostPower()) {
  17. Serial.println(F("RTC lost power, lets set the time!"));
  18. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  19. }
  20.  
  21. }
  22.  
  23. void loop()
  24. {
  25. // put your main code here, to run repeatedly:
  26. DateTime now = rtc.now();
  27.  
  28. unsigned long currentMillis = millis();
  29. if (currentMillis - previousMillis >= interval)
  30. {
  31. previousMillis = currentMillis;
  32. Serial.print(now.year(), DEC);
  33. Serial.print( "/" );
  34. Serial.print(now.month(), DEC);
  35. Serial.print( "/" );
  36. Serial.print(now.day(), DEC),
  37. Serial.print( " " );
  38. Serial.print(now.hour(), DEC);
  39. Serial.print( ":" );
  40. Serial.print(now.minute(), DEC);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment