#include <Arduino_FreeRTOS.h>
#include <semphr.h>
long d_time = 150;
volatile unsigned long last_micros;
SemaphoreHandle_t interruptSemaphore;
void setup() {
pinMode(3, INPUT_PULLUP);
xTaskCreate(TaskLed, "Led", 128, NULL, 0, NULL );
xTaskCreate(TaskBlink, "LedBlink", 128, NULL, 0, NULL );
interruptSemaphore = xSemaphoreCreateBinary();
if (interruptSemaphore != NULL) {
attachInterrupt(digitalPinToInterrupt(3), Interrupt, LOW);
}
}
void loop() {}
void interruptHandler() {
xSemaphoreGiveFromISR(interruptSemaphore, NULL);
}
void TaskLed(void *pvParameters)
{
pinMode(8, OUTPUT);
for (;;) {
if (xSemaphoreTake(interruptSemaphore, portMAX_DELAY) == pdPASS) {
digitalWrite(8, !digitalRead(8));
}
}
}
void TaskBlink(void *pvParameters)
{
pinMode(21, OUTPUT);
for (;;) {
digitalWrite(21, HIGH);
vTaskDelay(500 / portTICK_PERIOD_MS);
digitalWrite(21, LOW);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
void Interrupt() {
if((long)(micros() - last_micros) >= d_time * 1000) {
interruptHandler();
last_micros = micros();
}
}