/* Code for Adafruit Trinket (5V; Arduino) This application monitors a switch. When it detects closure, the app waits open_delay seconds. If the switch remains closed for this period, the remote control is energized via output rc for one second. If the switch opens again in less than open_delay (indicating the garage door has closed), rc is not energized and the app returns to monitoring the switch. */ // Pin 1 = LED on Tinket // Pin 0 = output to remote control, active-high // Pin 2 = input from switch, active-low, with pullup; use switch pins 1 & 3. int led = 1; int rc = 0; int sw = 2; long temp; long open_delay = 90; // Number of seconds to wait until closing the door void setup() { pinMode(led, OUTPUT); pinMode(rc, OUTPUT); pinMode(sw, INPUT_PULLUP); digitalWrite(rc, LOW); open_delay = open_delay * 1000; // Scale to msec. } void loop() { while (digitalRead(sw)) // Wait for sw to go low, meaning door is open ; debounce_low(); temp = millis(); // Wait for open_delay to pass or switch goes high again (indicating door has closed) while (((millis() - temp) < open_delay) & (digitalRead(sw) == LOW)) ; if ((millis() - temp) >= open_delay) { digitalWrite(rc, HIGH); // Activate remote to close the door wait(1000); // Wait a second digitalWrite(rc, LOW); // Deactivate remote } debounce_high(); // Debounce a high on the switch whether there is one or not. // Doesn't do any harm and saves a line to check it. } // Wait for dur msec. void wait(long dur) { long start = millis(); while ((millis() - start) < dur) ; } // Debounces the switch after it goes low. void debounce_low(void) { long db; db = millis(); while (1) { while ((digitalRead(sw) == LOW) && ((millis() - db) < 20)) ; if ((millis() - db) >= 20) break; db = millis(); } } // Debounces the switch after it returns high. void debounce_high(void) { long db; db = millis(); while (1) { while ((digitalRead(sw) == HIGH) && ((millis() - db) < 20)) ; if ((millis() - db) >= 20) break; db = millis(); } } // flash() is used for initial testing void flash() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) wait(50); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW wait(50); // wait for a second }