//Antonio Sergio Castro de Carvalho Junior
#include "pico/stdlib.h"
#include "hardware/timer.h"
#include <stdio.h>
// Configuração de hardware
const uint LED_PIN = 13; // GPIO do LED
const uint BUTTON_PIN = 5; // GPIO do botão (ativo em LOW)
// Variáveis compartilhadas com ISR
volatile int button_press_count = 0; // Contador de pressionamentos
volatile bool led_active = false; // Bloqueio para evitar reentrância
// Temporizador: Pisca LED a 10Hz por 10s
bool blink_led_callback(struct repeating_timer *t) {
static int blink_count = 0;
gpio_put(LED_PIN, !gpio_get(LED_PIN)); // Alterna estado do LED
blink_count++;
if (blink_count >= 100) { // 100 ciclos = 10s
gpio_put(LED_PIN, false); // Desliga LED
led_active = false; // Libera controle
printf("LED desligado após piscar por 10 segundos\n"); //
return false; // Encerra temporizador
}
return true; // Continua ciclo
}
// Verifica botão a cada 100ms
bool repeating_timer_callback(struct repeating_timer *t) {
static absolute_time_t last_press = 0;
static bool btn_prev = false; // Estado anterior
bool btn_now = !gpio_get(BUTTON_PIN); // Lê botão (LOW = pressionado)
// Detecta borda de subida com debounce de 200ms
if (btn_now && !btn_prev && (absolute_time_diff_us(last_press, get_absolute_time()) > 200000)) {
last_press = get_absolute_time();
button_press_count++;
printf("Botão pressionado %d vezes\n", button_press_count); //
// Aciona LED após 5 pressionamentos
if (button_press_count == 5 && !led_active) {
led_active = true;
printf("LED piscando a 10 Hz por 10 segundos\n");
static struct repeating_timer blink_timer;
add_repeating_timer_ms(100, blink_led_callback, NULL, &blink_timer);
button_press_count = 0; // Reinicia contador
}
}
btn_prev = btn_now; // Atualiza estado anterior
return true;
}
int main() {
stdio_init_all(); // Inicia comunicação serial
sleep_ms(100);
printf("SISTEMA INICIADO\n");
// Configura hardware
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, 0);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN); // Ativa resistor pull-up
// Inicia monitoramento do botão
struct repeating_timer timer;
add_repeating_timer_ms(100, repeating_timer_callback, NULL, &timer);
// Loop principal vazio - controle por interrupções
while (true) {
tight_loop_contents();
}
}
Loading
pi-pico-w
pi-pico-w
A