I'm trying to get a stepper to run on my teensy 4.1 using the accelstepper library. The motor has to move up or down a certain amount depending on which key is pressed and stop if the estop button is pressed. I know the stepper.run() command has to be called continuously to move so i placed it in a while loop with conditions. Unfortunately whenever i press a button nothing happens. I added serial communication to try and troubleshoot the issue. The while function is called and the teensy thinks the motor is running since it will returne "done" after a few seconds. So far it seems the problem is with the while loop. The motor moves just fine if placed in the void loop but doesnt inside a while loop, even when theres no digital read condition (while is always == 1). Is there something I'm missing here? I've been able to run accelstepper code in a while loop before on an arduino mega, but for some reason it wont work on a teensy. I've even changed the minimum pulse width and clock speeds in case it was a speed issue but still no luck.
Code is attached below:
Code is attached below:
Code:
#include <AccelStepper.h>
AccelStepper stepper(1,6,5); //step = 6, dir = 5
int upp = 0; //not used
int downn = 0; //not used
int estop = 0; //not used
const int up = 30;
const int down = 9;
const int stopp = 24;
const int enablee = 4;
void setup() {
stepper.setMaxSpeed(500);
stepper.setAcceleration(100);
stepper.setMinPulseWidth(50); //for troubleshooting
pinMode(up, INPUT);
pinMode(down, INPUT);
pinMode(stopp, INPUT);
pinMode(enablee, OUTPUT); //not used
Serial.begin(9600);
}
void loop() {
if (digitalRead(up)==HIGH) {
Serial.println("GOING UP");
stepper.move(5000); //set relative distance
while (stepper.isRunning()==1 && digitalRead(stopp)==0) {
stepper.run(); //keep running until conditions are met
}
Serial.println("DONE");
}
if (digitalRead(down)==HIGH) {
Serial.println("GOING DOWN");
stepper.move(-5000); //set relative distance
while (stepper.isRunning()==1 && digitalRead(stopp)==0) {
stepper.run(); //keep running until conditions are met
}
Serial.println("DONE");
}
}