diff --git a/chicken_door.ino b/chicken_door.ino index fc418bd..73f9854 100644 --- a/chicken_door.ino +++ b/chicken_door.ino @@ -5,35 +5,7 @@ * */ - -// Enable deep sleep with wake on watchdog interrupt. -// Based on Donal Morrissey's code at http://donalmorrissey.blogspot.co.uk/2010/04/sleeping-arduino-part-5-wake-up-via.html -#include -#include -#include - -volatile int f_wdt=1; - -ISR(WDT_vect) { - if (f_wdt == 0) - { - f_wdt=1; - } -} - -void enterSleep(void) { - set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* PWR_SAVE or PWR_DOWN */ - sleep_enable(); - sleep_mode(); - - /* The program will continue from here after the WDT timeout*/ - sleep_disable(); /* First thing to do is disable sleep. */ - - /* Re-enable the peripherals. */ - power_all_enable(); -// delay(8000); -// f_wdt = 1; -} +#include #define ONOFF_PIN 5 #define FWREV_PIN 3 @@ -48,44 +20,39 @@ #define RAISE_DOOR 4 #define BRIGHTNESS_SIZE 3 -#define BRIGHT_THRESHOLD 800 +#define BRIGHT_THRESHOLD 700 #define OPEN_DELAY 6250 +#define HYSTERESIS 50 word brightness[BRIGHTNESS_SIZE]; int state = WAIT_FOR_LIGHT; - -void setupWDT() { - /* Clear the reset flag. */ - MCUSR &= ~(1< BRIGHT_THRESHOLD) { - state = LOWER_DOOR; - } - break; - case LOWER_DOOR: - lowerDoor(); - state = WAIT_FOR_DARK; - break; - case WAIT_FOR_DARK: - if (readBrightness() < BRIGHT_THRESHOLD) { - state = RAISE_DOOR; - } - break; - case RAISE_DOOR: + switch (state) { + case INITIALIZE: + if (digitalRead(DOOR_CONTACT_PIN) == LOW) { raiseDoor(); - state = WAIT_FOR_LIGHT; - break; - } - enterSleep(); + } + state = WAIT_FOR_LIGHT; + break; + case WAIT_FOR_LIGHT: + if ((readBrightness() > (BRIGHT_THRESHOLD + HYSTERESIS)) || (button_state == 1)) { + button_state = 0; + state = LOWER_DOOR; + } else { + Narcoleptic.delay(30000); + } + break; + case LOWER_DOOR: + lowerDoor(); + state = WAIT_FOR_DARK; + break; + case WAIT_FOR_DARK: + if ((readBrightness() < (BRIGHT_THRESHOLD - HYSTERESIS)) || (button_state == 1)) { + button_state = 0; + state = RAISE_DOOR; + } else { + Narcoleptic.delay(30000); + } + break; + case RAISE_DOOR: + raiseDoor(); + state = WAIT_FOR_LIGHT; + break; } }