Last active
April 19, 2024 14:22
-
-
Save morgz/469ce109363293a0bc429f8ff10a57ee to your computer and use it in GitHub Desktop.
RTC Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
RV-3028-C7 Set Date Time Example | |
Configures date and time to the RTC module via the serial console, and then | |
prints the current date and time in ISO 8601 format every second. | |
Copyright (c) 2020 Macro Yau | |
https://github.com/MacroYau/RV-3028-C7-Arduino-Library | |
*/ | |
#include <RV3028C7.h> | |
RV3028C7 rtc; | |
// TEST | |
// 1. Plug in USB & LIPO Batteru | |
// 2. Run Once. | |
// Output: | |
// 16:15:56.517 -> 2024-04-19T00:00:01 | |
// 16:15:57.516 -> 2024-04-19T00:00:02 | |
// 16:15:58.510 -> 2024-04-19T00:00:03 | |
// 16:15:59.505 -> 2024-04-19T00:00:04 | |
// 16:16:00.514 -> 2024-04-19T00:00:05 | |
// 16:16:01.512 -> 2024-04-19T00:00:06 | |
// 16:16:02.506 -> 2024-04-19T00:00:07 | |
// 16:16:03.530 -> 2024-04-19T00:00:08 | |
// 16:16:04.530 -> 2024-04-19T00:00:09 | |
// 16:16:05.523 -> 2024-04-19T00:00:10 | |
// 16:16:06.525 -> 2024-04-19T00:00:11 | |
// 16:16:07.509 -> 2024-04-19T00:00:12 | |
// 16:16:08.514 -> 2024-04-19T00:00:13 | |
// 16:16:09.514 -> 2024-04-19T00:00:14 | |
// 16:16:10.545 -> 2024-04-19T00:00:15 | |
// 16:16:11.534 -> 2024-04-19T00:00:16 | |
// 3. Just disconnect & reconnect USB | |
// Time is kept. | |
// 16:17:07.584 -> 2024-04-19T00:01:12 | |
// 16:17:08.604 -> 2024-04-19T00:01:13 | |
// 16:17:09.595 -> 2024-04-19T00:01:14 | |
// 16:17:10.583 -> 2024-04-19T00:01:15 | |
// 16:17:11.602 -> 2024-04-19T00:01:16 | |
// 3. Just disconnect LIPO | |
// Time is kept | |
// 16:18:45.206 -> 2024-04-19T00:02:49 | |
// 16:18:46.194 -> 2024-04-19T00:02:50 | |
// 16:18:47.216 -> 2024-04-19T00:02:51 | |
// 16:18:48.207 -> 2024-04-19T00:02:52 | |
// 16:18:49.228 -> 2024-04-19T00:02:53 | |
// 16:18:50.218 -> 2024-04-19T00:02:54 | |
// 3. Reconnect LIPO | |
// 4. Disconnect LIPO & USB | |
// 5. Reconnect LIPO followed by USB | |
// Time is reset | |
// I think this proves the little battery is not working in conjunction with the RTC to remember time. | |
// 16:20:34.022 -> 2024-04-19T00:00:05 | |
// 16:20:35.036 -> 2024-04-19T00:00:06 | |
// 16:20:36.039 -> 2024-04-19T00:00:07 | |
// 16:20:37.031 -> 2024-04-19T00:00:08 | |
// 16:20:38.020 -> 2024-04-19T00:00:09 | |
void setup() { | |
Serial.begin(115200); | |
while(!Serial); | |
delay(1000); | |
Serial.println("RV-3028-C7 Set Date Time Example"); | |
Serial.println(); | |
Wire.begin(18, 17); // Initialize I2C communication with SDA on GPIO 18 and SCL on GPIO 17 | |
while (rtc.begin() == false) { | |
Serial.println("Failed to detect RV-3028-C7!"); | |
delay(5000); | |
} | |
if (isRTCUnset() == true) { | |
Serial.println("------"); | |
Serial.println("RTC Does NOT have time set"); | |
Serial.println("------"); | |
rtc.setDateTimeFromISO8601("2024-04-19T00:00:00"); | |
rtc.synchronize(); // Writes the new date time to RTC | |
} | |
else { | |
Serial.println("------"); | |
Serial.println("RTC HAS time set"); | |
Serial.println("------"); | |
} | |
Serial.println(); | |
} | |
void loop() { | |
Serial.println(rtc.getCurrentDateTime()); | |
delay(1000); | |
} | |
bool isRTCUnset() { | |
// Get the current time from the RTC | |
uint32_t currentTimestamp = getUnixTimestamp(); | |
// Calculate the Unix timestamp for the beginning of the year 2024 | |
uint32_t beginningOf2024 = 1706655600; // January 1, 2024 | |
// Check if the current time is before the beginning of the year 2024 | |
if (currentTimestamp < beginningOf2024) { | |
return true; // RTC is unset | |
} else { | |
return false; // RTC is set | |
} | |
} | |
uint32_t getUnixTimestamp() { | |
return rtc.getUnixTimestamp(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment