Limit switch with stepper

I am having a lot of issues on this project for my brother, basically we have a pocket door in his basement to his "man cave". We have tied a stepper motor to the door with a timing belt. At each end of the door opening is a limit switch, one for CLOSED position and OPEN position. He wants a RFID scanner on the outside to open it, and a push button on the inside that will close or open the door. After the door is opening from inside or outside, after a few seconds the door closes automatically. the motor has an acceleration coded in, but the number of steps to the OPEN/CLOSED positions aren't the same, so limit switches with do a quick stop for the motor. I hope this makes sense. hope we can find a solution, my code gets stuck, it'll open the door but won't stop at the limit switch and won't leave that section of code. we also have a push button in place of the RFID for a temporary set up

#include "SPI.h"
#include "MFRC522.h"
#include <AccelStepper.h>
#define SS_PIN 9
#define RST_PIN 8
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
int limit1;
int limit2;
int debounce = 200;
int enable;
int wiperRelay = 16;
int redButton = 20;
int doorButton;
int limit1State = 0;
int limit2State = 0;
int redButtonState = 0;
int doorButtonState = 0;
int elapsedTime = 10000;

void setup() {
  stepper.setMaxSpeed(1000000);
  stepper.setAcceleration(6000);
  delay(1000);
  Serial.begin(9600);
  SPI.begin();
  rfid.PCD_Init();
  pinMode(limit1, INPUT);
  pinMode(limit2, INPUT);
  pinMode(wiperRelay, OUTPUT);
  pinMode(redButton, INPUT);
  pinMode(doorButton, INPUT);
}

void loop() {
  if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
    return;

    // Serial.print(F("PICC type: "));
    MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
    // Serial.println(rfid.PICC_GetTypeName(piccType));

    // Check is the PICC of Classic MIFARE type
    if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
        piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
        piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
      Serial.println(F("Your tag is not of type MIFARE Classic."));
      return;
    }
    String strID = "";
    for (byte i = 0; i < 4; i++) {
      strID +=
        (rfid.uid.uidByte[i] < 0x10 ? "0" : "") +
        String(rfid.uid.uidByte[i], HEX) +
        (i != 3 ? ":" : "");
    }

    strID.toUpperCase();
    Serial.print("Tap card key: ");
    Serial.println(strID);
    delay(1000);

    if (strID.indexOf("20:CC:6F:85") >= 0) {  //put your own tap card key;
      Serial.println("********************");
      Serial.println("**Authorised access**");
      Serial.println("********************");
      DoorAccess;
      return;
    }
    else {
      Serial.println("****************");
      Serial.println("**Access denied**");
      Serial.println("****************");
      !DoorAccess;
      return;
    }
  }

  redButtonState = digitalRead(redButton);
  if (redButtonState == HIGH) {
    digitalWrite(wiperRelay, LOW);
    delay(100);
    digitalWrite(wiperRelay, HIGH);
    DoorAccess;
  } else {
    // turn LED off:
    digitalWrite(wiperRelay, HIGH);
  }


  doorButtonState = digitalRead(doorButton);
  if (doorButtonState == HIGH > debounce) {
    DoorAccess;
  }

  limit2State = digitalRead(limit2);
  if (limit2State == HIGH > elapsedTime) {
    DoorAccess;
  }

}
void DoorAccess() {
  limit1State = digitalRead(limit1);
  if (limit1State == HIGH > debounce) {
    enable = LOW;
    Serial.println("Door opening");
    stepper.moveTo(60000);
    stepper.runToPosition();
    limit2State = digitalRead(limit2);
    while (limit2State == HIGH > debounce) {
      stepper.stop();
      stepper.runToPosition();
      enable = HIGH;
      Serial.println("Door opened");
    }

  }
  else if (limit2 == HIGH > debounce) {
    enable = LOW;
    Serial.println("Door closing");
    stepper.moveTo(-60000);
    stepper.runToPosition();
    while (limit1 == HIGH > debounce) {
      stepper.stop();
      stepper.runToPosition();
      enable = HIGH;
      Serial.println("Door closed");
    }
  }
}

I've added random serial prints in the code to see in the monitor where my code goes and gets stuck.

