// https://forum.arduino.cc/t/running-two-stepper-motors-at-once/1115352
#include <AccelStepper.h>
#define dirPinLeft 7 /// direction left
#define stepPinLeft 8 /// step left
#define motorInterfaceType 1 /// bipolar motor
AccelStepper stepperLeft = AccelStepper(motorInterfaceType, stepPinLeft, dirPinLeft);
const int enPin1 = 9; /// enable pin 1
int processEnablerStateVariableCount = 5; // global state variable for remembering how many times to go
void setup() {
pinMode(enPin1, INPUT);
digitalWrite(enPin1, HIGH); /// disable motor control
stepperLeft.setMaxSpeed(100); /// slow is 100, fast is 900000
stepperLeft.setAcceleration(50);
stepperLeft.moveTo(15);
}
void loop() {
slowMove1();
}
void slowMove1() { /// motors are NOT disabled at end of function
digitalWrite(enPin1, LOW); /// enable motor control
if (stepperLeft.distanceToGo() == 0 && processEnablerStateVariableCount > 0) {
stepperLeft.moveTo(-stepperLeft.currentPosition());
--processEnablerStateVariableCount; /// reduce the count by one
}
stepperLeft.run();
digitalWrite(enPin1, HIGH); /// disable motor control
}