From a9f9de4fca234ecfcc205009f4b21f46f97f0dbd Mon Sep 17 00:00:00 2001 From: Mohamed Shahil <152691954+mohamedshahilshajahan@users.noreply.github.com> Date: Tue, 19 Dec 2023 18:05:05 +0530 Subject: [PATCH] Add files via upload --- DS1307.ino | 46 ++++++++++++++++++++++++++++++++++++++++++++++ DS3231.ino | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 DS1307.ino create mode 100644 DS3231.ino diff --git a/DS1307.ino b/DS1307.ino new file mode 100644 index 0000000..3f04c87 --- /dev/null +++ b/DS1307.ino @@ -0,0 +1,46 @@ +#include +#include + +RTC_DS1307 rtc; + +const int relay = A3; +bool on = LOW; +bool off = HIGH; + +void setup() { + Serial.begin(9600); + + if (!rtc.begin()) { + Serial.println("Couldn't find RTC"); + while (1); + } + + if (!rtc.isrunning()) { + Serial.println("RTC is not running, setting the time!"); + rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); + } + + pinMode(relay, OUTPUT); + digitalWrite(relay, off); +} + +void loop() { + DateTime now = rtc.now(); + int currentHour = now.hour(); + int currentMinute = now.minute(); + int currentSecond = now.second(); + + Serial.print("Current time: "); + Serial.println(now.timestamp()); + + if (currentHour == 17 && currentMinute == 40 && currentSecond == 0) { + digitalWrite(relay, on); + delay(1000); // Delay to ensure the relay is toggled only once + } + if (currentHour == 6 && currentMinute == 0 && currentSecond == 0) { + digitalWrite(relay, off); + delay(1000); // Delay to ensure the relay is toggled only once + } + + delay(1000); +} diff --git a/DS3231.ino b/DS3231.ino new file mode 100644 index 0000000..3414e6e --- /dev/null +++ b/DS3231.ino @@ -0,0 +1,46 @@ +#include +#include + +RTC_DS3231 rtc; + +const int relay = A3; +bool on = LOW; +bool off = HIGH; + +void setup() { + Serial.begin(9600); + + if (!rtc.begin()) { + Serial.println("Couldn't find RTC"); + while (1); + } + + if (rtc.lostPower()) { + Serial.println("RTC lost power, setting the time!"); + rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); + } + + pinMode(relay, OUTPUT); + digitalWrite(relay, off); +} + +void loop() { + DateTime now = rtc.now(); + int currentHour = now.hour(); + int currentMinute = now.minute(); + int currentSecond = now.second(); + + Serial.print("Current time: "); + Serial.println(now.timestamp()); + + if (currentHour == 17 && currentMinute == 40 && currentSecond == 0) { + digitalWrite(relay, on); + delay(1000); // Delay to ensure the relay is toggled only once + } + if (currentHour == 6 && currentMinute == 0 && currentSecond == 0) { + digitalWrite(relay, off); + delay(1000); // Delay to ensure the relay is toggled only once + } + + delay(1000); +}