////////////////////////// MULTI (MILLIS) TASK MACRO //////////////////////////////
// Declare and define the names of tasks
#define TSK_INIT(...) unsigned long __VA_ARGS__;
// Touch task time feed
#define TSK_TOGGLE unsigned long currentTask = millis();
// Macro to define start of code with a fixed interval
#define TSK_BEGIN(taskName, taskInterval) \
if (currentTask - (taskName) >= taskInterval) { \
(taskName) = currentTask;
// Macro to end code created with the previous macro
#define TSK_END }
// Yields the current task.
#define TSK_YIELD return;
//////////////////////////// END OF MACRO ////////////////////////////////
TSK_INIT(tsk1, tsk2, tsk3)
void setup() {
Serial.begin(9600);
while (!Serial) {
// wait for serial port to connect. Needed for native USB port only
// AND you want to block until there's a connection
// otherwise the shell can quietly drop output.
}
}
void loop() {
TSK_TOGGLE
TSK_BEGIN(tsk1, 500) {
static unsigned long counter;
Serial.print(" tsk1 -> ");
Serial.println(counter);
counter++;
}
TSK_END
TSK_BEGIN(tsk2, 5000) {
static unsigned long counter;
Serial.print(" tsk2 -> ");
Serial.println(counter);
counter++;
}
TSK_END
TSK_BEGIN(tsk3, 2000) {
while(1) {
static unsigned long counter;
Serial.print(" tsk3 -> ");
Serial.println(counter);
counter++;
TSK_YIELD;
}
}
TSK_END
}