#include "pico/stdlib.h"
class Traffic
{
private:
const uint RED_LED = 15;
const uint YELLOW_LED = 14;
const uint GREEN_LED = 13;
const uint BUTTON_PIN = 16; // Change to the appropriate GPIO pin number
const uint BUZZER_PIN = 12; // Change to the appropriate GPIO pin number
public:
Traffic()
{
// Initializing GPIO pins
gpio_init(RED_LED);
gpio_init(YELLOW_LED);
gpio_init(GREEN_LED);
gpio_init(BUTTON_PIN);
gpio_init(BUZZER_PIN);
gpio_set_dir(RED_LED, GPIO_OUT);
gpio_set_dir(YELLOW_LED, GPIO_OUT);
gpio_set_dir(GREEN_LED, GPIO_OUT);
gpio_set_dir(BUZZER_PIN, GPIO_OUT);
// Set BUTTON_PIN as input with pull-up resistor
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
}
void run()
{
while (true)
{
if (!gpio_get(BUTTON_PIN))
{
// Button is pressed, pedestrian wants to cross
pedestrianCross();
}
else
{
// Normal traffic flow
normalTrafficFlow();
}
}
}
void pedestrianCross()
{
gpio_put(GREEN_LED, 0);
gpio_put(RED_LED, 1);
gpio_put(BUZZER_PIN, 1); // Buzzer on
sleep_ms(3000); // Buzzer on for 3 seconds
gpio_put(BUZZER_PIN, 0); // Buzzer off
gpio_put(RED_LED, 0);
gpio_put(YELLOW_LED, 1);
sleep_ms(2000);
gpio_put(YELLOW_LED, 0);
gpio_put(RED_LED, 1);
sleep_ms(5000);
}
void normalTrafficFlow()
{
gpio_put(GREEN_LED, 0);
gpio_put(RED_LED, 1);
sleep_ms(5000);
gpio_put(RED_LED, 0);
gpio_put(YELLOW_LED, 1);
sleep_ms(2000);
gpio_put(YELLOW_LED, 0);
gpio_put(GREEN_LED, 1);
sleep_ms(5000);
}
};
int main()
{
Traffic trafficLight;
trafficLight.run();
return 0;
}