iioRobot

Open Source multi-axis robot

Arduino • Non blocking programming

- Posted in Software by

There is a whole bunch of tips and tricks on the internet regarding non-blocking Arduino programming. For my part, I chose to use the "DMTimer" library (https://github.com/toxnico/DMTimer).

After loading the library:

#include <dmtimer.h>

and having predisposed various timers:

DMTimer timer1(1000);  //  1 millisecond
DMTimer timer2(50000); // 50 milliseconds

all that remains is to perform the desired functions in the main loop, when each timer is reached :

void loop() {
   unsigned long now = micros();

   if (timer1.isTimeReached(now)) {
     doSomeStufHere();
   }

   if (timer2.isTimeReached(now)) {
     doOtherStufHere();
   }
}

You will notice the choice to align on the same time base by the predisposition of the "now" variable which will be the same for all calls to dependent functions.