@@ -37,36 +37,55 @@ On the header that is located by the barrel jack, you'll find the VRTC pin. And
3737
3838![ Battery Pack Powering the UNO R4 WiFi RTC] ( ./assets/Circuit.png )
3939
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 .
4141
4242``` arduino
4343#include "RTC.h"
4444
4545void setup() {
46- // put your setup code here, to run once:
4746 Serial.begin(9600);
4847 RTC.begin();
49- RTCTime mytime(24, Month::MAY, 2023, 11, 8, 0, DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);
5048
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+
5256
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+ }
5365}
5466
5567void loop() {
56- // put your main code here, to run repeatedly:
57-
68+
5869 RTCTime currenttime;
5970 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);
7089}
7190
7291```
0 commit comments