#include <Arduino_FreeRTOS.h>
#include <semphr.h>
// Global semaphore variable
SemaphoreHandle_t interruptSemaphore;
void setup() {
// Configure pin 2 as an input with internal pull-up resistor
pinMode(2, INPUT_PULLUP);
// Create task for controlling the LED
xTaskCreate(TaskLed, "Led", 128, NULL, 0, NULL);
// Create a binary semaphore
interruptSemaphore = xSemaphoreCreateBinary();
if (interruptSemaphore != NULL) {
// Attach interrupt for digital pin 2
attachInterrupt(digitalPinToInterrupt(2), interruptHandler, LOW);
}
}
void loop() {
// Empty loop as FreeRTOS tasks handle operations
}
void interruptHandler() {
// Give semaphore in the interrupt handler
xSemaphoreGiveFromISR(interruptSemaphore, NULL);
}
// LED task
void TaskLed(void *pvParameters) {
(void) pvParameters; // Unused parameter
pinMode(LED_BUILTIN, OUTPUT);
for (;;) {
// Take the semaphore
if (xSemaphoreTake(interruptSemaphore, portMAX_DELAY) == pdPASS) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
vTaskDelay(pdMS_TO_TICKS(10)); // Delay for 10 milliseconds
}
}