Hi everyone,
First of all, I know that a lot of similar subjects exist but after a lot of searching, I haven't found a really useful post.
I'm trying to use the AccelStepper library to raise and lower a heavy linear rail carriage using 2 push buttons. As long as a button is pressed, the motor accelerates and then rotates in one direction. When the button is released, the motor slows down and stops. Same for the other direction. Enable pin for the driver only when the motor needs to run. (two NC endstops in series with the corresponding buttons for safety)
The problem is that I can't get reliable movements (or movements at all). Either my code gets stuck in a loop (while loop), or the driver just doesn't manage to accelerate the motor. I'm pretty sure i'm using the library wrong... Even though this code must be the easiest thing in the world to write!
Disclaimer : motor and driver work just fine with other test codes, speeds seem high because of microstepping and gearbox. I have the right setup for the driver and enough current for it to work properly. Buttons and endstops are connected with small capacitors for bounces (+ internal pullup). (arduino nano, nema 23 + gearbox, tb6600 driver, 12V-5A power supply)
Anyway, here are my 2 main codes with 2 different approaches :
CODE 1
#include <AccelStepper.h>
#define dirPin 8
#define stepPin 9
#define enablePin 10
// Define button pins
#define buttonPinUP 2
#define buttonPinDOWN 3
// Endstops in series with corresponding button
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);
// Define variables for button state and motor direction control
// /!\ false = triggered !!!!!!
bool buttonUpState = true;
bool buttonDownState = true;
int speed = 1500;
int maxSpeed = 2000;
int acceleration = 500;
void setup() {
myStepper.setPinsInverted(false, false, true); // setInverted can be used here if pins are inverted
myStepper.setEnablePin(enablePin);
myStepper.setMinPulseWidth(20); // seems like tb6600 works better with this
myStepper.setMaxSpeed(maxSpeed);
myStepper.setAcceleration(acceleration);
//pinMode(enablePin, OUTPUT);
myStepper.disableOutputs();
delay(200);
myStepper.enableOutputs();
pinMode(buttonPinUP, INPUT_PULLUP);
pinMode(buttonPinDOWN, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
delay(200);
}
void loop() {
buttonUpState = digitalRead(buttonPinUP);
buttonDownState = digitalRead(buttonPinDOWN);
if (buttonUpState == false || buttonDownState == false){
myStepper.enableOutputs();
digitalWrite(ledPin, HIGH);
}
else if(!myStepper.run()) {
myStepper.disableOutputs();
digitalWrite(ledPin, LOW);
}
while(buttonUpState == false){
myStepper.setSpeed(-speed);
myStepper.run();
}
while(buttonDownState == false){
myStepper.setSpeed(speed);
myStepper.run();
}
myStepper.stop();
}
*Works sometimes but I have no acceleration because of setSpeed() and if I test the outputs, I'm often stuck in the while instruction.
CODE 2
#include <AccelStepper.h>
#define dirPin 8
#define stepPin 9
#define enablePin 10
// Define button pins
#define buttonPinUP 2
#define buttonPinDOWN 3
// Endstops in series with corresponding button
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);
// Define variables for button state and motor direction control
// /!\ false = triggered !!!!!!
bool buttonUpState = true;
bool buttonDownState = true;
//int speed = 1500;
int maxSpeed = 2000;
int acceleration = 500;
void setup() {
myStepper.setPinsInverted(false, false, true); // setInverted can be used here if pins are inverted
myStepper.setEnablePin(enablePin);
myStepper.setMinPulseWidth(20); // seems like tb6600 works better with this
myStepper.setMaxSpeed(maxSpeed);
myStepper.setAcceleration(acceleration);
//pinMode(enablePin, OUTPUT);
myStepper.disableOutputs();
delay(200);
myStepper.enableOutputs();
pinMode(buttonPinUP, INPUT_PULLUP);
pinMode(buttonPinDOWN, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
delay(200);
}
void loop() {
buttonUpState = digitalRead(buttonPinUP);
buttonDownState = digitalRead(buttonPinDOWN);
/*if (buttonUpState == false || buttonDownState == false){
myStepper.enableOutputs();
digitalWrite(ledPin, HIGH);
}
else if(!myStepper.run() && ledPin == HIGH) {
myStepper.disableOutputs();
digitalWrite(ledPin, LOW);
}*/
if (! buttonUpState){
myStepper.move(-10);
//myStepper.run();
digitalWrite(ledPin, HIGH);
}
if (! buttonDownState){
myStepper.moveTo(10);
//myStepper.run();
digitalWrite(ledPin, HIGH);
}
else{
myStepper.stop();
digitalWrite(ledPin, LOW);
}
myStepper.run();
}
Doesn't work at all even if it looks dead easy. Should get the acceleration and maxSpeed right.
I'd be eternally grateful if you could tell me where I'm going wrong, or if you have a better approach to keep this code as simple and reliable as possible. Thanks in advance.