#include <Arduino_FreeRTOS.h>
#include <Arduino.h>
#include <semphr.h>
// Task handles and semaphore
TaskHandle_t Task1Handle = NULL;
TaskHandle_t Task2Handle = NULL;
SemaphoreHandle_t xCountingSemaphore;
void Task1(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xCountingSemaphore, portMAX_DELAY) == pdTRUE) {
Serial.println("Task 1 is running and using serial");
xSemaphoreGive(xCountingSemaphore);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void Task2(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xCountingSemaphore, portMAX_DELAY) == pdTRUE) {
Serial.println("Task 2 is running and using serial");
xSemaphoreGive(xCountingSemaphore);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect
}
Serial.println("Setup complete");
// Create counting semaphore with max count of 1
xCountingSemaphore = xSemaphoreCreateCounting(1, 1);
if (xCountingSemaphore == NULL) {
Serial.println("Failed to create counting semaphore!");
return;
}
// Create tasks
if (xTaskCreate(Task1, "Task 1", 128, NULL, 1, &Task1Handle) != pdPASS) {
Serial.println("Task 1 creation failed!");
}
if (xTaskCreate(Task2, "Task 2", 128, NULL, 1, &Task2Handle) != pdPASS) {
Serial.println("Task 2 creation failed!");
}
}
void loop() {
// Nothing here
}