const int openButtonPin = 2; // Pin connected to the button for opening the garage door
const int closeButtonPin = 3; // Pin connected to the button for closing the garage door
const int emergencySwitchPin = 5; // Pin connected to the emergency switch
const int relayPin = 4; // Pin connected to the relay module
bool emergency = false; // Flag to indicate emergency status
bool ledState = false; // Flag to track LED state
void setup() {
Serial.begin(115200);
pinMode(openButtonPin, INPUT_PULLUP);
pinMode(closeButtonPin, INPUT_PULLUP);
pinMode(emergencySwitchPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
}
void loop() {
if (digitalRead(emergencySwitchPin) == LOW) {
// Emergency switch is triggered
Serial.println("Emergency switch activated");
emergency = true; // Set emergency flag
stopLED(); // Stop LED operation
while (digitalRead(emergencySwitchPin) == LOW) {
// Wait for emergency switch to be reset
delay(100);
}
Serial.println("Emergency switch deactivated");
emergency = false; // Clear emergency flag
}
if (!emergency) {
// If not in emergency mode, check button presses
if (digitalRead(openButtonPin) == LOW) {
Serial.println("Toggling LED ON");
toggleLED(); // Turn on the LED
delay(1000); // Adjust this delay according to your needs
}
if (digitalRead(closeButtonPin) == LOW) {
Serial.println("Toggling LED OFF");
toggleLED(); // Turn off the LED
delay(1000); // Adjust this delay according to your needs
}
}
}
void toggleLED() {
ledState = !ledState; // Toggle LED state
digitalWrite(relayPin, ledState ? HIGH : LOW); // Set relay pin accordingly
}
void stopLED() {
digitalWrite(relayPin, LOW); // Turn off the LED
}