Hi all,
Disclaimer: this is my first post for my first project. Please forgive any forum etiquette transgressions or noobish questions.
I'm trying to make a vertically sliding door using a stepper motor. While it will mostly use a photoresistor to operate, I would like to put a button on it to open/close it.
Ideally the process would be Push button -> servo unlocks door position -> stepper motor opens door -> servo locks door position and a second button push would be Push button -> servo unlocks door position -> stepper motor closes door -> servo locks door position.
So far I've gotten a button push to operate the servo to unlock the door position and the stepper motor opens the door, but then it turns back to close the door before the servo locks it again. This tells me that the wiring is fine but I need a better way to separate the "Open" and "Close" commands. I am unsure how to do so. Here is the snippet of the section that I believe is causing me trouble:
#include <Stepper.h>
#include <Servo.h>
Stepper motor(200,2,3,4,5); //The stepper motor is connected via a TB6612
Servo servo;
const int doorOpenClose = 13; //Button on pin 13
int buttonStatusDoor = 0;
int pos = 45; //Servo position
int doorStatus = 0; //Door position (1 = open, 2 = closed)
int doorMoving = 0; //I don't want a second button press to cause the stepper to reverse and lose step count, this is to stop that
void setup()
{
pinMode(doorOpenClose,INPUT);
servo.attach(6);
Serial.begin(9600);
motor.setSpeed(60);
}
void loop()
{
buttonStatusDoor = digitalRead(doorOpenClose);
if (buttonStatusDoor == LOW) //This works fine, the button press is registering
{
if (doorStatus == 1 and doorMoving == 0) //If the door is open and not moving
{
doorMoving = 1; //Door is moving, button pushes now don't register
servo.write(135); //Unlocks position
delay(300);
motor.step(-100); //Steps in closing direction (test number, don't worry)
delay(100);
servo.write(45); //Locks door position
doorStatus = 0; //Door position is saved as closed
doorMoving = 0; //We can push the button again and have it respond
}
if (doorStatus == 0 and doorMoving == 0) //If the door is closed and not moving
{
doorMoving = 1; //Door is moving
servo.write(135); //Unlocks position
delay(300);
motor.step(100); //Steps to open door
delay(100);
servo.write(45); //Locks position
doorStatus = 1; //Door position is saved as open
doorMoving = 0; //Button pushes will register again
}
}
}
I'm not sure if the doorStatus value change is causing it to open then close or if my if statements need changed. Any help (with the specified problem or general coding issues you may see) would be greatly appreciated. Thanks!