#include <Arduino_FreeRTOS.h>
const int led1Pin = 13;
const int led2Pin = 12;
const int buttonPin = 2;
volatile bool led2Blinking = false;
volatile unsigned long lastInterruptTime = 0;
// Delay settings for each LED (defaults to 500ms for both)
volatile unsigned long led1Delay = 1000;
volatile unsigned long led2Delay = 1000;
void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  xTaskCreate(TaskLed1, "Led1", 128, NULL, 1, NULL);
  xTaskCreate(TaskLed2, "Led2", 128, NULL, 1, NULL);
  xTaskCreate(TaskButton, "Button", 128, NULL, 1, NULL);
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING);
}
void loop() {
  // Empty.
}
void TaskLed1(void *pvParameters) {
  (void) pvParameters;
  for (;;) {
    digitalWrite(led1Pin, HIGH);
    vTaskDelay(led1Delay / portTICK_PERIOD_MS);
    digitalWrite(led1Pin, LOW);
    vTaskDelay(led1Delay / portTICK_PERIOD_MS);
  }
}
void TaskLed2(void *pvParameters) {
  (void) pvParameters;
  for (;;) {
    if (led2Blinking) {
      digitalWrite(led2Pin, HIGH);
      vTaskDelay(led2Delay / portTICK_PERIOD_MS);
      digitalWrite(led2Pin, LOW);
    }
    vTaskDelay(led2Delay / portTICK_PERIOD_MS);
  }
}
void TaskButton(void *pvParameters) {
  (void) pvParameters;
  for (;;) {
    // The button state is read in the ISR, so this task is empty.
    // But you could potentially use this task to adjust delay timings if needed.
    vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
}
void buttonInterrupt() {
  unsigned long interruptTime = xTaskGetTickCount();
  if (interruptTime - lastInterruptTime > 200 / portTICK_PERIOD_MS) {
    led2Blinking = !led2Blinking;
    // Optional: Adjust LED timings on button press
    // Example: double the LED blink rates when the button is pressed
    // led1Delay *= 2;
    // led2Delay *= 2;
  }
  lastInterruptTime = interruptTime;
}