#include <Arduino.h>
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
// Task handles and semaphore
TaskHandle_t Task1Handle = NULL;
TaskHandle_t Task2Handle = NULL;
SemaphoreHandle_t xBinarySemaphore;
void Task1(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xBinarySemaphore, portMAX_DELAY) == pdTRUE) {
Serial.println("Task 1 is running and using serial");
xSemaphoreGive(xBinarySemaphore);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void Task2(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xBinarySemaphore, portMAX_DELAY) == pdTRUE) {
Serial.println("Task 2 is running and using serial");
xSemaphoreGive(xBinarySemaphore);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void setup() {
Serial.begin(115200);
// Create binary semaphore
xBinarySemaphore = xSemaphoreCreateBinary();
xSemaphoreGive(xBinarySemaphore); // Initialize the semaphore as available
// Create tasks
xTaskCreate(Task1, "Task 1", 1024, NULL, 1, &Task1Handle);
xTaskCreate(Task2, "Task 2", 1024, NULL, 1, &Task2Handle);
}
void loop() {
// Nothing here
}