// "Task macros" from discussion at https://forum.arduino.cc/t/is-there-a-non-blocking-delay-thats-as-easy-to-use-as-the-built-in-delay-function/1195423/17?u=davex
// and the https://github.com/DrDiettrich/ALib0 library that that snippet was drawn from
// Also see https://forum.arduino.cc/t/hook-for-rtos-in-timer0-on-avr/1132394/4
// wokwi https://wokwi.com/projects/382926594591467521
// These macros build state machines with millis() and switch-case infrastructure.
// WAIT:: The output is suspicious per
// https://forum.arduino.cc/t/is-there-a-non-blocking-delay-thats-as-easy-to-use-as-the-built-in-delay-function/1195423/22
# define millis_t uint32_t
# define taskBegin() static int _mark = 0; static millis_t __attribute__((unused)) time_Stamp = 0; switch(_mark){ case 0:
# define taskSwitch() { _mark = __LINE__; return; case __LINE__: ; }
# define taskDelay(interval) time_Stamp = millis(); taskSwitch(); if ((millis() - time_Stamp) < (interval)) return;
# define taskWaitFor(condition) taskSwitch(); if (!(condition)) return;
# define taskRestart() { _mark = 0; return; }
# define taskEnd() _mark=0; }
void setup() {
Serial.begin(115200);
}
void loop() {
{
taskBegin();
Serial.println("Hello world!");
taskDelay(500);
taskEnd();
}
{
taskBegin();
Serial.println(" Hello mars!");
taskDelay(700);
taskEnd();
}
}
// alternately:
void he (void) {
taskBegin();
Serial.print(millis());
Serial.println(" Hello world!");
taskDelay(500);
taskEnd();
}
void hm (void) {
taskBegin();
Serial.print(millis());
Serial.println(" Hello mars!");
taskDelay(700);
taskEnd();
}