Skip to content

Commit

Permalink
Debounce hx616 pulse counting to prevent extraneous accumulated value
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chill117 committed Oct 10, 2022
1 parent 01ec962 commit f915d05
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/coin-acceptor/hx616.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
}
}

Expand All @@ -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;
}
}
Expand Down

0 comments on commit f915d05

Please sign in to comment.