#include <AccelStepper.h>
#define dirPinLeft 7 // direction left
#define stepPinLeft 8 // step left
#define dirPinRight 3 // direction right
#define stepPinRight 4 // step right
#define motorInterfaceType 1 // bipolar motor
AccelStepper stepperLeft = AccelStepper(motorInterfaceType, stepPinLeft, dirPinLeft);
AccelStepper stepperRight = AccelStepper(motorInterfaceType, stepPinRight, dirPinRight);
const int enPin1 = 9; // enable pin 1
const int enPin2 = 10; // enable pin 2
int processEnablerStateVariableCount = 6; // global state variable for remembering how many times to go
void setup() {
Serial.begin(9600);
pinMode(enPin1, INPUT);
pinMode(enPin2, INPUT);
digitalWrite(enPin1, HIGH); /// disable motor control
digitalWrite(enPin2, HIGH); /// disable motor control
stepperLeft.setMaxSpeed(900);
stepperLeft.setAcceleration(100);
stepperLeft.moveTo(15);
stepperRight.setMaxSpeed(900);
stepperRight.setAcceleration(100);
stepperRight.moveTo(15);
}
void loop() {
moveSteppers();
}
void moveSteppers() {
if (processEnablerStateVariableCount > 0) {
digitalWrite(enPin1, LOW); /// enable motor control
digitalWrite(enPin2, LOW); /// enable motor control
if (stepperRight.distanceToGo() == 0) {
stepperRight.moveTo(-stepperRight.currentPosition());
}
stepperRight.run();
if (stepperLeft.distanceToGo() == 0 && processEnablerStateVariableCount > 0) {
stepperLeft.moveTo(-stepperLeft.currentPosition());
--processEnablerStateVariableCount; /// reduce the count by one
}
stepperLeft.run();
}
digitalWrite(enPin1, HIGH); /// disable motor control
digitalWrite(enPin2, HIGH); /// disable motor control
}