Please review my code (AccelStepper; ESP32)

Hello everyone, I'm a noob and need some help.
I am currently working on a desk with adjustable hight.
Hardware:
ESP32
2 Nema 23 Steppers with breaks
external drivers

I melted my ESP32 today because I connected it wrong :upside_down_face: ... so I'm unable to test this code.

#include <AccelStepper.h>

// Define pins for motor 1
#define DIR_1_PIN 13
#define STEP_1_PIN 12
#define ENABLE_1_PIN 14
#define BRAKE_1_PIN 23
#define END_1_PIN_1 0
#define END_1_PIN_2 4

// Define pins for motor 2
#define DIR_2_PIN 27
#define STEP_2_PIN 26
#define ENABLE_2_PIN 25
#define BRAKE_2_PIN 22
#define END_2_PIN_1 16
#define END_2_PIN_2 17

// Define pins for buttons
#define UP_BUTTON_PIN 2
#define DOWN_BUTTON_PIN 15

// Define motor speeds
#define MAX_SPEED 1000
#define ACCEL 500

// Create stepper motor objects
AccelStepper motor1(AccelStepper::DRIVER, STEP_1_PIN, DIR_1_PIN);
AccelStepper motor2(AccelStepper::DRIVER, STEP_2_PIN, DIR_2_PIN);

void setup() {
  // Set pin modes
  pinMode(ENABLE_1_PIN, OUTPUT);
  pinMode(ENABLE_2_PIN, OUTPUT);
  pinMode(BRAKE_1_PIN, OUTPUT);
  pinMode(BRAKE_2_PIN, OUTPUT);
  pinMode(END_1_PIN_1, INPUT_PULLUP);
  pinMode(END_1_PIN_2, INPUT_PULLUP);
  pinMode(END_2_PIN_1, INPUT_PULLUP);
  pinMode(END_2_PIN_2, INPUT_PULLUP);
  pinMode(UP_BUTTON_PIN, INPUT_PULLUP);
  pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);

  // Set initial motor speeds and accelerations
  motor1.setMaxSpeed(MAX_SPEED);
  motor2.setMaxSpeed(MAX_SPEED);
  motor1.setAcceleration(ACCEL);
  motor2.setAcceleration(ACCEL);
}

void loop() {
  // Check endstop switches and stop motors if they are hit
  if (!digitalRead(END_1_PIN_1) || !digitalRead(END_1_PIN_2)) {
    motor1.stop();
  }
  if (!digitalRead(END_2_PIN_1) || !digitalRead(END_2_PIN_2)) {
    motor2.stop();
  }

  // Check button states and move motors accordingly
  if (digitalRead(UP_BUTTON_PIN) == LOW) {
    moveMotor1(MAX_SPEED);
    moveMotor2(MAX_SPEED);  // Hochfahren
  }
  if (digitalRead(DOWN_BUTTON_PIN) == LOW) {
    moveMotor1(-MAX_SPEED);
    moveMotor2(-MAX_SPEED);  // Herunterfahren
  }

  // Run motors
  motor1.runSpeed();
  motor2.runSpeed();
}

void moveMotor1(int dir) {
  // Enable motor
  digitalWrite(ENABLE_1_PIN, HIGH);

  // Release brake
  digitalWrite(BRAKE_1_PIN, HIGH);

  // Set motor direction
  motor1.setSpeed(dir);

  // Move motor until endstop is hit or button is released
  while ((dir == MAX_SPEED && digitalRead(END_1_PIN_1) == HIGH ) || (dir == -MAX_SPEED && digitalRead(END_1_PIN_2) == HIGH ) || digitalRead(UP_BUTTON_PIN) == LOW) {
    motor1.runSpeed();
  }

  // Stop motor
  motor1.stop();

  // Apply brake
  digitalWrite(BRAKE_1_PIN, LOW);

  // Disable motor
  digitalWrite(ENABLE_1_PIN, LOW);
}

void moveMotor2(int dir) {
  // Enable motor
  digitalWrite(ENABLE_2_PIN, HIGH);

  // Release brake
  digitalWrite(BRAKE_2_PIN, HIGH);

  // Set motor direction
  motor2.setSpeed(dir);

  // Move motor until endstop is hit or button is released
  while ((dir == MAX_SPEED && digitalRead(END_2_PIN_1) == HIGH ) || (dir == -MAX_SPEED && digitalRead(END_2_PIN_2) == HIGH ) || digitalRead(DOWN_BUTTON_PIN) == LOW) {
    motor2.runSpeed();
  }

  // Stop motor
  motor2.stop();

  // Apply brake
  digitalWrite(BRAKE_2_PIN, LOW);

  // Disable motor
  digitalWrite(ENABLE_2_PIN, LOW);
}

I'd love to hear what you think about it!

Do you mean "brakes"?
Or "breakers"?

electromagnetic brakes

You can test your code with the WOKWI-simulator until your three new ESP32 arrive

you have to add all the wiring
endstops can be simulated by buttons

You should have two ESp32 as spareparts
post a picture of your project. I'm pretty sure that you can improve the setup and the wiring

best regards Stefan

  • loop() checks the end stops, but aren't they also tested in your moveMotor functions?

  • loop() sequentially calls moveMotor1() and then moveMotor2(), but don't these moveMotor function run the motor continuously until the end stops are reached?

  • loop() also calls runSpeed(), but again, isn't runSpeed() called in the moveMotor functions?

  • as mentioned, the moveMotor functions run the motor until the end stops are reached, as long as the DOWN button is pressed (what if your going up), but why is dir also tested?

i would expect that while a button is held down, both brakes and motors should be enabled if not already and Both motors stepped no more than one step at a time if each motors end stop hasn't been reached.

once both end stops have been reached (or should it be either) or the corresponding direction button released, both brakes should be enabled and both motors disabled

the jist is there should be just on moveMotors(), need to recognize when starting, running and stopping.

please ask questions if i'm not clear about something

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