How to stop motor if reed switch "stop" is not triggered

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();
}

We would need to see a detailed diagram of how your components are wired together, also a datasheet or description of the various components.

1 Like

1 x Nano Every.
1 x Light-dependent resistor (triggering "coop door open" and "coop door close" events).
2 x Reed Switches (one positioned high and one low) on the coop door. Wires run back to Nano. These are the switches stopping the progress of the door when triggered.
1 x LN 298 Motor Driver controlled by Nano Every running a small motor that opens and closes the coop door as triggered by the light reading on the LDR. Again, reed switches stop the transit of the door.
2 x LEDs to let me know if the door closed or is open.

Stop the motor if the end switch is not triggered after a certain time.
Or add a current sensor in the supply of the motor driver, like an INA219 breakout board.
A blocked motor will draw more current.
Leo..

It does not sound like a complicated task but I cannot see what you have my eyes are not that good. Posting an annotated schematic as to how you have it wired. Include all power, ground and power sources. Also links to Technical Information on each of the hardware parts would help, if it is a link to a marketplace store such as Amazon it is probably sadly lacking in the needed information. I highly recommend you get rid of the L298 motor drives they lose about 3V to your motor terminals. Go through the information you have given will take at least an hour, a schematic less then 5 minutes. In that free time I can help many other users which I will do.

Thank-you. Can you suggest code for "Stop the motor if the end switch is not triggered after a certain time"? This is precisely what I think I need. Thanks for the idea of the INA219.

Would this work? So the a bottomSwitchPinVal == 0 will stop the motor but in case the switch does not = 0 (due to coop debris blocking complete travel of the door - chickens are not the tidiest) then a delay followed by a final stop door command will ensure it does stop.

  if (bottomSwitchPinVal == 0) {                         // if bottom reed switch circuit is closed
    stopCoopDoorMotorA();
    delay (300000); //Adds a delay
    stopCoopDoorMotorA(); //Manual stop coop door to ensure door is stopped

Thanks. What motor drivers do you like better?

Motor drivers are application and motor dependent. There is not much more I can do without the needed information.

Time how long the door needs to open/close in normal way. Then make a timer variabele at start and every loop check the timer. Don't use blocking code.

Use NC contacts. If the cable break the motor stops.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.