0

Best practice for non-blocking delay?

I am looking for best practice to have exact timings when accessing hardware (LCDs, I2C, SPI, etc.). Any suggestions on learning material or tips are appreciated!

28th Feb 2019, 9:22 PM
Kami Rotschild
3 Answers
+ 4
You could use a function that returns the number of milliseconds since boot -millis() in Arduino- and test for timeout in every loop... The idea in pseudo-code: loop { if (millis() - aux >= 1000) { // 1 s elapsed? do some stuff.... aux = millis(); } do something... do something else... } Another approach: instead of test every loop for milliseconds elapsed, you could use a timer interrupt (if available) flag = 0; loop { if (flag) { do some stuff.... flag = 0; } do something... do something else... } void timer_interrupt() { flag = 1; } Usually, you use the timer for other counters... you could set the timer to interrupt every 1ms and use variables to count for several events loop { if (tmo_stuff_A == 0) { do some stuff.... tmo_stuff_A = 1000; } if (tmo_stuff_B == 0) { do some stuff.... tmo_stuff_B = 500; } do something... do something else... } void timer_interrupt() { if (tmo_stuff_A > 0) tmo_stuff_A--; if (tmo_stuff_B > 0) tmo_stuff_B--; } Last recommendation: Use finite- state-machines. An example (mbed and S08, with timer interrupts...in spanish :( ) https://docs.google.com/viewer?a=v&pid=sites&srcid=cGlvaXguZWR1LmFyfHNpc3RlbWFzLWVtYmViaWRvc3xneDozZjkzNjZlMDk2Mzk3Zjlk Another example with Arduino: https://code.sololearn.com/cWGKp0lDL1J4/#c Hope that helps...
1st Mar 2019, 3:08 AM
unChabon
unChabon - avatar
+ 3
By any chance, are you talking about real time programming?
28th Feb 2019, 11:36 PM
Hatsy Rei
Hatsy Rei - avatar
0
diego Code thank you for the examples. I think, that might be helpful for a lot of people starting with Arduino. Hatsy Rei I guess your are asking for the OS? FreeRTOS. I was unsure if I can a quite task specific question: I want to get a SD-Card (without SPI) working on an ESP32 without any Arduino libraries.
1st Mar 2019, 5:50 AM
Kami Rotschild