//Make just run one time one day
#include <Wire.h>
#include <RTClib.h>
#include <Preferences.h>
RTC_DS1307 rtc;
Preferences prefs;
void checkDailyRun() {
DateTime now = rtc.now();
int lastRunDay = prefs.getInt("lastRunDay", -1);
Serial.println(lastRunDay);
if (now.day() != lastRunDay && now.hour() == 13) {
runDailyFunction(now);
prefs.putInt("lastRunDay", now.day());
}
}
void runDailyFunction(DateTime now) {
Serial.printf("Action Run FUNCTION : %02d:%02d NGÀY: %02d/%02d/%04d\n",
now.hour(), now.minute(), now.day(), now.month(), now.year());
// Code run more
}
void setup() {
Serial.begin(115200);
Wire.begin();
if (!rtc.begin()) {
Serial.println("RTC don't have");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC no run, set time default");
rtc.adjust(DateTime(2024, 1, 1, 0, 0, 0));
}
prefs.begin("daily-task", false);
//check first
checkDailyRun();
}
void loop() {
//check 60s after
static unsigned long lastCheck = 0;
if (millis() - lastCheck > 60000) {
lastCheck = millis();
checkDailyRun();
}
}