Hello!
I am using A4988 drivers to control my NEMA17 steppers, and using 1/16 microstepping by setting MS1, MS2 and MS3 high. Everything works well.
I am also using the SLEEP pins of A4988s to turn the motors off when not in use. And I control the logic as follows:
#define speed_delay 2000 // pulse width for motor 700
#define sleep_delay 1000 // works with 10?
#define num_motors 4
struct Stepper {
int stepPin;
int dirPin;
int sleepPin;
};
Stepper steppers[num_motors] = {
{ 2, 3, 10 },
{ 4, 5, 11 },
{ 6, 7, 12 },
{ 8, 9, 13 }
};
int newPos[num_motors];
int multiplier = 1;
void setup() {
for (int i = 0; i < num_motors; ++i) {
pinMode(steppers[i].stepPin, OUTPUT);
pinMode(steppers[i].dirPin, OUTPUT);
pinMode(steppers[i].sleepPin, OUTPUT);
digitalWrite(steppers[i].stepPin, LOW);
digitalWrite(steppers[i].dirPin, LOW);
digitalWrite(steppers[i].sleepPin, LOW);
}
}
void loop() {
for (int i = 0; i < num_motors; ++i) {
newPos[i] = (i % 2 == 0) ? (i * 1000 * multiplier) : (i * -1000 * multiplier);
go_there(i);
delay(1000);
}
multiplier = multiplier * -1;
}
void go_there(int i) {
if (newPos[i] < 0) { // clockwise
digitalWrite(steppers[i].dirPin, HIGH);
newPos[i] = newPos[i] * -1;
} else if (newPos[i] > 0) { // counter-clockwise
digitalWrite(steppers[i].dirPin, LOW);
}
digitalWrite(steppers[i].sleepPin, HIGH);
delay(sleep_delay); // <-- this highlights the problem
while (newPos[i] != 0) {
digitalWrite(steppers[i].stepPin, HIGH);
delayMicroseconds(speed_delay);
digitalWrite(steppers[i].stepPin, LOW);
delayMicroseconds(speed_delay);
newPos[i] = newPos[i] - 1;
}
digitalWrite(steppers[i].sleepPin, LOW);
}
When I set the sleep_delay to 10, the motor seems to work fine. When I increase it to 1000, there is a erratic jerk/jitter followed by 1000ms delay, and then the motor runs smoothly.
Why is this happening?
I started debugging this because I have a suspicion that this erratic jerk/jitter might be happening with 10ms sleep_delay as well, which could be affecting my precision measures. But I wasn't completely sure because 10ms delay is too tiny to observe by looking, and so I started increasing this number to see the effects.