#include <stdio.h>
#include "pico/stdlib.h"
const uint LED = 12;
const uint BTN = 6;
//static volatile uint a = 0;
static volatile bool control_1 = false;
static volatile bool control_2 = false;
static volatile bool control_3 = false;
static volatile int control_4 = 0;
static void gpio_irq_handler(uint gpio, uint32_t events);
int main () {
stdio_init_all();
gpio_init(LED);
gpio_set_dir(LED, GPIO_OUT);
gpio_init(BTN);
gpio_set_dir(BTN, GPIO_IN);
gpio_pull_down(BTN);
gpio_set_irq_enabled_with_callback(BTN, GPIO_IRQ_EDGE_RISE, true, &gpio_irq_handler);
while (true) {
//printf("A");
//sleep_ms(1000);
if (control_1) {
//sleep_ms(100);
gpio_put(LED, true);
control_2 = true;
if (control_3) {
sleep_ms(500);
control_4++;
control_3 = false;
printf("%d\n", control_4);
}
} else {
//sleep_ms(100);
gpio_put(LED, false);
control_2 = false;
if (control_3) {
sleep_ms(500);
control_4++;
control_3 = false;
printf("%d\n", control_4);
}
}
}
return 0;
}
void gpio_irq_handler(uint gpio, uint32_t events) {
if (control_1 == false && control_2 == false) {
control_1 = true;
control_3 = true;
} else if (control_1 == true && control_2 == true) {
control_1 = false;
control_3 = true;
}
return control_1;
}