| |
---|
| | #define AUX_BUTTON_PIN 7 |
---|
| | |
---|
| | #define INITIALIZE 0 |
---|
| | #define WAIT_FOR_LIGHT 1 |
---|
| | #define LOWER_DOOR 2 |
---|
| | #define OPEN_DOOR 2 |
---|
| | #define WAIT_FOR_DARK 3 |
---|
| | #define RAISE_DOOR 4 |
---|
| | #define CLOSE_DOOR 4 |
---|
| | |
---|
| | #define BRIGHTNESS_SIZE 3 |
---|
| | #define BRIGHT_THRESHOLD 600 |
---|
| | #define BRIGHT_THRESHOLD 200 |
---|
| | #define OPEN_DELAY 6400 |
---|
| | #define MAX_CLOSE_TIME 4000 |
---|
| | #define MAX_OPEN_TIME 4000 |
---|
| | #define HYSTERESIS 50 |
---|
| | |
---|
| | word brightness[BRIGHTNESS_SIZE]; |
---|
| | int state = WAIT_FOR_LIGHT; |
---|
| | volatile int button_state = 0; |
---|
| | |
---|
| | void setup() { |
---|
| | pinMode(ONOFF_PIN, OUTPUT); |
---|
| | pinMode(FWREV_PIN, OUTPUT); |
---|
| |
---|
| | Narcoleptic.disableSPI(); |
---|
| | Narcoleptic.disableADC(); |
---|
| | } |
---|
| | |
---|
| | /* void pressButton() { |
---|
| | button_state = 1; |
---|
| | } */ |
---|
| | |
---|
| | void raiseDoor() { |
---|
| | void closeDoor() { |
---|
| | long millisLeft = MAX_CLOSE_TIME; |
---|
| | digitalWrite(TEMP_SUPPLY_PIN, HIGH); |
---|
| | digitalWrite(FWREV_PIN, LOW); |
---|
| | delay(100); |
---|
| |
---|
| | while ((digitalRead(DOOR_CONTACT_2_PIN) == LOW) && (millisLeft > 0)) { |
---|
| | delay(100); |
---|
| | millisLeft -= 100; |
---|
| | } |
---|
| | delay(200); |
---|
| | // delay(200); |
---|
| | digitalWrite(ONOFF_PIN, LOW); |
---|
| | delay(10); |
---|
| | digitalWrite(TEMP_SUPPLY_PIN, LOW); |
---|
| | } |
---|
| | |
---|
| | void lowerDoor() { |
---|
| | void openDoor() { |
---|
| | long millisLeft = MAX_CLOSE_TIME; |
---|
| | digitalWrite(TEMP_SUPPLY_PIN, HIGH); |
---|
| | digitalWrite(FWREV_PIN, HIGH); |
---|
| | delay(100); |
---|
| |
---|
| | while ((digitalRead(DOOR_CONTACT_1_PIN) == LOW) && (millisLeft > 0)) { |
---|
| | delay(100); |
---|
| | millisLeft -= 100; |
---|
| | } |
---|
| | delay(200); |
---|
| | delay(250); |
---|
| | digitalWrite(ONOFF_PIN, LOW); |
---|
| | delay(10); |
---|
| | digitalWrite(FWREV_PIN, LOW); |
---|
| | digitalWrite(TEMP_SUPPLY_PIN, LOW); |
---|
| |
---|
| | case INITIALIZE: |
---|
| | digitalWrite(TEMP_SUPPLY_PIN, HIGH); |
---|
| | delay(500); |
---|
| | if (digitalRead(DOOR_CONTACT_2_PIN) == LOW) { |
---|
| | raiseDoor(); |
---|
| | closeDoor(); |
---|
| | } |
---|
| | 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; |
---|
| | if (readBrightness() > (BRIGHT_THRESHOLD + HYSTERESIS)) { |
---|
| | DPRINTLN("-> OPEN_DOOR"); |
---|
| | state = OPEN_DOOR; |
---|
| | } else { |
---|
| | #ifdef DEBUG |
---|
| | delay(10000); |
---|
| | #else |
---|
| | Narcoleptic.delay(30000); |
---|
| | #endif |
---|
| | } |
---|
| | break; |
---|
| | case LOWER_DOOR: |
---|
| | lowerDoor(); |
---|
| | case OPEN_DOOR: |
---|
| | openDoor(); |
---|
| | 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; |
---|
| | if (readBrightness() < (BRIGHT_THRESHOLD - HYSTERESIS)) { |
---|
| | DPRINTLN("-> CLOSE_DOOR"); |
---|
| | state = CLOSE_DOOR; |
---|
| | } else { |
---|
| | #ifdef DEBUG |
---|
| | delay(10000); |
---|
| | #else |
---|
| | Narcoleptic.delay(30000); |
---|
| | #endif |
---|
| | } |
---|
| | break; |
---|
| | case RAISE_DOOR: |
---|
| | raiseDoor(); |
---|
| | case CLOSE_DOOR: |
---|
| | closeDoor(); |
---|
| | DPRINTLN("-> WAIT_FOR_LIGHT"); |
---|
| | state = WAIT_FOR_LIGHT; |
---|
| | break; |
---|
| | } |
---|
| | |