A Polulo A4988
driver board listens for a LOW -> HIGH
pulse on a digital pin to step a stepper motor.
A simple way to pulse this with a delay of say 100μs
, is to use the delayMicroseconds
function:
#define STEP_PIN 3
#define DELAY 100
void setup(){
pinMode(STEP_PIN, OUTPUT);
}
void loop(){
//heavy computation taking time
digitalWrite(STEP_PIN, LOW);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(DELAY);
}
Although this works, the issue is that all the time that the processor spends in the delayMicroseconds
function is wasted. This means that if I wanted to, for example, handle inputs from a serial line, I would have no time to do this. In addition, the time for these computations to complete would result in the pulse time increasing which is not ideal as the speed of the stepper would no longer be uniform.
How can I efficiently pulse the stepper at the correct time, whilst leaving my self room to run other tasks between pulses?
P.s. This is for this project: https://github.com/joeiddon/chess_robot.