#define NUM_DOORS 4
// Door Open Request switches
const int dorPins[NUM_DOORS] = {16, 17, 18, 19};
// Door Contact sensors
const int contactPins[NUM_DOORS] = {12, 13, 14, 15};
// Relay outputs
const int relayPins[NUM_DOORS] = {25, 26, 27, 33};
// Emergency and Fire inputs
const int emergencyPin = 34;
const int firePin = 35;
// Toggle states
bool emergencyMode = false;
bool fireMode = false;
// For debouncing
bool prevEmergencyState = HIGH;
bool prevFireState = HIGH;
void setup() {
Serial.begin(115200);
for (int i = 0; i < NUM_DOORS; i++) {
pinMode(dorPins[i], INPUT_PULLUP);
pinMode(contactPins[i], INPUT_PULLUP);
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW);
}
pinMode(emergencyPin, INPUT_PULLUP);
pinMode(firePin, INPUT_PULLUP);
}
void loop() {
// Read and handle emergency toggle
bool currentEmergency = digitalRead(emergencyPin);
if (currentEmergency == LOW && prevEmergencyState == HIGH) {
emergencyMode = !emergencyMode;
Serial.println(emergencyMode ? "Emergency Mode ON" : "Emergency Mode OFF");
if (emergencyMode) activateAllDoors();
}
prevEmergencyState = currentEmergency;
// Read and handle fire alarm toggle
bool currentFire = digitalRead(firePin);
if (currentFire == LOW && prevFireState == HIGH) {
fireMode = !fireMode;
Serial.println(fireMode ? "Fire Alarm ON" : "Fire Alarm OFF");
if (fireMode) activateAllDoors();
}
prevFireState = currentFire;
// If in emergency or fire mode, keep relays on (skip normal operation)
if (emergencyMode || fireMode) {
// Keep all relays ON while in emergency/fire
setAllRelays(true);
} else {
// Normal door request logic
for (int i = 0; i < NUM_DOORS; i++) {
bool dorPressed = digitalRead(dorPins[i]) == LOW;
bool doorClosed = digitalRead(contactPins[i]) == HIGH;
if (dorPressed && doorClosed) {
Serial.print("Opening Door ");
Serial.println(i + 1);
digitalWrite(relayPins[i], HIGH);
delay(2000); // 2-second pulse
digitalWrite(relayPins[i], LOW);
}
}
setAllRelays(false); // turn off relays after normal loop
}
delay(100); // Debounce delay
}
void activateAllDoors() {
Serial.println("All doors unlocked!");
setAllRelays(true);
delay(2000);
}
void setAllRelays(bool state) {
for (int i = 0; i < NUM_DOORS; i++) {
digitalWrite(relayPins[i], state ? HIGH : LOW);
}
}