Skip to content

Instantly share code, notes, and snippets.

@giripriyadarshan
Created February 8, 2021 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giripriyadarshan/4bf69b3a6cee5e8329f2d6e0f6ee85aa to your computer and use it in GitHub Desktop.
Save giripriyadarshan/4bf69b3a6cee5e8329f2d6e0f6ee85aa to your computer and use it in GitHub Desktop.
Raspberry Pi Pico gpio_irq button debounce control for pushbuttons
#include "pico/stdlib.h"
bool state;
const uint LED_PIN = 25;
// Debounce control
unsigned long time = to_ms_since_boot(get_absolute_time());
const int delayTime = 50; // Delay for every push button may vary
void inter_test(uint gpio, uint32_t events) {
if ((to_ms_since_boot(get_absolute_time())-time)>delayTime) {
// Recommend to not to change the position of this line
time = to_ms_since_boot(get_absolute_time());
// Interrupt function lines
state = !state;
gpio_put(LED_PIN, state);
}
}
int main() {
stdio_init_all();
// just to know when the board is on ... not related to debounce
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
state = true;
gpio_put(LED_PIN, state);
// interrupt
gpio_init(2);
gpio_pull_up(2);
gpio_set_irq_enabled_with_callback(2, GPIO_IRQ_EDGE_FALL , true, &inter_test);
while(1) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment