The code is modified from a sketch written by Dave Nave. You helped him early on with his coding. It works beautifully with my hardware setup for the Chicken Coop Door Control with one exception. When the door closes, a reed switch at the bottom of the door closes and the motor stops. The problem - I need the motor to continue running for a few seconds to allow the predator locks to engage. This is no doubt due to an error in the design and build of the mechanism. That said, I would really like to do a software delay rather than a rebuild of the door. I added a function – stopCloseCoopDoorMotorB() that is called from the closeCoopDoorMotorB() function. I have tried several coding delays including a do/while loop (not sure I did this properly), a for loop and most recently millis. Obviously, I'm not doing something correctly as the code below and my other trials do not delay at all – the motor stops immediately. I have researched this problem and have many code snippets recorded that have not worked. I hate to ask for help, but I give up! I tried to copy the entire program – too long - it is 252 lines?
Here are the variables for the millis delay:
//CloseCoopDoorMotorB delay
unsigned long lastCloseCoopDoorMotorBTime = 0;
unsigned long CloseCoopDoorMotorBDelay = 4000; // 4 seconds
Here are the functions in question:
// stop the coop close door motor
void stopCloseCoopDoorMotorB()
{
digitalRead (bottomSwitchPinVal);
if (bottomSwitchPinVal == 0)
{
if ((unsigned long)(millis() - lastCloseCoopDoorMotorBTime) >= CloseCoopDoorMotorBDelay)
{
lastCloseCoopDoorMotorBTime = millis();
}
{
digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
digitalWrite (enableCoopDoorMotorB, LOW); // enable motor, 0 speed
}
}
}
void closeCoopDoorMotorB() // close the coop door motor (motor dir close = clockwise)
{
digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
digitalWrite (enableCoopDoorMotorB, HIGH); // enable motor, full speed
if (bottomSwitchPinVal == 0)
{
stopCloseCoopDoorMotorB();
}
}
Google + link to photos of my controller board (this is on my desk while I'm working on the code).
Arduino Pro Mini 5 volt 16 MHz, SN754410 Motor control IC, 7805 voltage regulator. CdS LDR is connected upper right. White alligator clips simulate the top reed switch, yellow, the bottom. There is a small 5 volt motor for trial use at the bottom of the left picture – in the Coop Door, I use a 24 volt gear motor.