//ALESSANDRA KIMIE HIRO.
//EMBARCATEC IFMA - GRUPO 02
//TAREFA 2
//1 - Elabore um programa para acionar um LED quando o botão A for pressionado 5 vezes,
//utilizando o temporizador como contador. Quando o valor da contagem atingir 5 vezes,
//um LED deve ser piscar por 10 segundos na frequência de 10 Hz.
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/cyw43_arch.h"
const uint LED_PIN = 12; // Pino do LED
const uint BUTTON_PIN = 5; // Pino do botão
volatile int button_press_count = 0; // Contador de pressões do botão
volatile bool led_active = false;
volatile bool led_blinking = false;
void init_gpio();
int64_t turn_off_callback(alarm_id_t id, void *user_data) {
gpio_put(LED_PIN, false); // Desliga o LED
led_blinking = false; // Desativa o piscar
return 0;
}
int64_t led_blink_callback(alarm_id_t id, void *user_data) {
static bool led_on = false;
if (led_blinking) {
gpio_put(LED_PIN, led_on); // Alterna o estado do LED
led_on = !led_on; // Alterna a variável para inverter o estado
}
return 100; // Define o intervalo de 100ms (10Hz)
}
void logic() {
static bool button_last_state = false;
bool button_pressed = !gpio_get(BUTTON_PIN);
if (button_pressed && !button_last_state) {
button_press_count++; // Incrementa a contagem ao pressionar o botão
printf("Pressão do botão: %d\n", button_press_count);
if (button_press_count == 5) {
led_blinking = true; // Ativa o piscar do LED
printf("5 pressões detectadas! LED começando a piscar.\n");
add_alarm_in_ms(100, led_blink_callback, NULL, true); // Inicia o piscar a 10Hz
add_alarm_in_ms(10000, turn_off_callback, NULL, false); // Desliga após 10 segundos
button_press_count = 0; // Reseta o contador
}
}
button_last_state = button_pressed; // Atualiza o estado do botão
}
int main() {
stdio_init_all();
clocks_init();
init_gpio();
while (true) {
logic();
sleep_ms(250); // Intervalo de verificação
}
}
void init_gpio() {
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, 0); // Inicializa o LED apagado
// Configura o pino do botão como entrada com pull-up interno
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
}