So I'm trying have a button override which will open or close the coop door based on bottom and top Reed switch values and implement a delay of about a 1/2 hour on docoopdoor() function. Any help would be appreciated.
My problem definitely lies in button override function in my functions. I have everthing working with no errors. Coop door opens and closes as expected based on photoresistor reading. Temp sensors work. The only thing I'm having trouble with is implementing this button override function to open and
close the coop door on demand when needed. I would like to be able to push it and either open it or close it based on the coop door status. Any help would be appreciated.
Thanks,
Chris D.
P.S. I posted about have the code the rest is in the attachment. Should be the entire sketch.
// ************************************** functions **************************************
// coop door button override // So I'm trying have a button override which will open or close the coop door based on bottom and top
void buttonOverride() { // Reed switch values and implement a delay of about a 1/2 hour on docoopdoor() function. Any help would be appreciated.
Serial.print(" Button State: ");
Serial.println(buttonswitchval);
buttonswitchval = digitalRead(buttonswitch);
delay(10);
buttonswitchval2 = digitalRead(buttonswitch);
delay(10);
if (buttonswitchval == buttonswitchval2) {
if (buttonswitchval != buttonswitchstate) {
if (buttonswitchval == LOW) {
if (buttonmode == 0) {
buttonmode = 1;
} //else {
// if
// debounceTopReedSwitch(); // read and debounce the switches
// debounceBottomReedSwitch();
if (buttonmode == 1 & topSwitchPinVal == 0 & bottomSwitchPinVal == 1) { // door closed
openCoopDoorMotorB(); // Open the door
delay(300000);
doCoopDoor();
}
else {
//debounceTopReedSwitch();
//debounceBottomReedSwitch();
if (buttonmode == 1 & topSwitchPinVal == 1 & bottomSwitchPinVal == 0) {
closeCoopDoorMotorB();
delay(300000);
doCoopDoor();
}
}
}
buttonswitchstate = buttonswitchval; // Save button switch state in buttonswitchval
}
}
}
// coop hvac
// operate the coop door
// photocel to read levels of exterior light
void readPhotoCell() { // function to be called repeatedly - per cooptimer set in setup
photocellReading = analogRead(photocellPin);
Serial.print(" Photocel Analog Reading = ");
Serial.println(photocellReading);
// set photocel threshholds
if (photocellReading >= 0 && photocellReading <= 3) {
photocellReadingLevel = '1';
Serial.print(" Photocel Reading Level:");
Serial.println(" - Dark");
} else if (photocellReading >= 4 && photocellReading <= 120){
photocellReadingLevel = '2';
Serial.print(" Photocel Reading Level:");
Serial.println(" - Twilight");
} else if (photocellReading >= 125 ) {
photocellReadingLevel = '3';
Serial.print(" Photocel Reading Level:");
Serial.println(" - Light");
}
}
//debounce bottom reed switch
void debounceBottomReedSwitch() {
//debounce bottom reed switch
bottomSwitchPinVal = digitalRead(bottomSwitchPin); // read input value and store it in val
delay(10);
bottomSwitchPinVal2 = digitalRead(bottomSwitchPin); // read input value again to check or bounce
if (bottomSwitchPinVal == bottomSwitchPinVal2) { // make sure we got 2 consistant readings!
if (bottomSwitchPinVal != bottomSwitchState) { // the switch state has changed!
bottomSwitchState = bottomSwitchPinVal;
}
Serial.print (" Bottom Switch Value: "); // display "Bottom Switch Value:"
Serial.println(digitalRead(bottomSwitchPin)); // display current value of bottom switch;
}
}
// debounce top reed switch
void debounceTopReedSwitch() {
topSwitchPinVal = digitalRead(topSwitchPin); // read input value and store it in val
delay(10);
topSwitchPinVal2 = digitalRead(topSwitchPin); // read input value again to check or bounce
if (topSwitchPinVal == topSwitchPinVal2) { // make sure we got 2 consistant readings!
if (topSwitchPinVal != topSwitchState) { // the button state has changed!
topSwitchState = topSwitchPinVal;
}
Serial.print (" Top Switch Value: "); // display "Bottom Switch Value:"
Serial.println(digitalRead(topSwitchPin)); // display current value of bottom switch;
}
}
// stop the coop door motor
void stopCoopDoorMotorB(){
digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn on motor open direction
analogWrite (enableCoopDoorMotorB, 0); // enable motor, 0 speed
}
// close the coop door motor (motor dir close = clockwise)
void closeCoopDoorMotorB() {
delay(900); // New delay to keep motor running for locks
digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
analogWrite (enableCoopDoorMotorB, 255); // enable motor, full speed
if (bottomSwitchPinVal == 0) { // if bottom reed switch circuit is closed
stopCoopDoorMotorB();
Serial.print(" Coop Door Closed - no danger");
}
}
// open the coop door (motor dir open = counter-clockwise)
void openCoopDoorMotorB() {
digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
analogWrite(enableCoopDoorMotorB, 255); // enable motor, full speed
if (topSwitchPinVal == 0) { // if top reed switch circuit is closed
stopCoopDoorMotorB();
Serial.print(" Coop Door open - danger!");
}
}
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
debounceTopReedSwitch(); // read and debounce the switches
debounceBottomReedSwitch();
closeCoopDoorMotorB(); // 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();
openCoopDoorMotorB(); // Open the door
}
}
}
}
// chickenCamServo
void doChickenCamServo() {
for (chickenCamServoPos = 0; chickenCamServoPos < 75; chickenCamServoPos += 1) { // turns servo 0 to 75 degrees; 1 degree at a time
chickenCamServo.write(chickenCamServoPos); // read chickenCamServoPosition variable chickenCamServoPos
}
for(chickenCamServoPos = 75; chickenCamServoPos>=1; chickenCamServoPos-= 1){ // return from 75 degrees to 0 degrees; 1 degree at a time
chickenCamServo.write(chickenCamServoPos); // read chickenCamServoPosition variable chickenCamServoPos
}
}
// lcd
// ************************************** the loop **************************************
void loop() {
coopTimer.run(); // ( timers for doCoopHVAC & readPhotoCell)
// delay(1000);
doCoopDoor();
doChickenCamServo();
doLcdMsg();
buttonOverride();
}
Moderator edit: tags corrected
CoopController_mixed2.ino (20.3 KB)