diff --git a/chicken_door.ino b/chicken_door.ino index 73f9854..3e860e1 100644 --- a/chicken_door.ino +++ b/chicken_door.ino @@ -7,11 +7,20 @@ #include +#define NODEBUG + +#ifdef DEBUG +#define DPRINTLN(str) Serial.println(str) +#else +#define DPRINTLN(str) +#endif + #define ONOFF_PIN 5 #define FWREV_PIN 3 #define DOOR_CONTACT_PIN 4 #define BUTTON_PIN 2 #define PHOTOCELL_PIN 0 +#define TEMP_SUPPLY_PIN 6 #define INITIALIZE 0 #define WAIT_FOR_LIGHT 1 @@ -34,6 +43,8 @@ pinMode(DOOR_CONTACT_PIN, INPUT); digitalWrite(DOOR_CONTACT_PIN, LOW); pinMode(BUTTON_PIN, INPUT); + pinMode(TEMP_SUPPLY_PIN, OUTPUT); + digitalWrite(TEMP_SUPPLY_PIN, HIGH); // for initial photocell reading // attachInterrupt(0, pressButton, LOW); digitalWrite(ONOFF_PIN, LOW); digitalWrite(FWREV_PIN, LOW); @@ -42,11 +53,17 @@ for (int i = 0; i < BRIGHTNESS_SIZE; i++) { brightness[i] = currentBrightness; } + digitalWrite(TEMP_SUPPLY_PIN, LOW); Narcoleptic.disableTimer1(); Narcoleptic.disableTimer2(); +#ifdef DEBUG + Serial.begin(9600); +#else Narcoleptic.disableSerial(); +#endif Narcoleptic.disableWire(); Narcoleptic.disableSPI(); + Narcoleptic.disableADC(); } /* void pressButton() { @@ -54,17 +71,21 @@ } */ void raiseDoor() { + digitalWrite(TEMP_SUPPLY_PIN, HIGH); digitalWrite(FWREV_PIN, LOW); - delay(10); + delay(100); digitalWrite(ONOFF_PIN, HIGH); while (digitalRead(DOOR_CONTACT_PIN) == LOW) { delay(100); } + delay(200); digitalWrite(ONOFF_PIN, LOW); delay(10); + digitalWrite(TEMP_SUPPLY_PIN, LOW); } void lowerDoor() { + digitalWrite(TEMP_SUPPLY_PIN, HIGH); digitalWrite(FWREV_PIN, HIGH); delay(10); digitalWrite(ONOFF_PIN, HIGH); @@ -72,10 +93,17 @@ digitalWrite(ONOFF_PIN, LOW); delay(10); digitalWrite(FWREV_PIN, LOW); + digitalWrite(TEMP_SUPPLY_PIN, LOW); } word readBrightness() { + Narcoleptic.enableADC(); + digitalWrite(TEMP_SUPPLY_PIN, HIGH); + delay(500); word currentBrightness = analogRead(PHOTOCELL_PIN); + DPRINTLN(currentBrightness); + digitalWrite(TEMP_SUPPLY_PIN, LOW); + Narcoleptic.disableADC(); unsigned long total = brightness[0]; for (int i = 0; i < BRIGHTNESS_SIZE; i++) { if (i == (BRIGHTNESS_SIZE - 1)) { @@ -92,33 +120,48 @@ void loop() { switch (state) { case INITIALIZE: + digitalWrite(TEMP_SUPPLY_PIN, HIGH); + delay(500); if (digitalRead(DOOR_CONTACT_PIN) == LOW) { raiseDoor(); } + DPRINTLN("-> WAIT_FOR_LIGHT"); state = WAIT_FOR_LIGHT; break; case WAIT_FOR_LIGHT: if ((readBrightness() > (BRIGHT_THRESHOLD + HYSTERESIS)) || (button_state == 1)) { button_state = 0; + DPRINTLN("-> LOWER_DOOR"); state = LOWER_DOOR; } else { +#ifdef DEBUG + delay(10000); +#else Narcoleptic.delay(30000); +#endif } break; case LOWER_DOOR: lowerDoor(); + DPRINTLN("-> WAIT_FOR_DARK"); state = WAIT_FOR_DARK; break; case WAIT_FOR_DARK: if ((readBrightness() < (BRIGHT_THRESHOLD - HYSTERESIS)) || (button_state == 1)) { button_state = 0; + DPRINTLN("-> RAISE_DOOR"); state = RAISE_DOOR; } else { +#ifdef DEBUG + delay(10000); +#else Narcoleptic.delay(30000); +#endif } break; case RAISE_DOOR: raiseDoor(); + DPRINTLN("-> WAIT_FOR_LIGHT"); state = WAIT_FOR_LIGHT; break; }