// Define constants for pin numbers
const int RELAY_PINS[] = {12, 13, 14, 27};
const int BUTTON_PINS[] = {16, 17, 18, 19};
const int PIR_PINS[] = {32, 33, 25, 26};
// Define constants for the number of relays, buttons, and PIR sensors
const int NUM_RELAYS = sizeof(RELAY_PINS) / sizeof(RELAY_PINS[0]);
const int NUM_BUTTONS = sizeof(BUTTON_PINS) / sizeof(BUTTON_PINS[0]);
const int NUM_PIRS = sizeof(PIR_PINS) / sizeof(PIR_PINS[0]);
// Initialize relay states and last button states
bool relayStates[NUM_RELAYS] = {false};
bool lastButtonStates[NUM_BUTTONS] = {LOW};
// Include the RTClib library
#include <Wire.h>
#include <RTClib.h>
// Define the RTC DS1307 pin connections
const int SCL_PIN = 22;
const int SDA_PIN = 21;
// Create an instance of the RTC_DS1307
RTC_DS1307 rtc;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Initialize the RTC DS1307
Wire.begin(SDA_PIN, SCL_PIN);
rtc.begin();
// Check if the RTC DS1307 is running
if (!rtc.isrunning()) {
// Set the RTC DS1307 to the current date and time
rtc.adjust(DateTime(__DATE__, __TIME__));
}
// Initialize relay pins as outputs
initRelayPins();
// Initialize button pins as inputs with pull-down resistors
initButtonPins();
// Initialize PIR pins as inputs
initPirPins();
}
void loop() {
// Get the current date and time from the RTC DS1307
DateTime now = rtc.now();
// Print the current date and time to the serial monitor
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// Check time-based relay control
handleTimeBasedRelayControl(now);
// Check push buttons
for (int buttonIndex = 0; buttonIndex < NUM_BUTTONS; buttonIndex++) {
handleButton(BUTTON_PINS[buttonIndex], RELAY_PINS[buttonIndex], relayStates[buttonIndex], lastButtonStates[buttonIndex]);
}
// Check PIR sensors
for (int pirIndex = 0; pirIndex < NUM_PIRS; pirIndex++) {
handlePIR(PIR_PINS[pirIndex], RELAY_PINS[pirIndex], relayStates[pirIndex]);
}
// Wait for 1 second before checking again
delay(1000);
}
void initRelayPins() {
for (int relayIndex = 0; relayIndex < NUM_RELAYS; relayIndex++) {
pinMode(RELAY_PINS[relayIndex], OUTPUT);
digitalWrite(RELAY_PINS[relayIndex], LOW); // Start with all relays off
}
}
void initButtonPins() {
for (int buttonIndex = 0; buttonIndex < NUM_BUTTONS; buttonIndex++) {
pinMode(BUTTON_PINS[buttonIndex], INPUT_PULLDOWN);
}
}
void initPirPins() {
for (int pirIndex = 0; pirIndex < NUM_PIRS; pirIndex++) {
pinMode(PIR_PINS[pirIndex], INPUT);
}
}
void handleButton(int buttonPin, int relayPin, bool &relayState, bool &lastButtonState) {
bool buttonState = digitalRead(buttonPin);
// Toggle relay state on button press
if (buttonState && !lastButtonState) {
relayState = !relayState;
digitalWrite(relayPin, relayState ? HIGH : LOW);
}
lastButtonState = buttonState;
}
void handlePIR(int pirPin, int relayPin, bool &relayState) {
if (digitalRead(pirPin) == HIGH) {
relayState = true;
digitalWrite(relayPin, HIGH);
} else if (!relayState) {
// Only turn off the relay if it wasn't turned on by the button
digitalWrite(relayPin, LOW);
}
}
void handleTimeBasedRelayControl(DateTime now) {
// Example time-based control for relay 1 and relay 2
// Relay 1: ON at 6:00 AM, OFF at 8:00 AM
if (now.hour() == 6 && now.minute() == 0) {
digitalWrite(RELAY_PINS[0], HIGH);
relayStates[0] = true;
} else if (now.hour() == 8 && now.minute() == 0) {
digitalWrite(RELAY_PINS[0], LOW);
relayStates[0] = false;
}
// Relay 2: ON at 6:00 PM, OFF at 10:00 PM
if (now.hour() == 18 && now.minute() == 0) {
digitalWrite(RELAY_PINS[1], HIGH);
relayStates[1] = true;
} else if (now.hour() == 22 && now.minute() == 0) {
digitalWrite(RELAY_PINS[1], LOW);
relayStates[1] = false;
} // Relay 3: ON at 6:00 PM, OFF at 10:00 PM
if (now.hour() == 18 && now.minute() == 0) {
digitalWrite(RELAY_PINS[1], HIGH);
relayStates[1] = true;
} else if (now.hour() == 22 && now.minute() == 0) {
digitalWrite(RELAY_PINS[1], LOW);
relayStates[1] = false;
}
// Relay 4: ON at 6:00 PM, OFF at 1:00 PM
if (now.hour() == 18 && now.minute() == 0) {
digitalWrite(RELAY_PINS[1], HIGH);
relayStates[1] = true;
} else if (now.hour() == 19 && now.minute() == 7) {
digitalWrite(RELAY_PINS[1], LOW);
relayStates[1] = false;
}
}