#include "RTClib.h"
#include "time.h"
String startStr = "23:59";
String stopStr = "00:00";
RTC_DS1307 rtc;
bool timerState;
bool oldTimerState = !timerState;
void setup () {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
}
rtc.adjust(DateTime(2022, 8, 24, 23, 58, 50));
}
void loop () {
DateTime now = rtc.now();
uint32_t epochTime = now.unixtime(); // epoch time given from internal RTC (seconds since 1970....)
uint32_t startOfDay = epochTime - (now.hour() * 3600UL) - (now.minute() * 60UL) - now.second();
uint32_t startTime = startStr.substring(0, 2).toInt() * 3600UL + startStr.substring(3).toInt() * 60UL;
uint32_t stopTime = stopStr.substring(0, 2).toInt() * 3600UL + stopStr.substring(3).toInt() * 60UL;
if ((startOfDay + startTime == epochTime) && !timerState) {
timerState = true;
}
if ((startOfDay + stopTime == epochTime) && timerState) {
timerState = false;
}
if (timerState != oldTimerState) {
oldTimerState = timerState;
char buffer[25];
snprintf(buffer, sizeof(buffer),
"%02d/%02d/%04d %02d:%02d:%02d - ",
now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second()
);
Serial.print(buffer);
digitalWrite(LED_BUILTIN, timerState);
timerState ? Serial.println("Timer ON") : Serial.println("Timer OFF");
}
delay(10);
}