#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "hardware/timer.h"
#include "pico/time.h"
const uint LED_BLUE = 12;
const uint BUTTON_A = 5;
const uint BUTTON_B = 6;
uint led_frequency = 1;
bool led_state = false;
struct repeating_timer blink_timer;
uint64_t callback_turn_off_led(alarm_id_t id, void *user_data) {
led_state = false;
return 0;
}
void callback_check_button_a(struct repeating_timer *timer) {
static uint total_num_pressed = 0;
bool user_pressed_button_a = false;
static absolute_time_t previous_time = 0;
uint target = 5;
user_pressed_button_a = gpio_get(BUTTON_A);
if (user_pressed_button_a && absolute_time_diff_us(previous_time, get_absolute_time()) > 200000) {
if (!led_state) {
total_num_pressed+=1;
previous_time = get_absolute_time();
if (total_num_pressed >= target) {
led_state = true;
total_num_pressed = 0;
led_frequency = 10;
add_alarm_in_ms(10000, callback_turn_off_led, NULL, false);
}
}
}
}
bool callback_blink(struct repeating_timer *t) {
static bool high_or_low = false;
gpio_put(LED_BLUE, high_or_low);
if(high_or_low) high_or_low = false;
else high_or_low = true;
return true;
}
void start_blinking() {
int wait_time = (500 / led_frequency);
printf("wait_time for this led is %d ms\n", wait_time);
add_repeating_timer_ms(wait_time, callback_blink, NULL, &blink_timer);
}
void stop_blinking() {
cancel_repeating_timer(&blink_timer);
gpio_put(LED_BLUE, 0);
}
int main() {
stdio_init_all();
gpio_init(LED_BLUE);
gpio_set_dir(LED_BLUE, GPIO_OUT);
gpio_put(LED_BLUE, 0);
gpio_init(BUTTON_A);
gpio_set_dir(BUTTON_A, GPIO_IN);
gpio_pull_down(BUTTON_A);
gpio_init(BUTTON_B);
gpio_set_dir(BUTTON_B, GPIO_IN);
gpio_pull_down(BUTTON_B);
struct repeating_timer timer;
add_repeating_timer_ms(20, callback_check_button_a, NULL, &timer);
bool has_started_blinking = false;
while (true) {
//printf("Hello, Wokwi!\n");
if (gpio_get(BUTTON_B)) {
sleep_ms(50);
if (gpio_get(BUTTON_B)) {
led_frequency = 1;
cancel_repeating_timer(&blink_timer);
if (has_started_blinking) start_blinking();
}
}
if (led_state) {
if (!has_started_blinking) start_blinking();
has_started_blinking=true;
} else {
stop_blinking();
has_started_blinking = false;
}
sleep_ms(1);
}
}Loading
pi-pico-w
pi-pico-w