/**
Copyright (c) 2025 Raspberry Pi Pico W - ACFA.
Autoria: ACFA...
*/
#include "pico/stdlib.h"
// Definição dos pinos
#define BUTTON_PIN 15 // Pino do botão
#define LED_PIN 19 // Pino do LED
// Variáveis globais
volatile int button_press_count = 0;
volatile bool led_blinking = false;
volatile absolute_time_t last_press_time;
void button_isr(uint gpio, uint32_t events) {
if (gpio == BUTTON_PIN) {
absolute_time_t now = get_absolute_time();
int64_t diff = absolute_time_diff_us(last_press_time, now);
// Debounce: Ignorar eventos em menos de 200ms
if (diff > 200000) {
button_press_count++;
last_press_time = now;
if (button_press_count >= 5) {
button_press_count = 0; // Resetar contagem
led_blinking = true; // Ativar o comportamento do LED
}
}
}
}
// Função para piscar o LED por 10 segundos
void blink_led(int frequency_hz, int duration_seconds) {
int delay_ms = 1000 / (2 * frequency_hz); // Delay calculado pela frequência
int total_blinks = frequency_hz * duration_seconds;
for (int i = 0; i < total_blinks; i++) {
gpio_put(LED_PIN, 1); // Liga o LED
sleep_ms(delay_ms);
gpio_put(LED_PIN, 0); // Desliga o LED
sleep_ms(delay_ms);
}
}
// Função de configuração do botão e LED
void config_botton_led() {
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN); // Configura o botão como entrada
gpio_pull_up(BUTTON_PIN); // Habilita pull-up interno para o botão
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT); // Configura o LED como saída
gpio_put(LED_PIN, 0); // Garante que o LED comece apagado
}
int main() {
// Inicialização dos pinos
stdio_init_all();
// Função de inicialização do botão e LED
config_botton_led();
// Configuração de interrupção para o botão
last_press_time = get_absolute_time();
gpio_set_irq_enabled_with_callback(BUTTON_PIN, GPIO_IRQ_EDGE_FALL, true, &button_isr);
while (true) {
if (led_blinking) {
blink_led(10, 10); // Piscar LED a 10 Hz por 10 segundos
led_blinking = false; // Desativar o comportamento do LED
}
sleep_ms(100); // Evitar consumo excessivo de CPU
}
}