Hello Everyone.
I build a chicken coop door based on Dave Nave's blog. The door was working perfectly inside for a few weeks. Last weekend I finally moved the unit outside and now it's not working. For some reason, the photoresistor is not working.
I've tested the resistor and its resistance is low with light and high when I cover it. I can't figure this out...and I'm sure it's something really silly. This is the part that's not making sense- if I take the wires off the photoresistor and connect them, the door opens. If I leave the wires disconnected, the door closes.
With that being said, I'm thinking something in the code is wrong and I have to adjust the photosensor for outside light intensity?
I'm new to this and trying to figure it out.... I've posted the code below (from Dave's website).
// libraries
#include <OneWire.h> // load the onewire library for thermometer
#include <LiquidCrystal.h> // load the liquid crystal library
// print debug messages or not to serial
const boolean SerialDisplay = true;
// pins assignments
// temperature chip i/o
const int photocellPin = A0; // photocell connected to analog 0
const int enableCoopDoorMotorB = 7; // enable motor b - pin 7
const int directionCloseCoopDoorMotorB = 8; // direction close motor b - pin 8
const int directionOpenCoopDoorMotorB = 9; // direction open motor b - pin 9
const int bottomSwitchPin = 26; // bottom switch is connected to pin 26
const int topSwitchPin = 27; // top switch is connected to pin 27
// variables
// photocell
int photocellReading; // analog reading of the photocel
int photocellReadingLevel; // photocel reading levels (dark, twilight, light)
// reed switches top and bottom of coop door
// top switch
int topSwitchPinVal; // top switch var for reading the pin status
int topSwitchPinVal2; // top switch var for reading the pin delay/debounce status
int topSwitchState; // top switch var for to hold the switch state
// bottom switch
int bottomSwitchPinVal; // bottom switch var for reading the pin status
int bottomSwitchPinVal2; // bottom switch var for reading the pin delay/debounce status
int bottomSwitchState; // bottom switch var for to hold the switch state
// photocell reading delay
unsigned long lastPhotocellReadingTime = 0;
unsigned long photocellReadingDelay = 6000; // 1 minutes
// debounce delay
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 100;
// ************************************** the setup **************************************
void setup(void) {
Serial.begin(9600); // initialize serial port hardware
// welcome message
if (SerialDisplay) {
Serial.println(" Processes running:");
Serial.println(" Timer doReadPhotoCell every 1 minutes - light levels: open or close door");
}
// coop door
// coop door motor
pinMode (enableCoopDoorMotorB, OUTPUT); // enable motor pin = output
pinMode (directionCloseCoopDoorMotorB, OUTPUT); // motor close direction pin = output
pinMode (directionOpenCoopDoorMotorB, OUTPUT); // motor open direction pin = output
// coop door leds
pinMode (coopDoorOpenLed, OUTPUT); // enable coopDoorOpenLed = output
pinMode (coopDoorClosedLed, OUTPUT); // enable coopDoorClosedLed = output
digitalWrite(coopDoorClosedLed, LOW);
// coop door switches
// bottom switch
pinMode(bottomSwitchPin, INPUT); // set bottom switch pin as input
digitalWrite(bottomSwitchPin, HIGH); // activate bottom switch resistor
// top switch
pinMode(topSwitchPin, INPUT); // set top switch pin as input
digitalWrite(topSwitchPin, HIGH); // activate top switch resistor
}
// ************************************** functions **************************************
// operate the coop door
// photocel to read levels of exterior light
void doReadPhotoCell() { // function to be called repeatedly - per coopPhotoCellTimer set in setup
photocellReading = analogRead(photocellPin);
if ((unsigned long)(millis() - lastPhotocellReadingTime) >= photocellReadingDelay) {
lastPhotocellReadingTime = millis();
// set photocel threshholds
if (photocellReading >= 0 && photocellReading <= 3) {
photocellReadingLevel = '1';
if (SerialDisplay) {
Serial.println(" Photocel Reading Level:");
Serial.println(" - Dark");
}
}
else if (photocellReading >= 4 && photocellReading <= 120) {
photocellReadingLevel = '2';
if (SerialDisplay) {
Serial.println(" Photocel Reading Level:");
Serial.println(" - Twilight");
}
}
else if (photocellReading >= 125 ) {
photocellReadingLevel = '3';
if (SerialDisplay) {
Serial.println(" Photocel Reading Level:");
Serial.println(" - Light");
}
}
if (SerialDisplay) {
Serial.println(" Photocel Analog Reading = ");
Serial.println(photocellReading);
}
}
}
//debounce bottom reed switch
void debounceBottomReedSwitch() {
//debounce bottom reed switch
bottomSwitchPinVal = digitalRead(bottomSwitchPin); // read input value and store it in val
if ((unsigned long)(millis() - lastDebounceTime) > debounceDelay) { // delay 10ms for consistent readings
bottomSwitchPinVal2 = digitalRead(bottomSwitchPin); // read input value again to check or bounce
if (bottomSwitchPinVal == bottomSwitchPinVal2) { // make sure we have 2 consistant readings
if (bottomSwitchPinVal != bottomSwitchState) { // the switch state has changed!
bottomSwitchState = bottomSwitchPinVal;
}
if (SerialDisplay) {
Serial.print (" Bottom Switch Value: "); // display "Bottom Switch Value:"
Serial.println(digitalRead(bottomSwitchPin)); // display current value of bottom switch;
}
}
}
}