Hi. I am using code written by Dave Nave to control a chicken coop door https://davenaves.com/blog/interests-projects/chickens/chicken-coop/arduino-chicken-door/. This code compiles and runs well in my experience (thank-you Mr Nave). What I'd like to add eventually is a way to manually open and close the door but first I need to time out the motors if the reed switch is not triggered (debris from the coop can block full travel of the door closing). I am a novice with code. Any thoughts where I could time out door open and door close for if the reed switches don't stop the motor?
Thank-you.
//This is the code used to restart the coop door. Trying different path to turning off serial display.
//This complies as of 11-14-2021 9:07 am
//version c is now tring to delete unwanted code
//Try uploading this to the coop on 11-14-2021
//Revised May 2022 to add delay at closing
//Try uploading to coop
//Reference Dave Nave https://davenaves.com/blog/interests-projects/chickens/chicken-coop/arduino-chicken-door/
// pins assignments
const int photocellPin = A0; // photocell connected to analog 0
const int enableCoopDoorMotorA = 8; // enable motor b - pin 7
const int directionCloseCoopDoorMotorA = 9; // direction close motor b - pin 8
const int directionOpenCoopDoorMotorA = 10; // direction open motor b - pin 9
const int bottomSwitchPin = 3; // bottom switch is connected to pin 3
const int topSwitchPin = 2; // top switch is connected to pin 2
const int coopDoorOpenLed = 4; // led set to digital pin 4
const int coopDoorClosedLed = 5; // led set to digital pin 5
// 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 = 600000; // 10 minutes
// debounce delay
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 100;
void setup(void) {
// coop door
// coop door motor
pinMode (enableCoopDoorMotorA, OUTPUT); // enable motor pin = output
pinMode (directionCloseCoopDoorMotorA, OUTPUT); // motor close direction pin = output
pinMode (directionOpenCoopDoorMotorA, OUTPUT); // motor open direction pin = output
// coop door leds
pinMode (coopDoorOpenLed, OUTPUT); // enable coopDoorOpenLed = output
pinMode (coopDoorClosedLed, OUTPUT); // enable coopDoorClosedLed = output
// 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 <= 10) {
photocellReadingLevel = '1';
} else if (photocellReading >= 11 && photocellReading <= 200) {
photocellReadingLevel = '2';
} else if (photocellReading >= 201 ) {
photocellReadingLevel = '3';
}
}
}
//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;
}
}
}
}
// debounce top reed switch
void debounceTopReedSwitch() {
topSwitchPinVal = digitalRead(topSwitchPin); // read input value and store it in val
if ((unsigned long)(millis() - lastDebounceTime) > debounceDelay) { // delay 10ms for consistent readings
topSwitchPinVal2 = digitalRead(topSwitchPin); // read input value again to check or bounce
if (topSwitchPinVal == topSwitchPinVal2) { // make sure we have 2 consistant readings
if (topSwitchPinVal != topSwitchState) { // the button state has changed!
topSwitchState = topSwitchPinVal;
}
}
}
}
// stop the coop door motor
void stopCoopDoorMotorA() {
digitalWrite (directionCloseCoopDoorMotorA, LOW); // turn off motor close direction
digitalWrite (directionOpenCoopDoorMotorA, LOW); // turn on motor open direction
analogWrite (enableCoopDoorMotorA, 0); // enable motor, 0 speed
}
// close the coop door motor (motor dir close = clockwise)
void closeCoopDoorMotorA() {
digitalWrite (directionCloseCoopDoorMotorA, HIGH); // turn on motor close direction
digitalWrite (directionOpenCoopDoorMotorA, LOW); // turn off motor open direction
analogWrite (enableCoopDoorMotorA, 255); // enable motor, full speed
if (bottomSwitchPinVal == 0) { // if bottom reed switch circuit is closed
stopCoopDoorMotorA();
digitalWrite (coopDoorOpenLed, LOW);
digitalWrite (coopDoorClosedLed, HIGH);
}
}
// open the coop door (motor dir open = counter-clockwise)
void openCoopDoorMotorA() {
digitalWrite(directionCloseCoopDoorMotorA, LOW); // turn off motor close direction
digitalWrite(directionOpenCoopDoorMotorA, HIGH); // turn on motor open direction
analogWrite(enableCoopDoorMotorA, 255); // enable motor, full speed
if (topSwitchPinVal == 0) { // if top reed switch circuit is closed
stopCoopDoorMotorA();
digitalWrite (coopDoorOpenLed, HIGH);
digitalWrite (coopDoorClosedLed, LOW);
}
}
// do the coop door
void doCoopDoor() {
if (photocellReadingLevel == '1') { // if it's dark
if (photocellReadingLevel != '2') { // if it's not twilight
if (photocellReadingLevel != '3') { // if it's not light
delay(2700000);
debounceTopReedSwitch(); // read and debounce the switches
debounceBottomReedSwitch();
closeCoopDoorMotorA(); // close the door
}
}
}
if (photocellReadingLevel == '3') { // if it's light
if (photocellReadingLevel != '2') { // if it's not twilight
if (photocellReadingLevel != '1') { // if it's not dark
debounceTopReedSwitch(); // read and debounce the switches
debounceBottomReedSwitch();
openCoopDoorMotorA(); // Open the door
}
}
}
}
// ************************************** the loop **************************************
void loop() {
doReadPhotoCell();
doCoopDoor();
}