From a810456ef153218837effca6833dcd59e8fef96f Mon Sep 17 00:00:00 2001 From: "Adrian A. Baumann" Date: Mon, 29 Jun 2026 01:54:57 +0200 Subject: [PATCH] Discard odd-position '1' bursts: filter wind-up spurious pulse --- src/main.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 52b7807..91abbf0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ // --------------------------------------------------------------------------- #define DIAL_PIN 4 // GPIO connected to the pulse contact (to GND) -#define PULSE_DEBOUNCE_MS 15 // ignore edges faster than this +#define PULSE_DEBOUNCE_MS 100 // ignore edges faster than this #define INTER_PULSE_MS 350 // silence longer than this = digit complete #define INTER_DIGIT_MS 2500 // silence longer than this = number complete, publish @@ -58,6 +58,7 @@ static volatile int dialPulses = 0; static volatile unsigned long dialLastPulse = 0; // millis() of last counted pulse static String dialNumber = ""; static unsigned long dialLastDigit = 0; // millis() of last completed digit +static int dialRawCount = 0; // raw digit count since last publish (odd positions = wind-up spurious) static void IRAM_ATTR onDialPulse() { unsigned long now = millis(); @@ -278,11 +279,17 @@ static void dialLoop() { interrupts(); lastLoggedPulses = 0; + dialRawCount++; // 10 pulses = digit '0'; 1–9 pulses = digits '1'–'9' char digit = '0' + (pulses >= 10 ? 0 : pulses); - dialNumber += digit; - dialLastDigit = now; - Serial.printf("[DIAL] burst end pulses=%d -> digit='%c' number_so_far=%s\n", pulses, digit, dialNumber.c_str()); + bool spurious = (dialRawCount % 2 == 1) && (digit == '1'); + if (spurious) { + Serial.printf("[DIAL] burst end pulses=%d -> digit='%c' (wind-up spurious, discarded)\n", pulses, digit); + } else { + dialNumber += digit; + dialLastDigit = now; + Serial.printf("[DIAL] burst end pulses=%d -> digit='%c' number_so_far=%s\n", pulses, digit, dialNumber.c_str()); + } } // Number complete when no further digits arrive within the timeout @@ -290,6 +297,15 @@ static void dialLoop() { Serial.printf("[DIAL] number complete=%s\n", dialNumber.c_str()); mqttPublishDialed(dialNumber); dialNumber = ""; + dialRawCount = 0; + } + + // If the sequence stalled with no accepted digit (e.g. user wound up then + // stopped), reset the count so the next wind-up is treated as position 1. + if (dialRawCount > 0 && dialNumber.length() == 0 && pulses == 0 + && (now - lastPulse) >= INTER_DIGIT_MS) { + Serial.println("[DIAL] idle reset"); + dialRawCount = 0; } }