#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
// Define the number of LEDs in each direction and the number of directions
#define NUM_LEDS 2 // Two LEDs per direction (e.g., RED, GREEN)
#define NUM_DIRECTIONS 2 // Two directions for one intersection
// Define GPIO pins for traffic lights (two directions, each with two LEDs)
int traffic_lights[NUM_DIRECTIONS][NUM_LEDS] = {
{1, 2}, // Direction 1: RED1, GREEN1
{4, 5} // Direction 2: RED2, GREEN2
};
// Define GPIO pins for pedestrian features
#define PIR_SENSOR_GPIO 6 // GPIO pin for PIR sensor
#define PUSH_BUTTON_GPIO 14 // GPIO pin for pedestrian push button
// Define GPIO pins for pedestrian timer bars and white pedestrian LEDs
int timer_bar[NUM_DIRECTIONS][10] = {
{39, 38, 37, 36, 35, 0, 45, 48, 47, 21}, // Timer bar for Direction 2
{11, 10, 9, 46, 3, 8, 18, 17, 16, 15} // Timer bar for Direction 1
};
int white_leds[NUM_DIRECTIONS] = {13, 7}; // White pedestrian LEDs for Direction1 and Direction2
// Function Declarations
void initialize_gpio(); // Initialize GPIO pins
void traffic_light_sequence(); // Run continuous traffic light sequence
void pedestrian_signal(int same_traffic[], int opposite_traffic[], int white_led, int timer_bar[]);
// Main function
void app_main() {
initialize_gpio(); // Initialize all GPIO pins for inputs and outputs
// Infinite loop for traffic light and pedestrian signals
while (1) {
int push_button_state = gpio_get_level(PUSH_BUTTON_GPIO);
int pir_sensor_state = gpio_get_level(PIR_SENSOR_GPIO);
// If pedestrian button or PIR sensor is triggered, handle pedestrian signal
if (push_button_state == 1) {
pedestrian_signal(traffic_lights[0], traffic_lights[1], white_leds[0], timer_bar[0]);
} else if (pir_sensor_state == 1) {
pedestrian_signal(traffic_lights[1], traffic_lights[0], white_leds[1], timer_bar[1]);
} else {
// Continue normal traffic light sequence
traffic_light_sequence();
}
}
}
// Initialize GPIO pins for inputs and outputs
void initialize_gpio() {
gpio_set_direction(PIR_SENSOR_GPIO, GPIO_MODE_INPUT);
gpio_set_direction(PUSH_BUTTON_GPIO, GPIO_MODE_INPUT);
// Configure traffic light pins as outputs
for (int l = 0; l < NUM_DIRECTIONS; l++) {
for (int t = 0; t < NUM_LEDS; t++) {
gpio_set_direction(traffic_lights[l][t], GPIO_MODE_OUTPUT);
}
}
// Configure pedestrian white LEDs and timer bar pins as outputs
for (int l = 0; l < NUM_DIRECTIONS; l++) {
gpio_set_direction(white_leds[l], GPIO_MODE_OUTPUT);
for (int t = 0; t < 10; t++) {
gpio_set_direction(timer_bar[l][t], GPIO_MODE_OUTPUT);
}
}
}
// Continuous traffic light sequence
void traffic_light_sequence() {
// Sequence 1: Direction 1 Green, Direction 2 Red
gpio_set_level(traffic_lights[0][0], 0); // RED1 OFF
gpio_set_level(traffic_lights[0][1], 1); // GREEN1 ON
gpio_set_level(traffic_lights[1][0], 1); // RED2 ON
gpio_set_level(traffic_lights[1][1], 0); // GREEN2 OFF
vTaskDelay(5000 / portTICK_PERIOD_MS); // Wait 5 seconds
// Sequence 2: Direction 1 Red, Direction 2 Green
gpio_set_level(traffic_lights[0][0], 1); // RED1 ON
gpio_set_level(traffic_lights[0][1], 0); // GREEN1 OFF
gpio_set_level(traffic_lights[1][0], 0); // RED2 OFF
gpio_set_level(traffic_lights[1][1], 1); // GREEN2 ON
vTaskDelay(5000 / portTICK_PERIOD_MS); // Wait 5 seconds
}
// Pedestrian signal sequence
void pedestrian_signal(int same_traffic[], int opposite_traffic[], int white_led, int timer_bar[]) {
// Step 1: Turn same traffic red and opposite traffic yellow
gpio_set_level(same_traffic[0], 1); // RED ON for same traffic
gpio_set_level(opposite_traffic[1], 1); // GREEN OFF (Yellow assumed ON)
// Step 2: Turn on all segments of pedestrian timer bar
for (int t = 0; t < 10; t++) {
gpio_set_level(timer_bar[t], 1);
}
// Step 3: Countdown with blinking pedestrian white LED
for (int i = 0; i < 10; i++) {
gpio_set_level(white_led, 1); // Turn ON white LED
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(white_led, 0); // Turn OFF white LED
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(timer_bar[i], 0); // Turn OFF current timer bar segment
printf("Pedestrian signal countdown: %d seconds remaining\n", 10 - i);
}
// Step 4: Reset pedestrian and traffic signals
gpio_set_level(white_led, 0); // Turn OFF pedestrian white LED
gpio_set_level(same_traffic[0], 0); // Turn OFF RED light for same traffic
gpio_set_level(opposite_traffic[1], 0); // Turn OFF GREEN/YELLOW for opposite traffic
}