#include <Arduino_FreeRTOS.h>
#include <semphr.h>
SemaphoreHandle_t xCountingSemaphore;
void setup()
{
Serial.begin(9600); // Bật thư viện giao tiếp serial.
pinMode(LED_BUILTIN, OUTPUT);
// Tạo tác vụ cho LED Arduino
xTaskCreate(Task1, "Ledon", 128, NULL, 1, NULL);
xTaskCreate(Task2, "Ledoff", 128, NULL, 1, NULL);
xCountingSemaphore = xSemaphoreCreateCounting(1, 1);
xSemaphoreGive(xCountingSemaphore);
}
void loop() {}
void Task1(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
xSemaphoreTake(xCountingSemaphore, portMAX_DELAY);
Serial.println("Inside Task1 and Serial monitor Resource Taken");
digitalWrite(LED_BUILTIN, HIGH);
vTaskDelay(1000); // Trì hoãn 1 giây để quan sát rõ ràng
xSemaphoreGive(xCountingSemaphore);
vTaskDelay(1);
}
}
void Task2(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
xSemaphoreTake(xCountingSemaphore, portMAX_DELAY);
Serial.println("Inside Task2 and Serial monitor Resource Taken");
digitalWrite(LED_BUILTIN, LOW);
vTaskDelay(1000); // Trì hoãn 1 giây để quan sát rõ ràng
xSemaphoreGive(xCountingSemaphore);
vTaskDelay(1);
}
}