// Include necessary libraries
#include <Arduino.h>
// Define relay control pin
#define RELAY_PIN 6
// Define sensor pins
#define SENSOR_1 A0
#define SENSOR_2 A1
#define SENSOR_3 A2
#define SENSOR_4 A3
#define SENSOR_5 A4
#define SENSOR_6 A5
// Define timing constants (leave as you had them)
#define ZERO_CHECK_DURATION 10000 // time setting to check for Door open
#define RELAY_PULSE_DURATION 20000 // x seconds relay pulse
#define SENSOR_CHECK_INTERVAL 1000 // Check sensors every 1 second
// New: Reset duration when ALL doors remain closed -> reset relay (2 minutes)
#define RESET_DURATION 120000 // 2 minutes in ms
// Relay active level in your sketch: you used HIGH in setup as "off"
// so keep the same logic: RELAY_ON = LOW, RELAY_OFF = HIGH
const uint8_t RELAY_ON = LOW;
const uint8_t RELAY_OFF = HIGH;
// Latch state: true when relay has been triggered and latched
bool relayLatched = false;
// Timer for reset when ALL sensors are closed
unsigned long allClosedStart = 0;
// Helper: array of sensor pins for easy iteration
const int sensorPins[6] = { SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4, SENSOR_5, SENSOR_6 };
void setup() {
// Start serial communication
Serial.begin(9600);
while (!Serial)
; // Wait for serial monitor
// Set relay pin as output
pinMode(RELAY_PIN, OUTPUT);
// Set sensor pins as input
pinMode(SENSOR_1, INPUT_PULLUP);
pinMode(SENSOR_2, INPUT_PULLUP);
pinMode(SENSOR_3, INPUT_PULLUP);
pinMode(SENSOR_4, INPUT_PULLUP);
pinMode(SENSOR_5, INPUT_PULLUP);
pinMode(SENSOR_6, INPUT_PULLUP);
Serial.println("System Initialized...");
Serial.println("Monitoring for continuous zero readings...");
// Turn relay OFF initially
digitalWrite(RELAY_PIN, RELAY_OFF);
}
void loop() {
// Check each sensor for continuous zero readings
checkSensorForContinuousZero(SENSOR_1, "Sensor 1");
checkSensorForContinuousZero(SENSOR_2, "Sensor 2");
checkSensorForContinuousZero(SENSOR_3, "Sensor 3");
checkSensorForContinuousZero(SENSOR_4, "Sensor 4");
checkSensorForContinuousZero(SENSOR_5, "Sensor 5");
checkSensorForContinuousZero(SENSOR_6, "Sensor 6");
// After checking sensors, evaluate reset condition:
checkAllClosedForReset();
delay(100); // Small delay between sensor checks (preserves original pacing)
}
void checkSensorForContinuousZero(int sensorPin, String sensorName) {
Serial.println("Checking " + sensorName + "...");
unsigned long startTime = millis();
bool continuousZero = true;
// Monitor sensor for ZERO_CHECK_DURATION
while (millis() - startTime < ZERO_CHECK_DURATION) {
int sensorValue = analogRead(sensorPin);
Serial.println(sensorName + " reading: " + String(sensorValue));
// If sensor is not zero, break the continuous zero check
if (sensorValue != 0) {
continuousZero = false;
Serial.println(sensorName + " not zero - check failed");
break;
}
// Wait SENSOR_CHECK_INTERVAL before next reading
delay(SENSOR_CHECK_INTERVAL);
}
// If sensor was zero for continuous ZERO_CHECK_DURATION, trigger relay (only if not already latched)
if (continuousZero) {
Serial.println(sensorName + " was ZERO for required duration.");
if (!relayLatched) {
Serial.println(sensorName + " TRIGGERING RELAY (and latching)!");
// Relay cycle (kept same as original behavior)
Serial.println("Starting relay pulse...");
// Turn relay ON (active LOW in your setup)
digitalWrite(RELAY_PIN, RELAY_ON);
Serial.println("Relay ON (LOW)");
// Set latched state (so reset logic can later de-assert it)
relayLatched = true;
Serial.println("Relay latched. Waiting for reset condition (all doors closed for 2 minutes).");
// Reset allClosedStart because we just triggered relay and want a fresh wait
allClosedStart = 0;
} else {
Serial.println("Relay already latched. Ignoring repeated trigger.");
}
}
}
// Check if ALL sensors are non-zero continuously for RESET_DURATION; then reset relay
void checkAllClosedForReset() {
// If relay is not latched there's nothing to reset
if (!relayLatched) {
allClosedStart = 0;
return;
}
// Read all sensors
bool allClosed = true;
for (int i = 0; i < 6; i++) {
int val = analogRead(sensorPins[i]);
// A reading of 0 = open in your logic, so non-zero means closed
if (val == 0) {
allClosed = false;
break;
}
}
if (allClosed) {
if (allClosedStart == 0) {
allClosedStart = millis();
Serial.println("All sensors read non-zero. Starting reset countdown...");
} else {
unsigned long elapsed = millis() - allClosedStart;
// If all closed for RESET_DURATION, reset the relay
if (elapsed >= RESET_DURATION) {
Serial.println("All doors remained closed for 2 minutes -> RESETTING RELAY");
relayLatched = false;
Serial.println("Relay OFF (HIGH)");
digitalWrite(RELAY_PIN, RELAY_OFF); // Ensure relay is OFF
allClosedStart = 0;
} else {
// optional: print countdown occasionally (uncomment if desired)
// Serial.println("Reset countdown: " + String((RESET_DURATION - elapsed) / 1000) + " seconds remaining");
}
}
} else {
// Not all closed — cancel/reset countdown
if (allClosedStart != 0) {
Serial.println("A door opened again - reset countdown cancelled.");
}
allClosedStart = 0;
}
}