@@ -37,36 +37,55 @@ On the header that is located by the barrel jack, you'll find the VRTC pin. And
37
37
38
38
![ Battery Pack Powering the UNO R4 WiFi RTC] ( ./assets/Circuit.png )
39
39
40
- The following sketch will start the RTC but only set the time if it is not already running .
40
+ The following sketch will start the RTC but only set the time if it this is the first time starting the board since adding the VRTC battery .
41
41
42
42
``` arduino
43
43
#include "RTC.h"
44
44
45
45
void setup() {
46
- // put your setup code here, to run once:
47
46
Serial.begin(9600);
48
47
RTC.begin();
49
- RTCTime mytime(24, Month::MAY, 2023, 11, 8, 0, DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);
50
48
51
- RTC.setTimeIfNotRunning(mytime);
49
+ // A fallback time object, for setting the time if there is no time to retrieve from the RTC.
50
+ RTCTime mytime(6, Month::NOVEMBER, 2023, 18, 12, 00, DayOfWeek::MONDAY, SaveLight::SAVING_TIME_ACTIVE);
51
+
52
+ // Tries to retrieve time
53
+ RTCTime savedTime;
54
+ RTC.getTime(savedTime);
55
+
52
56
57
+ if (!RTC.isRunning()) {
58
+ // this means the RTC is waking up "as new"
59
+ if (savedTime.getYear() == 2000) {
60
+ RTC.setTime(mytime);
61
+ } else {
62
+ RTC.setTime(savedTime);
63
+ }
64
+ }
53
65
}
54
66
55
67
void loop() {
56
- // put your main code here, to run repeatedly:
57
-
68
+
58
69
RTCTime currenttime;
59
70
RTC.getTime(currenttime);
60
-
61
- int hours = currenttime.getHour();
62
- int minutes = currenttime.getMinutes();
63
-
64
-
65
- Serial.print("Hours: ");
66
- Serial.println(hours);
67
- Serial.println("Minutes: ");
68
- Serial.println(minutes);
69
-
71
+ Serial.print("Current time: ");
72
+
73
+ /* DATE */
74
+ Serial.print(currenttime.getDayOfMonth());
75
+ Serial.print("/");
76
+ Serial.print(Month2int(currenttime.getMonth()));
77
+ Serial.print("/");
78
+ Serial.print(currenttime.getYear());
79
+ Serial.print(" - ");
80
+
81
+ /* HOURS:MINUTES:SECONDS */
82
+ Serial.print(currenttime.getHour());
83
+ Serial.print(":");
84
+ Serial.print(currenttime.getMinutes());
85
+ Serial.print(":");
86
+ Serial.println(currenttime.getSeconds());
87
+
88
+ delay(1000);
70
89
}
71
90
72
91
```
0 commit comments