From 8c13fd82557dadbe81023c534cd19c31ec40bde4 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Tue, 12 Dec 2017 15:22:42 -0700 Subject: Debouncing finally works (at least, it does on my two test hosts). --- spaghetti-monster/fsm-pcint.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'spaghetti-monster/fsm-pcint.c') diff --git a/spaghetti-monster/fsm-pcint.c b/spaghetti-monster/fsm-pcint.c index c04375a..722cb88 100644 --- a/spaghetti-monster/fsm-pcint.c +++ b/spaghetti-monster/fsm-pcint.c @@ -24,20 +24,19 @@ #include uint8_t button_is_pressed() { - // debounce a little - uint8_t highcount = 0; - // measure for 16/64ths of a ms - for(uint8_t i=0; i (BP_SAMPLES/2)); - //button_was_pressed = result; - return result; + while ((readings != 0) && (readings != 0xFFFFFFFF)); + button_last_state = readings; + return readings; } inline void PCINT_on() { @@ -59,20 +58,15 @@ ISR(PCINT0_vect) { //DEBUG_FLASH; - /* - uint8_t pressed; + // as it turns out, it's more reliable to detect pin changes from WDT + // because PCINT itself tends to double-tap when connected to a + // noisy / bouncy switch (so the content of this function has been + // moved to a separate function, called from WDT only) + // PCINT_inner(button_is_pressed()); - // add event to current sequence - pressed = button_is_pressed(); - PCINT_inner(pressed); - */ - if (! PCINT_since_WDT) { - PCINT_since_WDT = 1; - PCINT_inner(button_is_pressed()); - } } -// should only be called from PCINT and WDT +// should only be called from PCINT and/or WDT // (is a separate function to reduce code duplication) void PCINT_inner(uint8_t pressed) { uint8_t pushed; -- cgit v1.2.3