@@ -37,36 +37,52 @@ 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);
52
-
49
+ RTCTime mytime(6, Month::NOVEMBER, 2023, 18, 12, 00, DayOfWeek::MONDAY, SaveLight::SAVING_TIME_ACTIVE);
50
+
51
+ RTCTime savedTime;
52
+ RTC.getTime(savedTime);
53
+
54
+ if (!RTC.isRunning()) {
55
+ // this means the RTC is waking up "as new"
56
+ if (savedTime.getYear() == 2000) {
57
+ RTC.setTime(mytime);
58
+ } else {
59
+ RTC.setTime(savedTime);
60
+ }
61
+ }
53
62
}
54
63
55
64
void loop() {
56
- // put your main code here, to run repeatedly:
57
-
65
+
58
66
RTCTime currenttime;
59
67
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
-
68
+ Serial.print("Current time: ");
69
+
70
+ /* DATE */
71
+ Serial.print(currenttime.getDayOfMonth());
72
+ Serial.print("/");
73
+ Serial.print(Month2int(currenttime.getMonth()));
74
+ Serial.print("/");
75
+ Serial.print(currenttime.getYear());
76
+ Serial.print(" - ");
77
+
78
+ /* HOURS:MINUTES:SECONDS */
79
+ Serial.print(currenttime.getHour());
80
+ Serial.print(":");
81
+ Serial.print(currenttime.getMinutes());
82
+ Serial.print(":");
83
+ Serial.println(currenttime.getSeconds());
84
+
85
+ delay(1000);
70
86
}
71
87
72
88
```
0 commit comments