// a simple traffic light system using GPIO pins to control LEDs.
#define LED_RED_1 2
#define LED_YELLOW_1 3
#define LED_GREEN_1 4
#define LED_RED_2 8
#define LED_YELLOW_2 9
#define LED_GREEN_2 10
// These lines define constants for the GPIO pins connected to the LEDs.
// LED_RED_1 is connected to pin 2, LED_YELLOW_1 to pin 3, and LED_GREEN_1 to pin 4.
// Similarly, LED_RED_2 is connected to pin 8, LED_YELLOW_2 to pin 9, and LED_GREEN_2 to pin 10.
int main() {
// The main function where the program execution starts.
_gpio_init(LED_RED_1);
gpio_set_dir(LED_RED_1, GPIO_OUT);
_gpio_init(LED_YELLOW_1);
gpio_set_dir(LED_YELLOW_1, GPIO_OUT);
_gpio_init(LED_GREEN_1);
gpio_set_dir(LED_GREEN_1, GPIO_OUT);
_gpio_init(LED_RED_2);
gpio_set_dir(LED_RED_2, GPIO_OUT);
_gpio_init(LED_YELLOW_2);
gpio_set_dir(LED_YELLOW_2, GPIO_OUT);
_gpio_init(LED_GREEN_2);
gpio_set_dir(LED_GREEN_2, GPIO_OUT);
// These lines initialize the GPIO pins for the LEDs
// and set their direction to output. _gpio_init initializes the pin, and gpio_set_dir sets the pin direction to output.
while (1) {
// This starts an infinite loop, meaning the code inside will run repeatedly
gpio_put(LED_RED_1, 1);
gpio_put(LED_YELLOW_1, 0);
gpio_put(LED_GREEN_1, 0);
gpio_put(LED_RED_2, 1);
gpio_put(LED_YELLOW_2, 0);
gpio_put(LED_GREEN_2, 0);
// Turns on the red LEDs and turns off the yellow and green LEDs for both traffic lights. Then, it waits for 1 second.
sleep_ms(1000);
gpio_put(LED_RED_1, 0);
gpio_put(LED_GREEN_1, 1);
// Turns off the red LED and turns on the green LED for the first traffic light. Then, it waits for 3 seconds.
sleep_ms(3000);
gpio_put(LED_GREEN_1, 0);
gpio_put(LED_YELLOW_1, 1);
// Turns off the green LED and turns on the yellow LED for the first traffic light. Then, it waits for 1 second.
sleep_ms(1000);
gpio_put(LED_YELLOW_1, 0);
gpio_put(LED_RED_1, 1);
gpio_put(LED_RED_2, 1);
// Turns off the yellow LED and turns on the red LEDs for both traffic lights. Then, it waits for 1 second.
sleep_ms(1000);
// Turns off the red LED and turns on the green LED for the second traffic light. Then, it waits for 3 seconds.
gpio_put(LED_RED_2, 0);
gpio_put(LED_GREEN_2, 1);
sleep_ms(3000);
// Turns off the green LED and turns on the yellow LED for the second traffic light. Then, it waits for 1 second.
gpio_put(LED_GREEN_2, 0);
gpio_put(LED_YELLOW_2, 1);
sleep_ms(1000);
// The loop repeats indefinitely.
}
// The program returns 0, indicating successful execution, though this line is never reached due to the infinite loop.
return (0);
}