#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
const int pirPin = 2; // PIR sensor pin
const int ledPin = 13; // LED pin
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure LED is initially off
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Handmatig instellen van de tijd naar 21:00 uur
rtc.adjust(DateTime(2024, 3, 22, 21, 0, 0)); // Jaar, maand, dag, uur, minuut, seconde
}
void loop() {
int pirValue = digitalRead(pirPin);
// Get current time from RTC
DateTime now = rtc.now();
// Check if motion is detected and if it's between 20:00 and 05:00
if (pirValue == HIGH && isNightTime(now.hour())) {
digitalWrite(ledPin, HIGH); // Turn on LED
Serial.println("Motion detected!");
delay(100000); // Wait for 1 second
} else {
digitalWrite(ledPin, LOW); // Turn off LED
}
}
bool isNightTime(int currentHour) {
// Check if current hour is between 20:00 and 05:00
if (currentHour >= 20 || currentHour < 5) {
return true;
} else {
return false;
}
}