Hi folks,
I'm trying to motorise a wardrobe setup and have prepared the hardware part, but now I'm somewhat stuck on the programming task. I'm an architect, so programming is something new to me, but I'm trying to understand it as much as possible and trying to learn and watch tons of videos.
I have this schematic of how I have everything connected. Right now I'm doing the test part and I'm working my way through simpler code to more complex one.
I managed to write code, that moves both motors while holding any of the buttons. If I release the button and hold it again, the direction is reversed. If the endstop is being touched, it stops both motors and starts moving in the opposite direction with a small delay (so I would be able to release the button and the "home" position). This was pretty ok and fun, but now I'm at the trickier part.
I'm adding the third moving endstop (it is moving with wardrobe 2) and I have to come up with some logic on how to move the wardrobes. My first intention was to move one wardrobe at a time (to not put that much load on the power supply). When I press the button it should start moving W1 or W2 based on the last direction (the direction changes with each button press). Then it hits one of the end stops (L or R) and then it should start moving the other wardrobe to the Middle endstop. If Middle and L/R are pressed it has to move to the non-activated endstop and after hitting it it stops or if I still hold the button it has to start moving the other wardrobe. If just L/R is activated, it should move the other wardrobe to the M stop.
There are a lot of other problems I guess, but I don't have any idea, how to handle this kind of logic/variable-based code.
Do you have some inspiration, that I could read/watch to find some solution for this?
Here is the code, that I managed to test with the motors and end stops.
And pictures of the setup of the motor, some parts are missing, but I will add those once I install it in place.
Thank you for any advice or good word, I appreciate it. ![]()
```cpp
#include <AccelStepper.h>
// Buttons
#define BTN_1 4 // Button - entrance (to bedroom)
#define BTN_2 5 // Button - exit (from bedroom)
#define BTN_3 6 // Button - hideout
// Endstops
#define END_L A0 // Endstop - left
#define END_R A2 // Endstop - right
// Stepper motor pins for the left stepper
#define STEP_L 7 // Define step pin (pulse pin) for the left stepper motor
#define DIR_L 8 // Define direction pin for the left stepper motor
#define ENA_L 9 // Define enable pin for the left stepper motor
// Stepper motor pins for the right stepper
#define STEP_R 10 // Define step pin (pulse pin) for the right stepper motor
#define DIR_R 11 // Define direction pin for the right stepper motor
#define ENA_R 12 // Define enable pin for the right stepper motor
// Alarm pin connected from both stepper drivers - NOT USING THOSE YET
#define ALM 13
// SSR for reseting power to the power supply of stepper drivers - NOT USING THOSE YET
#define SSR A3
AccelStepper stepperL(AccelStepper::DRIVER, STEP_L, DIR_L);
AccelStepper stepperR(AccelStepper::DRIVER, STEP_R, DIR_R);
// Variables - button states and endstop states
bool btn1_state = false; // actual button state
bool btn2_state = false; // actual button state
bool btn3_state = false; // actual button state
bool lastBtn1_state = false; // previous button state
bool lastBtn2_state = false; // previous button state
bool lastBtn3_state = false; // previous button state
bool END_L_STATE = false; // actual endstop state
bool END_R_STATE = false; // actual endstop state
bool lastEND_L_STATE = false; // previous endstop state
bool lastEND_R_STATE = false; // previous endstop state
bool direction = true; // true = right, false = left
bool waitingForDelay = false; // flag to check if waiting for delay
unsigned long endstopHitTime = 0;
const unsigned long endstopDelay = 700; // Delay in milliseconds
void setup() {
// Initialize the buttons
pinMode(BTN_1, INPUT_PULLUP);
pinMode(BTN_2, INPUT_PULLUP);
pinMode(BTN_3, INPUT_PULLUP);
// Initialize the endstops
pinMode(END_L, INPUT_PULLUP);
pinMode(END_R, INPUT_PULLUP);
// Initialize the left stepper motor
stepperL.setMaxSpeed(400); // Set maximum speed
stepperL.setAcceleration(100); // Set acceleration
stepperL.setEnablePin(ENA_L);
stepperL.setPinsInverted(false, false, true);
stepperL.disableOutputs();
// Initialize the right stepper motor
stepperR.setMaxSpeed(400);
stepperR.setAcceleration(100);
stepperR.setEnablePin(ENA_R);
stepperR.setPinsInverted(false, false, true);
stepperR.disableOutputs();
}
void loop() {
// Update button and endstop states
updateButtonStates();
updateEndstopStates();
unsigned long currentMillis = millis(); // Save current millis to be able to add delay after hitting endstop and reversing movement
if (waitingForDelay) {
// Check if the delay period has passed
if (currentMillis - endstopHitTime >= endstopDelay) {
waitingForDelay = false;
} else {
return; // Exit the loop until delay period has passed
}
}
// Check if any button is pressed
if (btn1_state || btn2_state || btn3_state) {
// Enable the stepper motors
stepperL.enableOutputs();
stepperR.enableOutputs();
// Move the motors in the current direction
if (direction) {
if (!END_R_STATE) { // Allow movement if right endstop is not triggered
stepperL.moveTo(stepperL.currentPosition() + 10000); // Greater movement then current position and physicall limitations (
stepperR.moveTo(stepperR.currentPosition() + 10000); // will be adjusted to be sligtly more then real physicall limitations)
}
} else {
if (!END_L_STATE) { // Allow movement if left endstop is not triggered
stepperL.moveTo(stepperL.currentPosition() - 10000);
stepperR.moveTo(stepperR.currentPosition() - 10000);
}
// Run the motors while any button is pressed
while ((btn1_state || btn2_state || btn3_state)) {
stepperL.run();
stepperR.run();
// Update button and endstop states
updateButtonStates();
updateEndstopStates();
// Check if an endstop is hit
if (END_L_STATE && !direction) {
// Left endstop reached and moving left
direction = true; // Change direction to right
stepperL.setCurrentPosition(0); // Reset position
stepperR.setCurrentPosition(0); // Reset position
endstopHitTime = millis();
waitingForDelay = true;
break;
}
if (END_R_STATE && direction) {
// Right endstop reached and moving right
direction = false; // Change direction to left
stepperL.setCurrentPosition(0); // Reset position
stepperR.setCurrentPosition(0); // Reset position
endstopHitTime = millis();
waitingForDelay = true;
break;
}
}
// Disable the stepper motors to save power
stepperL.disableOutputs();
stepperR.disableOutputs();
} else {
// Reset positions to 0 when no button is pressed
direction = !direction; // Switch the direction for the next press
stepperL.setCurrentPosition(0);
stepperR.setCurrentPosition(0);
}
}
void updateButtonStates() {
lastBtn1_state = btn1_state;
lastBtn2_state = btn2_state;
lastBtn3_state = btn3_state;
btn1_state = (digitalRead(BTN_1) == LOW);
btn2_state = (digitalRead(BTN_2) == LOW);
btn3_state = (digitalRead(BTN_3) == LOW);
}
void updateEndstopStates() {
lastEND_L_STATE = END_L_STATE;
lastEND_R_STATE = END_R_STATE;
END_L_STATE = (digitalRead(END_L) == LOW);
END_R_STATE = (digitalRead(END_R) == LOW);
}







