#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc; // <-- use RTC_DS1307 for DS1307 module
const int LDR_Pin = A0;
const int Relay_Pin = 7;
const int threshold = 500; // tune this
void setup() {
Serial.begin(9600);
pinMode(LDR_Pin, INPUT);
pinMode(Relay_Pin, OUTPUT);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// If RTC has lost power, set the time once.
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, setting time!");
// Set to compile time — run once, then comment out the next line
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
int lightValue = analogRead(LDR_Pin);
Serial.print("Hour: "); Serial.print(hour);
Serial.print(" | LDR: "); Serial.println(lightValue);
// The hour should be either above 18 or below 6.
if (hour >= 18 || hour < 6)
{
if (lightValue < threshold)
{
digitalWrite(Relay_Pin, HIGH);
} else {
digitalWrite(Relay_Pin, LOW);
}
}
delay(1000);
}