Cipriana
Cipriana

Reputation: 13

How do you use the accelstepper arduino library to move a specified number of steps, check an external input, then continue?

I'm trying to use the Accelstepper library to run my stepper motor. My objective is to run the motor for a specific number of steps, check to see if an external switch is pressed, and then continue at a constant speed. However, I have found that I cannot specify a number of steps and then run at a constant speed afterwards.

My current code runs a while loop and runs for the number of steps that I specify while ignoring any code regarding my switch.

motor.setCurrentPosition(0);
while(motor.currentPosition()!=50){
  motor.setSpeed(500);
  motor.runSpeed();
}
delay(1000);
if (digitalRead(switchPin)==LOW){
  motor.setSpeed(500);
  motor.runSpeed();
}

Upvotes: 1

Views: 2880

Answers (1)

Swedgin
Swedgin

Reputation: 903

You need to put the last motor.runSpeed() in an infinite loop. Now it gets executed only once if switchpin is low. After that, the program exits the if condition and terminates.

motor.setSpeed(500);
motor.setCurrentPosition(0);

while(motor.currentPosition()!=50){
    motor.runSpeed();
}

delay(1000);

if (digitalRead(switchPin)==LOW){

    while (1) {
        motor.runSpeed();
    }

}

In the while loop you can check another vlag to break out of it, if needed.

Upvotes: 2

Related Questions