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 ... 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!