I'm working on a simple project that keeps my garage door closed, as my neighbor continues to forget to close it even though we've been burglarized >:(
All the code works properly (relevant section pasted below) assuming the door is not closed manually while the loop is running. But I've been struggling with the proper way to cancel the loop if the switchstate becomes low. I tried a few different things, including a while loop, a second if statement within the loop, and ISR but to no avail. My research suggests an interrupt is the correct method as it is continuously evaluating for the state to change, but the application of that code is still a bit beyond my level. Any ideas?
void setup() {
pinMode(8, OUTPUT); // Relay
pinMode(2, INPUT); // Switch
pinMode(13, OUTPUT); // LED
}
void loop() {
switchstate = digitalRead(2);
if (switchstate == HIGH) { // If Garage Door is open
delay(2 * 60 * 1000UL); // Wait a pre-determined amount of time (2 minutes)
blink1(10); // Warning LED blinks : 10 seconds
blink2(10); // Blink faster: 5 seconds
blink3(22); // Blink super fast: 5 seconds
! // Determine if switch state has changed, and if so, cancel program and return to beginning of loop
digitalWrite(8, HIGH); // Activate relay
delay(500); // For half a second
digitalWrite(8, LOW); // Relay off
delay(1 * 1000UL); // Program paused while door closes. (replace 1 with # of seconds this takes)
return; // Return to detecting door state
}
}