From f915d053411f03c11573567fd247787d32c696a5 Mon Sep 17 00:00:00 2001 From: Charles Hill Date: Mon, 10 Oct 2022 19:42:27 +0100 Subject: [PATCH] Debounce hx616 pulse counting to prevent extraneous accumulated value Don't count pulses caused by reconnecting coin signal pin Or powering on the hx616 Or changing the physical switches on the hx616 Still possible but less likely as pulse high/low must be 20-200ms --- src/coin-acceptor/hx616.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/coin-acceptor/hx616.cpp b/src/coin-acceptor/hx616.cpp index 388bfdf..f73be7d 100644 --- a/src/coin-acceptor/hx616.cpp +++ b/src/coin-acceptor/hx616.cpp @@ -13,8 +13,20 @@ namespace { uint16_t numPulses = 0; unsigned short coinSignalPin; + uint8_t minPulseTime = 20;// milliseconds + uint8_t maxPulseTime = 200;// milliseconds + unsigned long pinFallTime; + void IRAM_ATTR onPinStateChange() { - numPulses++; + const auto pinState = digitalRead(coinSignalPin); + if (pinState == LOW) { + pinFallTime = millis(); + } else if (pinFallTime > 0) { + const unsigned long diffTime = millis() - pinFallTime; + if (diffTime >= minPulseTime && diffTime <= maxPulseTime) { + numPulses++; + } + } } } @@ -33,7 +45,7 @@ namespace coinAcceptor_hx616 { } else { logger::write("Initializing HX616 coin acceptor..."); pinMode(coinSignalPin, INPUT_PULLUP); - attachInterrupt(coinSignalPin, onPinStateChange, RISING); + attachInterrupt(coinSignalPin, onPinStateChange, CHANGE); state = State::initialized; } }