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