/*
Forum: https://forum.arduino.cc/t/stepper-motor-cancellation/1213693
Wokwi: https://wokwi.com/projects/387464144368906241
*/
#include <Stepper.h>
int stepsPerRevolution = 2048;
int motSpeed = 50;
int dt = 500;
const byte button1Pin = 6;
const byte button2Pin = 7;
int motDir = 1;
bool motRun = false;
Stepper myStepper(stepsPerRevolution, 2, 4, 3, 5);
void setup()
{
Serial.begin(9600);
myStepper.setSpeed(motSpeed);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
}
void loop()
{
bool button1State = digitalRead(button1Pin);
motRun = (button1State == LOW);
while (motRun){
bool button2State = digitalRead(button2Pin);
myStepper.step(motDir * 1);
delayMicroseconds(dt);
motRun = (button2State == HIGH);
}
}