// Library: https://github.com/glutio/Taskfun
// This Wokwi project: https://wokwi.com/projects/366407158206025729
// Test to output messages to the Serial monitor from different tasks
#include <Taskfun.h>
SyncVar<bool> serialInUse;
void setup() {
Serial.begin(9600);
serialInUse = false;
pinMode(LED_BUILTIN, OUTPUT);
setupTasks();
noInterrupts();
runTask(TaskOne, 0);
runTask(TaskTwo, 0);
interrupts();
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(150);
digitalWrite(LED_BUILTIN, LOW);
delay(350);
}
void TaskOne(int)
{
while (true)
{
Print("Hello");
delay(2);
}
}
void TaskTwo(int)
{
while (true)
{
Print("!---------------!");
delay(3);
}
}
// Print the whole message.
void Print( char *s)
{
while (serialInUse) // Is another task using the Serial port ?
yield(); // come back later
serialInUse = true; // set the flag
Serial.println(s);
serialInUse = false; // release the flag
}