Here is the **full, updated code** that initializes the DS1307 RTC to start from `2025-07-05 00:00:00` **only once** (e.g., on first power-up or when RTC is not running):
```cpp
#include <RTClib.h> // For RTC_DS1307 and DateTime
#include "my_lcd.h" // Your custom LCD library
MyLCD lcd;
RTC_DS1307 rtc;
void setup() {
Serial.begin(115200);
lcd.begin();
if (!rtc.begin()) {
Serial.println(F("Error: RTC not found!"));
while (1); // Halt if RTC fails
}
if (!rtc.isrunning()) {
// Only set RTC time if it is not running
rtc.adjust(DateTime(2025, 7, 5, 0, 0, 0));
Serial.println(F("RTC initialized to 2025-07-05 00:00:00"));
}
}
void loop() {
lcd.display("ESP32-DevKitC-v4", "Hello, Zi Ying!", "You're lowk cute icl!");
delay(5000);
lcd.clear();
DateTime now = rtc.now();
char dtbuf[21] = "YYYY-MM-DD hh:mm:ss";
now.toString(dtbuf); // Format time into dtbuf
lcd.datetime(dtbuf);
lcd.alarm(1, "Temp UL1:", "40.6", 0);
delay(5000);
lcd.alarm(2, "Curr UL2:", "60.0", "A");
delay(5000);
}LCD (20x4) (I2C)
DS1307 RTC