diff --git a/chicken_door.ino b/chicken_door.ino new file mode 100644 index 0000000..d1869e8 --- /dev/null +++ b/chicken_door.ino @@ -0,0 +1,150 @@ +/* + * Open and close the door on the chicken house depending on the + * ambient light levels. + * Copyright Alex Tucker 2013 + * + */ + + +// 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(); +} + +#define ONOFF_PIN 2 +#define FWREV_PIN 3 +#define DOOR_CONTACT_PIN 4 +#define BUTTON_PIN 5 +#define PHOTOCELL_PIN 0 + +#define INITIALIZE 0 +#define WAIT_FOR_LIGHT 1 +#define LOWER_DOOR 2 +#define WAIT_FOR_DARK 3 +#define RAISE_DOOR 4 + +#define BRIGHTNESS_SIZE 3 +#define BRIGHT_THRESHOLD 600 + +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: + raiseDoor(); + state = WAIT_FOR_LIGHT; + break; + } + enterSleep(); + } +}