const int PIR_SENSOR_PIN = 12;
const int RELAY_PIN = 13;
const int LED_Open = 2; // Open switch indicator
const int LED_Closed = 3; // Closed switch indicator
unsigned long motionDetectedTime = 0; // To keep track of the last motion detected time
bool motionDetected = false; // To keep track of whether motion was detected
void setup() {
pinMode(PIR_SENSOR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
int pirValue = digitalRead(PIR_SENSOR_PIN); // Read PIR sensor value
if (pirValue == HIGH) { // If motion is detected
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(LED_Closed, HIGH);
digitalWrite(LED_Open, LOW);
motionDetected = true; // Set motion detected flag
motionDetectedTime = millis(); // Update the time motion was detected
} else { // If no motion is detected
if (motionDetected && (millis() - motionDetectedTime) >= 10000) { // Check if 10 seconds have passed
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LED_Open, HIGH);
digitalWrite(LED_Closed, LOW);
motionDetected = false; // Reset motion detected flag
}
}
}