/**
Copyright (c) 2025 Raspberry Pi Pico W - ACFA.
Autoria: ACFA...
*/
#include "pico/stdlib.h"
// Definição dos pinos
#define BUTTON_PIN_A 15 // Pino do botão A --->>> (button green - verde) esse deve ser pressionado 5X...
#define BUTTON_PIN_B 14 // Pino do botão B --->>> (button blue - azul) esse é o botão de seleção da frequência...
#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_a;
volatile absolute_time_t last_press_time_b;
volatile int led_frequency = 10; //Considere freq. padrão do LED: 10 Hz.
void button_isr(uint gpio, uint32_t events) {
absolute_time_t now = get_absolute_time();
int64_t diff;
if (gpio == BUTTON_PIN_A) {
diff = absolute_time_diff_us(last_press_time_a, now);
// Debounce: Ignorar eventos em menos de 200ms
if (diff > 200000) {
button_press_count++;
last_press_time_a = now;
if (button_press_count >= 5) {
button_press_count = 0; // Resetar contagem
led_blinking = true; // Ativar o comportamento do LED
}
}
}
else if (gpio == BUTTON_PIN_B) {
diff = absolute_time_diff_us(last_press_time_b, now);
// Debounce: Ignorar eventos em menos de 200ms
if (diff > 200000) {
led_frequency = (led_frequency == 10) ? 1 : 10;
last_press_time_b = now;
}
}
}
// Função para piscar o LED
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_button_led() {
gpio_init(BUTTON_PIN_A);
gpio_set_dir(BUTTON_PIN_A, GPIO_IN); // Configura o botão como entrada
gpio_pull_up(BUTTON_PIN_A); // Habilita pull-up interno para o botão
gpio_init(BUTTON_PIN_B);
gpio_set_dir(BUTTON_PIN_B, GPIO_IN); // Configura o botão como entrada
gpio_pull_up(BUTTON_PIN_B); // 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_button_led();
// Configuração de interrupção para o botão
last_press_time_a = get_absolute_time();
last_press_time_b = get_absolute_time();
gpio_set_irq_enabled_with_callback(BUTTON_PIN_A, GPIO_IRQ_EDGE_FALL, true, &button_isr);
gpio_set_irq_enabled_with_callback(BUTTON_PIN_B, GPIO_IRQ_EDGE_FALL, true, &button_isr);
while (true) {
if (led_blinking) {
blink_led(led_frequency, 10); // Piscar LED a led_frequency Hz por 10 segundos
led_blinking = false; // Desativar o comportamento do LED
}
sleep_ms(100); // Evitar consumo excessivo de CPU
}
}