I am using A4988 driver and didnt test yet with DRV8825.
I tested with 2 different motors from old printers , so dont have much datasheet..only steps 7.5 degree and
coil resistance, that I was able to manage the current limit on driver board.
If I upload some test code only to run motors without button control, that works nice.
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
byte directionPin = 6;
byte stepPin = 7;
byte buttonCWpin = 4;
byte buttonCCWpin = 5;
boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
byte ledPin = 13;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 300; // milliseconds
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with millis()");
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonCWpin, INPUT);
pinMode(buttonCCWpin, INPUT);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons() {
buttonCCWpressed = true;
buttonCWpressed = true;
if (digitalRead(buttonCWpin) == HIGH) {
buttonCWpressed = false;
}
if (digitalRead(buttonCCWpin) == HIGH) {
buttonCCWpressed = false;
}
}
void actOnButtons() {
if (buttonCWpressed == false) {
digitalWrite(directionPin, LOW);
singleStep();
}
if (buttonCCWpressed == false) {
digitalWrite(directionPin, HIGH);
singleStep();
}
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
// prevStepMillis += millisBetweenSteps;
prevStepMillis = curMillis;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}
Difference is at the end , your code have line prevStepMillis += millisBetweenSteps;
which I changed to prevStepMillis = curMillis; and now motor runs smotely.
Before that ...
Somethimes it happen that when I push CCW button, motor make 1 step CW and then run correct CCW direction.
The same is for CW button.
Also jumps few steps ahead...which doesnt happen with changed line.
I am using RC debounce circuit with Schmitt triger.
Look at the video
Stepper 1 is with your code, stepper 2 is with corrected code
I understand. I have just been having a similar problem myself.
The problem is that if you don't update the value of prevStepMillis it will have a very old value (maybe from some minutes ago) and the result of that is that there will be a huge number of very fast steps (probably too fast for the motor) while the time catches up.
Your solution is perfectly practical.
What I have done in my own program is the equivalent of setting prevStepMillis = currentMillis when a button is first pressed. That brings the value up to date without interfering with the slightly greater precision of using prevStepMillis += millisBetweenSteps; for the rest of the time that the button is held. But my program is not using button and I doubt if the small loss of accuracy will matter when using buttons.
I will consider whether I should change the demo code.
Thanks for the explanation.
It is realy nice feeling when things works as we plan.
I am working on some kind of cutting machine and this was first step ... moving the motors
If you plan on moving two or more stepper motors in sync (like on a CNC machine) then I suspect precise step timing will be important and I would not rely on prevStepMillis = currentMillis other than for setting the initial value for a movement.