diff --git a/ABounce.cpp b/ABounce.cpp index b763225..c33db7e 100644 --- a/ABounce.cpp +++ b/ABounce.cpp @@ -52,15 +52,16 @@ void ABounce::rebounce(unsigned long interval) int ABounce::update() { - if ( debounce() ) { + unsigned long now = millis(); + if ( debounce(now) ) { rebounce(0); return stateChanged = 1; } // We need to rebounce, so simulate a state change - if ( rebounce_millis && (millis() - previous_millis >= rebounce_millis) ) { - previous_millis = millis(); + if ( rebounce_millis && (now - previous_millis >= rebounce_millis) ) { + previous_millis = now; rebounce(0); return stateChanged = 1; } @@ -82,12 +83,12 @@ int ABounce::read() // Protected: debounces the pin -int ABounce::debounce() { +int ABounce::debounce(unsigned long now) { uint8_t newState = getButton(analogRead(pin)); if (state != newState ) { - if (millis() - previous_millis >= interval_millis) { - previous_millis = millis(); + if (now - previous_millis >= interval_millis) { + previous_millis = now; state = newState; return 1; } diff --git a/ABounce.h b/ABounce.h index fd86410..66b8b13 100644 --- a/ABounce.h +++ b/ABounce.h @@ -66,7 +66,7 @@ class ABounce bool fallingEdge(); protected: - int debounce(); + int debounce(unsigned long now); int getButton(int buttonValue); unsigned long previous_millis, interval_millis, rebounce_millis; uint8_t state;