diff --git a/chicken_door.ino b/chicken_door.ino index d1869e8..fc418bd 100644 --- a/chicken_door.ino +++ b/chicken_door.ino @@ -31,12 +31,14 @@ /* Re-enable the peripherals. */ power_all_enable(); +// delay(8000); +// f_wdt = 1; } -#define ONOFF_PIN 2 +#define ONOFF_PIN 5 #define FWREV_PIN 3 #define DOOR_CONTACT_PIN 4 -#define BUTTON_PIN 5 +#define BUTTON_PIN 2 #define PHOTOCELL_PIN 0 #define INITIALIZE 0 @@ -46,7 +48,8 @@ #define RAISE_DOOR 4 #define BRIGHTNESS_SIZE 3 -#define BRIGHT_THRESHOLD 600 +#define BRIGHT_THRESHOLD 800 +#define OPEN_DELAY 6250 word brightness[BRIGHTNESS_SIZE]; int state = WAIT_FOR_LIGHT; @@ -74,7 +77,7 @@ pinMode(BUTTON_PIN, INPUT); digitalWrite(ONOFF_PIN, LOW); digitalWrite(FWREV_PIN, LOW); - Serial.begin(9600); +// Serial.begin(9600); state = INITIALIZE; word currentBrightness = analogRead(PHOTOCELL_PIN); for (int i = 0; i < BRIGHTNESS_SIZE; i++) { @@ -98,7 +101,7 @@ digitalWrite(FWREV_PIN, HIGH); delay(10); digitalWrite(ONOFF_PIN, HIGH); - delay(10000); + delay(OPEN_DELAY); digitalWrite(ONOFF_PIN, LOW); delay(10); digitalWrite(FWREV_PIN, LOW); @@ -106,13 +109,23 @@ word readBrightness() { word currentBrightness = analogRead(PHOTOCELL_PIN); - unsigned long total = currentBrightness; - for (int i = 0; i < (BRIGHTNESS_SIZE - 1); i++) { - brightness[i] = brightness[i+1]; + unsigned long total = brightness[0]; + for (int i = 0; i < BRIGHTNESS_SIZE; i++) { + if (i == (BRIGHTNESS_SIZE - 1)) { + brightness[i] = analogRead(PHOTOCELL_PIN); + } else { + brightness[i] = brightness[i+1]; + } total += brightness[i]; } - brightness[BRIGHTNESS_SIZE - 1] = currentBrightness; - return total / BRIGHTNESS_SIZE; + word avg = total / (BRIGHTNESS_SIZE + 1); +/* Serial.begin(9600); + Serial.print("Brightness, avg: "); + Serial.print(avg); + Serial.print(", current: "); + Serial.print(currentBrightness); + Serial.print("\n"); */ + return avg; } void loop() {