// Pin definitions
const int BUTTON1_PIN = 14; // Manual control for RELAY1
const int BUTTON2_PIN = 27; // Manual control for RELAY2
const int RELAY1_PIN = 15;
const int RELAY2_PIN = 32;
const int PIR_PIN = 33;
const int LDR_PIN = 35;
bool occupancy1 = false;
bool occupancy2 = false;
void setup() {
Serial.begin(115200);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(PIR_PIN, INPUT);
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
digitalWrite(RELAY1_PIN, LOW);
digitalWrite(RELAY2_PIN, LOW);
}
void loop() {
static bool lastButton1State = HIGH;
static bool lastButton2State = HIGH;
bool currentButton1State = digitalRead(BUTTON1_PIN);
bool currentButton2State = digitalRead(BUTTON2_PIN);
// Manual toggle for RELAY1
if (lastButton1State == HIGH && currentButton1State == LOW) {
occupancy1 = !occupancy1;
digitalWrite(RELAY1_PIN, occupancy1 ? HIGH : LOW);
Serial.println(occupancy1 ? "Manual: Load 1 ON" : "Manual: Load 1 OFF");
delay(300);
}
lastButton1State = currentButton1State;
// Manual toggle for RELAY2
if (lastButton2State == HIGH && currentButton2State == LOW) {
occupancy2 = !occupancy2;
digitalWrite(RELAY2_PIN, occupancy2 ? HIGH : LOW);
Serial.println(occupancy2 ? "Manual: Load 2 ON" : "Manual: Load 2 OFF");
delay(300);
}
lastButton2State = currentButton2State;
// LDR reading
int ldrValue = analogRead(LDR_PIN);
Serial.print("LDR Value: ");
Serial.println(ldrValue);
const int DARK_THRESHOLD = 2000;
// PIR + LDR logic for both relays
if (digitalRead(PIR_PIN) == HIGH && ldrValue < DARK_THRESHOLD) {
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
Serial.println("Motion & Darkness Detected: Loads ON");
delay(5000);
digitalWrite(RELAY1_PIN, occupancy1 ? HIGH : LOW);
digitalWrite(RELAY2_PIN, occupancy2 ? HIGH : LOW);
Serial.println("Motion Timeout: Restoring manual states");
delay(3000);
}
}