#include <Wire.h>
#include <RTClib.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include "time.h"
#define SDA_PIN 21 // Replace with the actual SDA pin
#define SCL_PIN 22 // Replace with the actual SCL pin
#define RelayPin 18
RTC_DS3231 rtc;
void setup () {
Serial.begin(115200);
Serial.println("Schedule is Started ...");
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);
Wire.begin(SDA_PIN, SCL_PIN);
// Check if the RTC is connected properly
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
Serial.println("RTC is mounted");
}
// Check if the RTC lost power and if so, set the time
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
else {
Serial.println("RTC No Lost Power");
}
//Set time first
//setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
DateTime rtcTime = rtc.now();
setTime(rtcTime.hour(), rtcTime.minute(), rtcTime.second(),rtcTime.month() , rtcTime.day(), rtcTime.year()-2000);
// this area for setting alarm
Alarm.alarmRepeat(22, 23, 0, DeviceLed);
Alarm.timerRepeat(5, Repeats);
}
void loop () {
displayTime(); // Display time every second
delay(1000);
}
void displayTime() {
DateTime now = rtc.now();
Serial.printf("%02d:%02d:%02d-%02d:%02d:%2d\n", now.hour(), now.minute(), now.second(),now.day(),now.month(),now.year()-2000);
}
void DeviceLed() {
digitalWrite(RelayPin, HIGH);
delay(3000);
digitalWrite(RelayPin, LOW);
}
void Repeats(){
Serial.println("15 second timer");
//digitalWrite(relayPin, LOW);
}