SemaphoreHandle_t mutex; // Create handle
void setup() {
Serial.begin(115200);
mutex = xSemaphoreCreateMutex(); //Create the mutex object
xTaskCreate(
print1, // Function name of the task
"print1", // Name of the task (e.g. for debugging)
2048, // Stack size (bytes)
NULL, // Parameter to pass
1, // Task priority
NULL // Task handle
);
xTaskCreate(
print2, // Function name of the task
"print2", // Name of the task (e.g. for debugging)
2048, // Stack size (bytes)
NULL, // Parameter to pass
1, // Task priority
NULL // Task handle
);
}
void print1(void *pvParameters) {
while (1) {
print_task(1);
delay(1); // Short delay is needed!
}
}
void print2(void *pvParameters) {
while (1) {
print_task(2);
delay(1); // Short delay is needed!
}
}
void loop() {
print_task(3);
delay(1);
}
void print_task(uint8_t t)
{
xSemaphoreTake(mutex, portMAX_DELAY); // Take the mutex
for (int i = 0; i < 10; i++) {
Serial.print(t);
}
Serial.println("");
xSemaphoreGive(mutex); // // Release the mutex
}