#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pwm.h"
// Pin definitions and configurations
const uint LED_PIN = 13; // LED pin
const uint BTN_PIN = 5; // Button pin
const uint16_t PERIOD = 1000; // Period for 10 Hz
const float DIVIDER_PWM = 16.0; // PWM clock divider
volatile uint click_count = 0; // Click counter
volatile absolute_time_t last_press_time; // Stores the last click time
// PWM configuration
void setup_pwm() {
uint slice = pwm_gpio_to_slice_num(LED_PIN);
gpio_set_function(LED_PIN, GPIO_FUNC_PWM);
pwm_set_clkdiv(slice, DIVIDER_PWM);
pwm_set_wrap(slice, PERIOD);
pwm_set_gpio_level(LED_PIN, 0); // LED initially off
pwm_set_enabled(slice, true); // Enable PWM
}
// Button interrupt callback
void gpio_callback(uint gpio, uint32_t events) {
if (gpio == BTN_PIN && (events & GPIO_IRQ_EDGE_RISE)) {
absolute_time_t now = get_absolute_time();
if (absolute_time_diff_us(last_press_time, now) > 200000) { // Debounce: 200ms
last_press_time = now; // Update last click time
click_count++; // Increment counter
}
}
}
// Function to blink the LED at 10 Hz for 10 seconds
void blink_led_10hz_for_10s() {
uint slice = pwm_gpio_to_slice_num(LED_PIN); // Get the PWM slice
uint64_t start_time = to_ms_since_boot(get_absolute_time()); // Initial time
while (to_ms_since_boot(get_absolute_time()) - start_time < 10000) { // While 10 seconds haven't passed
pwm_set_gpio_level(LED_PIN, PERIOD / 2); // 50% duty cycle (LED on)
sleep_ms(50); // Half period (50 ms on)
pwm_set_gpio_level(LED_PIN, 0); // 0% duty cycle (LED off)
sleep_ms(50); // Half period (50 ms off)
}
}
int main() {
stdio_init_all();
// Button configuration
gpio_init(BTN_PIN);
gpio_set_dir(BTN_PIN, GPIO_IN);
gpio_pull_down(BTN_PIN);
gpio_set_irq_enabled_with_callback(BTN_PIN, GPIO_IRQ_EDGE_RISE, true, &gpio_callback);
// PWM configuration
setup_pwm();
// Main loop
while (true) {
if (click_count == 5) {
printf("Botão pressionado 5 vezes\n");
click_count = 0; // Reset counter
blink_led_10hz_for_10s(); // Blink LED
}
sleep_ms(10); // Small pause to avoid overloading the loop
}
}