Hi everyone. I'm a noob, I'm 99% finished my first project but I'm stuck with one problem I can't seem to find answers to by myself, on this forum or others. I'm making an automatic chicken coop door that is timed with an RTC and has alarms to run a motor in one direction to lift the door open and the reverse direction to drop the door closed. I also have a lever micro switch to stop the door at a certain point when its being lifted open. In order to close the door I'm trying to just time the duration of the motor but despite my best efforts the motor doesn't seem to be obeying the 'delay' command and just keeps on going indefinitely.
Quick Summary of Current Status:
- Morning alarm triggers motor to turn clockwise, opening door (WORKING)
- Door hits the switch, stops the motor and holds (WORKING)
- Evening alarm triggers the motor to turn counter-clockwise, closing door (WORKING)
- At this point I want to use a delay command on the motor for X sec. until the door is fully closed (NOT WORKING, KEEPS RUNNING)
I'd be very thankful of any help. Attached is the code so far.
#include <Time.h>
#include <TimeAlarms.h>
void setup()
{
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(2, INPUT_PULLUP); // Initiates Switch pin
Serial.begin(9600);
setTime(8,29,0,1,1,11); // Set RTC to Saturday 8:29:00am Jan 1 2011
Alarm.alarmRepeat(8,29,10, MorningAlarm); // Door Opens @ 8:30am every day
Alarm.alarmRepeat(8,29,25, EveningAlarm); // Door Closes @ 8:31am every day
}
void loop(){
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
int sensorVal = digitalRead(2);
Serial.print(sensorVal);
if (sensorVal == HIGH ) { //Normally Open Switch Closed
digitalWrite(9, HIGH); //Engage the Brake for Channel A
} else { //Normally Open Switch Open
digitalWrite(9, LOW); //Disengage the Brake for Channel A
}
}
// functions to be called when an alarm triggers:
void MorningAlarm(){ //Door Opens
digitalWrite(12, HIGH); //Winding Spool = HIGH, Unwinding Spool = LOW
digitalWrite(9, LOW); //Engage Brake = HIGH, Disengage Brake = LOW
analogWrite(3, 40); // Speed 0-255
}
void EveningAlarm(){ //Door Closes
int sensorVal = digitalRead(2);
if (sensorVal == HIGH ) { //Switch Pushed
digitalWrite(12, LOW); //Winding Spool = HIGH, Unwinding Spool = LOW
digitalWrite(9, LOW); //Engage Brake = HIGH, Disengage Brake = LOW
analogWrite(3, 40); //Speed 0-255
delay(5000); //THIS IS WHAT ISN'T WORKING
}
}
void digitalClockDisplay()
{
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}