#include <Arduino_FreeRTOS.h>
#include <semphr.h>
SemaphoreHandle_t interruptSemaphore;
SemaphoreHandle_t blinkMutex;
#define PIN_LED1 13
#define PIN_LED2 7
#define PIN_BUTTON 2
long debouncing_time = 150;
volatile unsigned long last_micros;
volatile int delayBlink = 100;
volatile bool state = true;
void debounceInterrupt() {
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
dynamicDelay();
interruptHandler();
last_micros = micros();
}
}
void dynamicDelay() {
xSemaphoreTake(blinkMutex, portMAX_DELAY);
if (state) {
delayBlink += 300;
} else {
delayBlink -= 300;
}
state = !state;
xSemaphoreGive(blinkMutex);
}
void interruptHandler() {
xSemaphoreGiveFromISR(interruptSemaphore, NULL);
}
void TaskLed1(void *pvParameters) {
(void) pvParameters;
Serial.println("TaskLed1 started");
pinMode(PIN_LED1, OUTPUT);
while(1) {
xSemaphoreTake(interruptSemaphore, portMAX_DELAY);
digitalWrite(PIN_LED1, !digitalRead(PIN_LED1));
}
}
void TaskLed2(void *pvParameters) {
(void) pvParameters;
Serial.println("TaskLed2 started");
pinMode(PIN_LED2, OUTPUT);
while(1) {
digitalWrite(PIN_LED2, HIGH);
vTaskDelay(delayBlink / portTICK_PERIOD_MS);
digitalWrite(PIN_LED2, LOW);
vTaskDelay(delayBlink / portTICK_PERIOD_MS);
}
}
void setup() {
Serial.begin(9600); // Begin serial communication
pinMode(PIN_BUTTON, INPUT_PULLUP);
xTaskCreate(TaskLed1, "Led1", 128, NULL, 2, NULL );
xTaskCreate(TaskLed2, "Led2Blink", 128, NULL, 1, NULL );
interruptSemaphore = xSemaphoreCreateBinary();
blinkMutex = xSemaphoreCreateMutex();
if (interruptSemaphore != NULL) {
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON), debounceInterrupt, LOW);
}
}
void loop() {}