// #include <stdio.h>
// #include "pico/stdlib.h"
// #include "hardware/gpio.h"
// #define LED_VERDE_PIN 11
// #define LED_AZUL_PIN 12
// #define LED_VERMELHO_PIN 13
// #define BUTTON_A 5
// #define BUTTON_B 6
// // Configuração da interrupção
// // static void gpio_irq_handler(uint gpio, uint32_t events);
// static volatile bool ciclo_ativo = false;
// void gpio_irq_handler(uint gpio, uint32_t events){
// ciclo_ativo = !ciclo_ativo;
// printf("Estou na interrupção! \n");
// printf("ciclo_ativo = %d\n", ciclo_ativo);
// }
// void iniciar_ciclo(){
// printf("ciclo_ativo (iniciar_ciclo) = %d\n", ciclo_ativo);
// if(ciclo_ativo){
// //Led verde ligado por 5 segundos
// gpio_put(LED_VERDE_PIN, true);
// gpio_put(LED_AZUL_PIN, false);
// gpio_put(LED_VERMELHO_PIN, false);
// sleep_ms(5000);
// gpio_put(LED_VERDE_PIN, false);
// gpio_put(LED_AZUL_PIN, false);
// gpio_put(LED_VERMELHO_PIN, true);
// sleep_ms(5000);
// gpio_put(LED_VERDE_PIN, false);
// gpio_put(LED_AZUL_PIN, true);
// gpio_put(LED_VERMELHO_PIN, false);
// sleep_ms(5000);
// }else{
// gpio_put(LED_VERDE_PIN, false);
// gpio_put(LED_AZUL_PIN, false);
// gpio_put(LED_VERMELHO_PIN, false);
// }
// }
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#define LED_VERDE_PIN 11
#define LED_AZUL_PIN 12
#define LED_VERMELHO_PIN 13
#define BUTTON_A 5
#define BUTTON_B 6
static volatile bool ciclo_ativo = false;
static int estado_led = 0; // Controla qual LED está ativo
bool timer_callback(struct repeating_timer *t) {
if(ciclo_ativo) {
switch(estado_led) {
case 0: // Verde
gpio_put(LED_VERDE_PIN, true);
gpio_put(LED_AZUL_PIN, false);
gpio_put(LED_VERMELHO_PIN, false);
break;
case 1: // Vermelho
gpio_put(LED_VERDE_PIN, false);
gpio_put(LED_AZUL_PIN, false);
gpio_put(LED_VERMELHO_PIN, true);
break;
case 2: // Azul
gpio_put(LED_VERDE_PIN, false);
gpio_put(LED_AZUL_PIN, true);
gpio_put(LED_VERMELHO_PIN, false);
break;
}
estado_led = (estado_led + 1) % 3; // Alterna entre 0, 1 e 2
} else {
gpio_put(LED_VERDE_PIN, false);
gpio_put(LED_AZUL_PIN, false);
gpio_put(LED_VERMELHO_PIN, false);
}
return true; // continua o timer
}
void gpio_irq_handler(uint gpio, uint32_t events) {
ciclo_ativo = !ciclo_ativo;
printf("Estou na interrupção!\n");
printf("ciclo_ativo = %d\n", ciclo_ativo);
}
void controlar_leds() {
struct repeating_timer timer;
// Configura timer para 5 segundos (5000ms)
add_repeating_timer_ms(5000, timer_callback, NULL, &timer);
while(true) {
tight_loop_contents(); // Mantém o processador ocupado
}
}
// ... resto do código (setup_pin e main) permanece igual ...
void setup_pin(){
gpio_init(LED_VERMELHO_PIN);
gpio_set_dir(LED_VERMELHO_PIN, GPIO_OUT);
gpio_init(LED_VERDE_PIN);
gpio_set_dir(LED_VERDE_PIN, GPIO_OUT);
gpio_init(LED_AZUL_PIN);
gpio_set_dir(LED_AZUL_PIN, GPIO_OUT);
gpio_init(BUTTON_A);
gpio_set_dir(BUTTON_A, GPIO_IN);
gpio_pull_up(BUTTON_A);
gpio_init(BUTTON_B);
gpio_set_dir(BUTTON_B, GPIO_IN);
gpio_pull_up(BUTTON_B);
}
int main() {
stdio_init_all();
setup_pin();
printf("Passei aqui no main!\n");
gpio_set_irq_enabled_with_callback(BUTTON_A, GPIO_IRQ_EDGE_RISE, true, &gpio_irq_handler);
gpio_set_irq_enabled_with_callback(BUTTON_B, GPIO_IRQ_EDGE_RISE, true, &gpio_irq_handler);
while(true){
controlar_leds();
sleep_ms(10);
}
return 0;
}