// Filename: icte7.ino
// Author: Olivia Cady
// Date: 02/19/26
// Description: Uses a FreeRTOS scheduler to control a traffic light
// ==============================================
// Includes
// ==============================================
#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
// ==============================================
// TODO: Define a global queue handle (QueueHandle_t)
// This queue will pass traffic light states between tasks.
QueueHandle_t queue;
// ==============================================
// 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 states {RED, YELLOW, GREEN};
states red = RED;
states yellow = YELLOW;
states green = GREEN;
// ==============================================
// 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
void Task_TrafficController(void *pvParameters) {
// Your code here
while(1) {
xQueueSend(queue, &red, 0);
vTaskDelay(pdMS_TO_TICKS(3000));
xQueueSend(queue, &green, 0);
vTaskDelay(pdMS_TO_TICKS(3000));
xQueueSend(queue, &yellow, 0);
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)")
void Task_TrafficLight(void *pvParameters) {
// Your code here
while(1) {
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, LOW);
states next_state;
xQueueReceive(queue, &next_state, 0);
if (next_state == RED) {
Serial.println("STOP");
digitalWrite(RED_LED, HIGH);
} else if (next_state == YELLOW) {
Serial.println("SLOW DOWN");
digitalWrite(YELLOW_LED, HIGH);
} else if (next_state == GREEN) {
Serial.println("GO");
digitalWrite(GREEN_LED, HIGH);
}
}
}
// ==============================================
// 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
// Hint: Queue length can be 2, item size = sizeof(enum)
queue = xQueueCreate(2, sizeof(states));
// TODO: Create both tasks using xTaskCreate
// Give both tasks the same priority for now
xTaskCreate(Task_TrafficLight, "Traffic Light", 4096, NULL, 1, NULL);
xTaskCreate(Task_TrafficController, "Traffic Controller", 4096, NULL, 1, NULL);
}
// ==============================================
// Loop
// ==============================================
void loop() {
// Leave empty. FreeRTOS handles task scheduling.
}