// Author : Chayathon.R
// ID : 6530300082
// Date : 23/12/24
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#define p1_button 25
#define p2_button 26
#define p3_button 27
#define LED 12
QueueHandle_t xQueue;
int pass1;
int pass2;
int pass3;
void vButtonTask1(void *pvparameters)
{
int Button_data = 1;
while (1)
{
int real_time = millis();
if (digitalRead(p1_button) == LOW and real_time - pass1 >= 1000)
{
pass1 = millis();
xQueueSend(xQueue, &Button_data, 0);
Serial.println("Button 1 Click");
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void vButtonTask2(void *pvparameters)
{
int Button_data = 2;
while (1)
{
int real_time = millis();
if (digitalRead(p2_button) == LOW and real_time - pass2 >= 1000)
{
pass2 = millis();
xQueueSend(xQueue, &Button_data, 0);
Serial.println("Button 2 Click");
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void vButtonTask3(void *pvparameters)
{
int Button_data = 3;
while (1)
{
int real_time = millis();
if (digitalRead(p3_button) == LOW and real_time - pass3 >= 1000)
{
pass3 = millis();
xQueueSend(xQueue, &Button_data, 0);
Serial.println("Button 3 Click");
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void vControlTask(void *pvParameters)
{
while (1)
{
if (uxQueueMessagesWaiting(xQueue) >= 3)
{
int peekData = 0;
if (xQueuePeek(xQueue, &peekData, 0) == pdPASS)
{
Serial.print("Peeked data: ");
Serial.println(peekData);
if (peekData == 1)
{
int pressedButtons[3] = {0};
for (int i = 0; i < 3; i++)
{
if (xQueueReceive(xQueue, &pressedButtons[i], 0) == pdPASS)
{
Serial.print("Button received: ");
Serial.println(pressedButtons[i]);
}
}
if (pressedButtons[0] == 1 && pressedButtons[1] == 2 && pressedButtons[2] == 3)
{
digitalWrite(LED, HIGH);
Serial.println("LED Activated!");
vTaskDelay(pdMS_TO_TICKS(1000));
digitalWrite(LED, LOW);
}
else
{
Serial.println("Incorrect button sequence!");
}
}
}
}
if (uxQueueSpacesAvailable(xQueue) == 0)
{
Serial.println("Queue is full! Resetting queue.");
xQueueReset(xQueue);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void setup()
{
Serial.begin(115200);
pinMode(p1_button, INPUT_PULLUP);
pinMode(p2_button, INPUT_PULLUP);
pinMode(p3_button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
xQueue = xQueueCreate(10, sizeof(int));
if (xQueue == NULL)
{
Serial.println("Failed to create queue!");
return;
}
xTaskCreate(vButtonTask1, "Button Task 1", 1024, NULL, 1, NULL);
xTaskCreate(vButtonTask2, "Button Task 2", 1024, NULL, 1, NULL);
xTaskCreate(vButtonTask3, "Button Task 3", 1024, NULL, 1, NULL);
xTaskCreate(vControlTask, "Control Task", 2048, NULL, 2, NULL);
Serial.println("All tasks started!");
}
void loop()
{
vTaskDelay(pdMS_TO_TICKS(10));
}