#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "stdio.h"
#include "hardware/gpio.h"
#include <time.h>
#include "hardware/structs/rosc.h"
int count = 0;
uint32_t ciclo;
static uint64_t now = 0;
static uint64_t last_time = 0;
#define BUTTON_PIN 12
void led_hertz(int hertz)
{
int start_time = to_ms_since_boot(get_absolute_time());
hertz = hertz * 2;
while (to_ms_since_boot(get_absolute_time()) - start_time < 1000)
{
ciclo = 1000 / hertz;
gpio_put(3, 1);
sleep_ms(ciclo);
gpio_put(3, 0);
sleep_ms(ciclo);
}
}
bool debounce() {
uint64_t now = to_us_since_boot(get_absolute_time());
if ((now - last_time) >= 2000)
{
last_time = now;
return true;
}
return false;
}
void app()
{
if (!gpio_get(BUTTON_PIN))
{
if (debounce())
{
count++;
printf("Botão pressionado. Contagem: %d\n", count);
}
while (!gpio_get(BUTTON_PIN))
{
sleep_ms(100);
}
}
if ( count == 5 )
{
for (int i = 0; i <= 10; i++)
{
led_hertz(10);
}
count = 0;
}
}
int main()
{
last_time = to_us_since_boot(get_absolute_time());
stdio_init_all();
gpio_init(3);
gpio_set_dir(3, GPIO_OUT);
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
while (1)
{
app();
sleep_ms(1);
}
}