user36955: 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.

user36955: Instead of delaying between pulses, loop around the main loop() function as fast as possible constantly checking the time to see if we should pulse the motor.
This means that computations can occur in the loop (so long as they are reasonably fast).
To implement this, the micros() function can be used to get an unsigned long representing the microseconds that have passed since the start of the program.
Then in each loop cycle, we need to check if the difference between the current time and the last time that we pulsed is >= the pulse delay. In which case, we send a pulse to the motor
However, instead of setting the last pulse time variable to the current time, we instead increment the last pulse variable by the delay time. This has the affect that even if we were slightly late in pulsing, we pretend that we did in fact pulse at the right time so that they do not drift over time.
This would look something like:
#define STEP_PIN  3
#define DELAY     100

uint32_t last_pulse;

void setup(){
    pinMode(STEP_PIN, OUTPUT);
    last_pulse = micros();
}

void loop(){
    //heavy computation taking time
    if (micros() - last_pulse >= DELAY){
        last_pulse += DELAY;
        digitalWrite(STEP_PIN, LOW);
        digitalWrite(STEP_PIN, HIGH);
    }
}


Note that there is a slight subtlety here in that the result from micros() and therefore the value of last_pulse will overflow after ~70 minutes. However, because we are taking a difference of two unsigned numbers, this method will still work, so long as micros() is never a whole cycle ahead of last_pulse which in practice will never be the case.

36955: <p>A Polulo <a href="https://www.pololu.com/product/1182" rel="nofollow noreferrer"><code>A4988</code></a> driver board listens for a <code>LOW -&gt; HIGH</code> pulse on a digital pin to step a stepper motor.</p>

<p>A simple way to pulse this with a delay of say <code>100μs</code>, is to use the <code>delayMicroseconds</code> function:</p>

<pre><code>#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);
}
</code></pre>

<p>Although this works, the issue is that all the time that the processor spends in the <code>delayMicroseconds</code> function is <em>wasted</em>. 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.</p>

<p>How can I efficiently pulse the stepper at the correct time, whilst leaving my self room to run other tasks between pulses?</p>

<hr>

<p>P.s. This is for this project: <a href="https://github.com/joeiddon/chess_robot" rel="nofollow noreferrer">https://github.com/joeiddon/chess_robot</a>.</p>
 36955: <p>Instead of delaying between pulses, loop around the main <code>loop()</code> function as fast as possible <em>constantly checking the time to see if we should pulse the motor</em>.</p>

<p>This means that computations can occur in the loop (so long as they are reasonably fast).</p>

<p>To implement this, the <code>micros()</code> function can be used to get an unsigned long representing the microseconds that have passed since the start of the program.</p>

<p>Then in each loop cycle, we need to check if the difference between the current time and the last time that we pulsed is <code>&gt;=</code> the pulse delay. In which case, we send a pulse to the motor</p>

<p>However, instead of setting the last pulse time variable to the current time, we instead increment the last pulse variable by the delay time. This has the affect that even if we were slightly late in pulsing, we pretend that we <em>did</em> in fact pulse at the right time so that they do not drift over time.</p>

<p>This would look something like:</p>

<pre><code>#define STEP_PIN  3
#define DELAY     100

uint32_t last_pulse;

void setup(){
    pinMode(STEP_PIN, OUTPUT);
    last_pulse = micros();
}

void loop(){
    //heavy computation taking time
    if (micros() - last_pulse &gt;= DELAY){
        last_pulse += DELAY;
        digitalWrite(STEP_PIN, LOW);
        digitalWrite(STEP_PIN, HIGH);
    }
}
</code></pre>

<hr>

<p><em>Note that there is a slight subtlety here in that the result from <code>micros()</code> and therefore the value of <code>last_pulse</code> will overflow after <a href="https://www.arduino.cc/reference/en/language/functions/time/micros/" rel="nofollow noreferrer"><code>~70</code> minutes</a>. However, because we are taking a <strong>difference</strong> of two unsigned numbers, this method <a href="https://stackoverflow.com/questions/2061245/how-to-subtract-two-unsigned-ints-with-wrap-around-or-overflow">will still work</a>, so long as <code>micros()</code> is never a whole cycle ahead of <code>last_pulse</code> which in practice will never be the case.</em></p>
