From 5665a3dd89c5e67bfefc4c9401238b9c62c7964b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 13 Mar 2026 14:24:12 +0000 Subject: [PATCH] Optimize performance by caching millis() in ABounce::update() - Modified ABounce::debounce() to accept a 'now' timestamp. - Updated ABounce::update() to call millis() once and reuse the value. - Improved timing consistency and reduced function call overhead. Co-authored-by: prestalab <2825421+prestalab@users.noreply.github.com> --- ABounce.cpp | 13 +++++++------ ABounce.h | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) 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;