#include "SPI.h"
#include "MFRC522.h"
#include <AccelStepper.h>
#define SS_PIN 9
#define RST_PIN 8
#define dirPin 15
#define stepPin 14
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
const int doorButton = 19;
const int limit1 = 18;
const int limit2 = 17;
const int enable = 23;
int limit1State;
int limit2State;
int doorButtonState;
int lastDoorButtonState = LOW;
int lastlimit1State = LOW;
int lastlimit2State = LOW;
unsigned long lastDoorDebounceTime = 0;
unsigned long lastlimit1Time = 0;
unsigned long lastlimit2Time = 0;
unsigned long debounceDelay = 50;
unsigned long limit1Delay = 50;
unsigned long limit2Delay = 50;

void setup() {
  stepper.setMaxSpeed(1000000);
  stepper.setAcceleration(6000);
  pinMode(doorButton, INPUT);
  pinMode(limit1, INPUT);
  pinMode(limit2, INPUT);
  pinMode(enable, OUTPUT);
  digitalWrite(enable, HIGH);
  Serial.begin(9600);
}

void loop() {
  Serial.println("1");
  int doorReading = digitalRead(doorButton);
  
  Serial.println("2");
  
  if (doorReading != lastDoorButtonState) {
    lastDoorDebounceTime = millis();
  }

    Serial.println("3");
    
  if ((millis() - lastDoorDebounceTime) > debounceDelay) {
    if (doorReading != doorButtonState) {
      doorButtonState = doorReading;
      if (doorButtonState == HIGH) {
        int limit1Reading = digitalRead(limit1);
        int limit2Reading = digitalRead(limit2);
        if (limit1Reading != lastlimit1State) {
          lastlimit1Time = millis();

            Serial.println("4");
            
        }
        if ((millis() - lastlimit1Time) > limit1Delay) {
          if (limit1Reading != limit1State) {
            limit1State = limit1Reading;
            if (limit1State == HIGH) {
              digitalWrite(enable, LOW);
              Serial.println("Door opening");
              stepper.moveTo(80000);
              stepper.runToPosition();
              int limit2Reading = digitalRead(limit2);
              if (limit2Reading != lastlimit2State) {
                lastlimit2Time = millis();
                  Serial.println("5");
              }
              if ((millis() - lastlimit2Time) > limit2Delay) {
                  Serial.println("6");
                if (limit2Reading != limit2State) {
                    Serial.println("7");
                  limit2State = limit2Reading;
                  while (limit2State == HIGH) {
                    stepper.stop();
                    stepper.runToPosition();
                    digitalWrite(enable, HIGH);
                      Serial.println("6");
                    Serial.println("Door opened");
                  }
                }
              }
            }
          }
        }
        lastlimit1State = limit1Reading;
        lastlimit2State = limit2Reading;
        if (limit2Reading != lastlimit2State) {
          lastlimit2Time = millis();
        }
        if ((millis() - lastlimit2Time) > limit2Delay) {
          if (limit2Reading != limit2State) {
            limit2State = limit2Reading;
            if (limit2State == HIGH) {
              digitalWrite(enable, LOW);
              Serial.println("Door closing");
              stepper.moveTo(-60000);
              stepper.runToPosition();
              while (limit1Reading != lastlimit1State) {
                lastlimit1Time = millis();
              }
              if ((millis() - lastlimit1Time) > limit1Delay) {
                if (limit1Reading != limit1State) {
                  limit1State = limit1Reading;
                  if (limit1State == HIGH) {
                    stepper.stop();
                    stepper.runToPosition();
                    digitalWrite(enable, HIGH);
                    Serial.println("Door closed");
                  }
                }
              }
            }
          }
        }
        lastlimit1State = limit1Reading;
        lastlimit2State = limit2Reading;
      }
    }
  }
  lastDoorButtonState = doorReading;
}

while (limit2State == HIGH)
{
stepper.stop();
stepper.runToPosition();
digitalWrite(enable, HIGH);
Serial.println("6");
Serial.println("Door opened");
}

How you get out of this while loop ?

What's the mean of this condition?

infinite loop

infinite loop

Im very new/rustic at coding, so I was under the impression after if completed the initial reading it exited the loop

while (. . . )
{
. . .
}

At the line with the } brace, you go back to while ( . . .)


Only use while( ) loops if you know they will execute very very fast.

You keep updating the limit2State value within the while-loop, or use a break statement.

so how should I fix this?

You put the above line of code inside your "while" loop, so the controlling value is changed.

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