#include <Arduino_FreeRTOS.h>
#include <semphr.h>
SemaphoreHandle_t xSemaphore = NULL;
const int buttonPin = 2;
const int ledPin = 13;
void handleButtonInterrupt() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken) {
taskYIELD();
}
}
void taskLED(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xSemaphore, portMAX_DELAY) == pdTRUE) {
digitalWrite(ledPin, HIGH);
vTaskDelay(pdMS_TO_TICKS(1000));
digitalWrite(ledPin, LOW);
}
}
}
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
xSemaphore = xSemaphoreCreateBinary();
if (xSemaphore != NULL) {
xTaskCreate(taskLED, "Task LED", 256, NULL, 1, NULL);
attachInterrupt(digitalPinToInterrupt(buttonPin), handleButtonInterrupt, FALLING);
vTaskStartScheduler();
}
}
void loop() {
}