diff --git a/src/Bounce2.h b/src/Bounce2.h index 1a4d7ee..38b5735 100644 --- a/src/Bounce2.h +++ b/src/Bounce2.h @@ -75,6 +75,7 @@ private: inline void toggleStateFlag(const uint8_t flag) {state ^= flag;} inline bool getStateFlag(const uint8_t flag) const {return((state & flag) != 0);} inline bool thresholdPassed(); + inline void updateTime(); public: /*! diff --git a/src/Debounce.cpp b/src/Debounce.cpp index 7da28bd..ecac2f3 100644 --- a/src/Debounce.cpp +++ b/src/Debounce.cpp @@ -29,7 +29,7 @@ void Debouncer::begin(){ #ifdef BOUNCE_LOCK_OUT previous_millis = 0; #else - previous_millis = millis(); + updateTime(); #endif } @@ -48,7 +48,7 @@ bool Debouncer::update(){ bool currentState = readCurrentState(); if(currentState != getStateFlag(DEBOUNCED_STATE)){ - previous_millis = millis(); + updateTime(); changeState(); } } @@ -105,7 +105,7 @@ bool Debouncer::update(){ // Update unstable bit to match readState toggleStateFlag(UNSTABLE_STATE); - previous_millis = millis(); + updateTime(); } return changed(); @@ -133,7 +133,7 @@ bool Debouncer::update(){ */ if(currentState != getStateFlag(UNSTABLE_STATE)){ - previous_millis = millis(); + updateTime(); toggleStateFlag(UNSTABLE_STATE); } else if(thresholdPassed()){ @@ -147,9 +147,7 @@ bool Debouncer::update(){ */ if(currentState != getStateFlag(DEBOUNCED_STATE)){ - - previous_millis = millis(); - + updateTime(); changeState(); } } @@ -206,4 +204,8 @@ bool Debouncer::fell() const { inline bool Debouncer::thresholdPassed(){ return millis() - previous_millis >= interval_millis; +} + +inline void Debouncer::updateTime(){ + previous_millis = millis(); } \ No newline at end of file