I am attempting to make three steppers stop in succession with each additional button press, and the first and third stepper stop correctly, however when I press the button for the third time (counter % 4 = 3), the second and third steppers both stop. The second stops the way it is supposed to; the third takes a little longer and vibrates once it comes to a stop, only stopping completely on the fourth press.
I am using HW135 EasyDriver and NEMA17 steppers.
#include <AccelStepper.h>
#define stp 3
#define stp2 4
#define stp3 5
#define dir 6
#define dir2 7
#define dir3 8
AccelStepper stepper3(AccelStepper::DRIVER, stp, dir);
AccelStepper stepper1(AccelStepper::DRIVER, stp2, dir2);
AccelStepper stepper2(AccelStepper::DRIVER, stp3, dir3);
int SPR = 1600, decTime = 6000;
int button = 10, Switch = 11;
int Time, timeSince;
int switchVal, buttonVal, lastButtonVal, counter;
int speed = 2500;
int accel = 500;
bool Off = false, midRun = false;
void setup() {
stepper1.setMaxSpeed(speed);
stepper2.setMaxSpeed(speed);
stepper3.setMaxSpeed(speed);
stepper1.setAcceleration(accel);
stepper2.setAcceleration(accel);
stepper3.setAcceleration(accel);
pinMode(button, INPUT);
pinMode(Switch, INPUT_PULLUP);
}
void loop() {
switchVal = digitalRead(Switch);
buttonVal = digitalRead(button);
Time = millis();
if (!switchVal) {
Off = true;
if (buttonVal && (lastButtonVal != buttonVal) && (Time - timeSince > 100)) {
timeSince = Time;
counter++;
if (counter % 4 == 1) {
stepper1.moveTo(stepper1.currentPosition() + 1e9);
stepper2.moveTo(stepper2.currentPosition() + 1e9);
stepper3.moveTo(stepper3.currentPosition() + 1e9);
} else if (counter % 4 == 2) {
stepper1.moveTo(stepper1.currentPosition() + decTime + (SPR - (stepper1.currentPosition() % SPR)));
} else if (counter % 4 == 3) {
stepper2.moveTo(stepper2.currentPosition() + decTime + (SPR - (stepper2.currentPosition() % SPR)));
midRun = true;
} else if (counter % 4 == 0 && midRun) {
stepper3.moveTo(stepper3.currentPosition() + decTime + (SPR - (stepper3.currentPosition() % SPR)));
midRun = false;
}
}
} else {
if (Off) {
stepper1.moveTo(stepper1.currentPosition() - (stepper1.currentPosition() % SPR));
stepper2.moveTo(stepper2.currentPosition() - (stepper2.currentPosition() % SPR));
stepper3.moveTo(stepper3.currentPosition() - (stepper3.currentPosition() % SPR));
counter = 0;
Off = false;
midRun = false;
}
}
lastButtonVal = buttonVal;
stepper1.run();
stepper2.run();
stepper3.run();
}
I also tried writing it like this:
else if (counter % 4 == 3) {
stepper2.moveTo(stepper2.currentPosition() + decTime + (SPR - (stepper2.currentPosition() % SPR)));
stepper3.moveTo(stepper3.currentPosition() + 1e9);
midRun = true;
}

