#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
const int trigPin = 2;
const int echoPin = 3;
const int buttonPin = 4;
const int buzzerPin = 5;
const int NIGHT_START_HOUR = 22; // Night mode starts at 10:00 PM (24-hour format)
const int NIGHT_END_HOUR = 7; // Night mode ends at 7:00 AM (24-hour format)
const int DISTANCE_THRESHOLD = 5; // Threshold distance in meters
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Set the initial date and time (11:05 AM, 04/11/2023)
if (!rtc.isrunning()) {
rtc.adjust(DateTime(2023, 11, 4, 11, 5, 0)); // Year, Month, Day, Hour, Minute, Second
}
}
void loop() {
DateTime now = rtc.now();
// Check if it's nighttime (between 10:00 PM and 7:00 AM)
if (now.hour() >= NIGHT_START_HOUR || now.hour() < NIGHT_END_HOUR) {
// Check for motion (ultrasonic sensor) detection
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
if (distance < DISTANCE_THRESHOLD) {
// Motion detected within the threshold distance during nighttime, activate alert (buzzer)
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
}
// Check for manual activation (push button)
if (digitalRead(buttonPin) == LOW) {
// Button pressed during nighttime, activate alert (buzzer)
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000); // Wait for a moment before allowing another button press
}
}
// Rest of the code for other functionalities
}