#include "freertos/FreeRTOS.h" // FreeRTOS kernel API
#include "freertos/task.h" // Task creation/management
#include "freertos/queue.h" // Queue for inter-task communication
// ==============================================
// Pin Definitions
// ==============================================
#define RED_LED 2 // TODO: Connect Red LED
#define YELLOW_LED 5 // TODO: Connect Yellow LED
#define GREEN_LED 4 // TODO: Connect Green LED
// ==============================================
// Queue Handle
// ==============================================
QueueHandle_t xQueueTrafficStates;
// TODO: Define a global queue handle (QueueHandle_t)
// This queue will pass traffic light states between tasks.
// ==============================================
// Enum for Traffic Light States
// ==============================================
// TODO: Define an enum with RED, GREEN, and YELLOW
// This will make your code easier to read than using raw numbers.
enum trafficLight{
RED , GREEN , YELLOW
};
// ==============================================
// Traffic Controller Task
// ==============================================
// TODO: In a loop, send the current state to the queue
// Sequence: RED → GREEN → YELLOW → RED → ...
// Use vTaskDelay(pdMS_TO_TICKS(3000)) between state changes
// Using a switch statement within the while loop to switch to different traffic states
void Task_TrafficController(void *pvParameters) {
enum trafficLight currentState = RED;
while(1){
xQueueSend(xQueueTrafficStates, ¤tState,portMAX_DELAY);
switch (currentState){
case RED:
currentState = GREEN;
break;
case GREEN:
currentState = YELLOW;
break;
case YELLOW:
currentState = RED;
break;
}
vTaskDelay(pdMS_TO_TICKS(3000));
}
}
// ==============================================
// Traffic Light Task
// ==============================================
// TODO: Wait for a state from the queue.
// Turn OFF all LEDs first, then turn ON only the LED for that state.
// Print the current state to Serial (e.g., "Traffic Light: GREEN (Go)")
// having a lot of if-statements to check which traffic state should be on from the Queue.
void Task_TrafficLight(void *pvParameters) {
while(1){
enum trafficLight currentState;
String lightOn;
int pin;
if(xQueueReceive(xQueueTrafficStates, ¤tState,portMAX_DELAY) == pdPASS){
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, LOW);
if(currentState == RED){
lightOn = "RED";
pin = 2;
}
if (currentState == YELLOW){
lightOn = "YELLOW";
pin = 5;
}
if (currentState == GREEN) {
lightOn = "GREEN";
pin = 4;
}
digitalWrite(pin, HIGH);
Serial.print("Traffic light:");
Serial.print(lightOn);
Serial.print(" (Go)");
}
vTaskDelay(pdMS_TO_TICKS(30));
}
}
// ==============================================
// Setup
// ==============================================
void setup() {
Serial.begin(115200);
// TODO: Set all LED pins as OUTPUT
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT );
pinMode(GREEN_LED, OUTPUT);
// TODO: Create the queue using xQueueCreate
xQueueTrafficStates = xQueueCreate(2, sizeof(int));
// Hint: Queue length can be 2, item size = sizeof(enum)
// TODO: Create both tasks using xTaskCreate
xTaskCreate(Task_TrafficController, "taskTrafficController",1024,NULL,1,NULL);
xTaskCreate(Task_TrafficLight, "taskTrafficLight", 1024, NULL, 1, NULL);
// Give both tasks the same priority for now
}
// ==============================================
// Loop
// ==============================================
void loop() {
// Leave empty. FreeRTOS handles task scheduling.
}Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1