#include <stdio.h>
#include "pico/stdlib.h"
#define LED_RED_PIN 11
#define LED_YELLOW_PIN 12
#define LED_GREEN_PIN 13
#define BUTTON_PIN 5
bool active_cycle = false;
void setup_cycle() {
while (active_cycle) {
// LED Verde aceso por 5 segundos
gpio_put(LED_GREEN_PIN, 1);
sleep_ms(5000);
gpio_put(LED_GREEN_PIN, 0);
// LED Amarelo aceso por 2 segundos
gpio_put(LED_YELLOW_PIN, 1);
sleep_ms(2000);
gpio_put(LED_YELLOW_PIN, 0);
// LED Vermelho aceso por 5 segundos
gpio_put(LED_RED_PIN, 1);
sleep_ms(5000);
gpio_put(LED_RED_PIN, 0);
}
}
int main() {
stdio_init_all();
// Configura os pinos dos LEDs como output
gpio_init(LED_GREEN_PIN);
gpio_set_dir(LED_GREEN_PIN, GPIO_OUT);
gpio_init(LED_YELLOW_PIN);
gpio_set_dir(LED_YELLOW_PIN, GPIO_OUT);
gpio_init(LED_RED_PIN);
gpio_set_dir(LED_RED_PIN, GPIO_OUT);
// Configura o pino do push_button como input com pull-down
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_down(BUTTON_PIN);
// Loop principal para verificar o estado do botão
while (true) {
if (gpio_get(BUTTON_PIN)) {
// Alterna o estaado do cilo do semáforo
active_cycle = !active_cycle;
// Se o ciclo estiver ativo, inicia o ciclo do semáforo
if (active_cycle) {
setup_cycle();
}
// Adiciona um pequeno delay para evitar múltiplas leituras do botão
sleep_ms(200);
}
}
return 0;
}