Motor won't stop running when triggered by alarm

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:

  1. Morning alarm triggers motor to turn clockwise, opening door (WORKING)
  2. Door hits the switch, stops the motor and holds (WORKING)
  3. Evening alarm triggers the motor to turn counter-clockwise, closing door (WORKING)
  4. 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);
}

I don't know anything about the Alarm library you are using but I suspect that you should not be using delay() for anything because the Arduino can do nothing while a delay() is running.

Why not use the Alarm library to manage that time interval?

Or use millis() as illustrated in several things at a time. But it may not be easy to integrate that with your Alarm library. Personally, I would use millis() for timing everything.

But, perhaps the problem is as simple as the fact that you have no code to turn off the motor after the delay() has expired.

...R

I don't know anything about the Alarm library you are using but I suspect that you should not be using delay() for anything because the Arduino can do nothing while a delay() is running.

Well, I do, and you are correct that delay() should not be used, but for the wrong reason.

You MUST use Alarm.delay() EVERY time you need to delay (though running a motor blindly for 5 seconds is stupid).