#include <FreeRTOS.h>
#include <event_groups.h>
#include <timers.h>

// Define the bits used in the event group
#define BIT_0 (1 << 0)
#define BIT_1 (1 << 1)

// Create an event group handle
EventGroupHandle_t xEventGroup;
TimerHandle_t xTimer;

void vTimerCallback(TimerHandle_t xTimer)
{
xEventGroupSetBits(xEventGroup, BIT_0);

}
// Task 1
void vTask1(void *pvParameters) {
    for (;;) {
        // Set BIT_0 in the event group
        xEventGroupSetBits(xEventGroup, BIT_1);
        vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
    }
}

// Task 2
void vTask2(void *pvParameters) {
    for (;;) {
        // Wait for BIT_0 to be set within the event group
        xEventGroupWaitBits(xEventGroup, BIT_0, pdTRUE, pdFALSE, portMAX_DELAY);
        // Perform some action when BIT_0 is set
        Serial.println("Task 2: BIT_0 is set");
    }
}

// Task 3
void vTask3(void *pvParameters) {
    for (;;) {
        // Wait for both BIT_0 and BIT_1 to be set within the event group
        xEventGroupWaitBits(xEventGroup, BIT_0 | BIT_1, pdTRUE, pdTRUE, portMAX_DELAY);
        // Perform some action when both BIT_0 and BIT_1 are set
        Serial.println("Task 3: Both BIT_0 and BIT_1 are set");
    }
}

void setup() {
    Serial.begin(9600);

    // Create the event group
    xEventGroup = xEventGroupCreate();

    // Create the tasks
    xTaskCreate(vTask1, "Task 1", 128, NULL, 1, NULL);
    xTaskCreate(vTask2, "Task 2", 128, NULL, 1, NULL);
    xTaskCreate(vTask3, "Task 3", 128, NULL, 1, NULL);
       xTimer = xTimerCreate(
        "LED Timer",             // Timer name
        pdMS_TO_TICKS(1000),     // Timer period (1 second)
        pdTRUE,                  // Auto-reload
        (void *)0,               // Timer ID
        vTimerCallback           // Callback function
    );

    xTimerStart(xTimer, 0);
    // Start the scheduler
    vTaskStartScheduler();
}

void loop() {
    // Empty. Things are done in tasks.
}