#include <Arduino.h>
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
#define BUTTON_PIN 2
#define LED_PIN 13
SemaphoreHandle_t xBinarySemaphore;
void vTaskFunction(void *pvParameters);
void ISR_ButtonPress();
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), ISR_ButtonPress, FALLING);
xBinarySemaphore = xSemaphoreCreateBinary();
if (xBinarySemaphore != NULL) {
xTaskCreate(
vTaskFunction, /* Task function. */
"Task1", /* String with name of task. */
128, /* Stack size in bytes. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
}
}
void loop() {
// Do nothing here.
}
void vTaskFunction(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(xBinarySemaphore, portMAX_DELAY) == pdTRUE) {
digitalWrite(LED_PIN, HIGH);
vTaskDelay(1000 / portTICK_PERIOD_MS);
digitalWrite(LED_PIN, LOW);
}
}
}
void ISR_ButtonPress() {
Serial.println("Button pressed!");
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(xBinarySemaphore, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}