From 70d3bcfd2a9a65b23290ba47c23e0bcbeb58eee1 Mon Sep 17 00:00:00 2001 From: Sebastian Nieto Date: Mon, 7 Sep 2026 21:27:30 -0300 Subject: [PATCH] Implement smooth non-blocking backlight fade for day/night transitions --- sketch/obd2_gauge/meter.h | 55 +++++++++++++++++++++++--------- sketch/obd2_gauge/obd2_gauge.ino | 2 ++ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/sketch/obd2_gauge/meter.h b/sketch/obd2_gauge/meter.h index ca7145a..dca5201 100644 --- a/sketch/obd2_gauge/meter.h +++ b/sketch/obd2_gauge/meter.h @@ -801,37 +801,62 @@ void updateMeter(uint8_t pidNo, String response) { // update parameter on screen } /*-------------------------------*/ +// Target and current brightness variables for smooth fade +uint8_t targetBacklight = 255; +float currentBacklight = 255.0f; +uint32_t lastBacklightStepTime = 0; + +void updateBacklightFade() { + uint32_t now = millis(); + if (now - lastBacklightStepTime >= 15) { // every 15ms step + lastBacklightStepTime = now; + if (abs(currentBacklight - (float)targetBacklight) > 0.5f) { + if (currentBacklight < targetBacklight) { + currentBacklight += 3.0f; + if (currentBacklight > targetBacklight) + currentBacklight = targetBacklight; + } else { + currentBacklight -= 3.0f; + if (currentBacklight < targetBacklight) + currentBacklight = targetBacklight; + } + ledcWrite(backlightChannel, (uint32_t)currentBacklight); + } + } +} + // auto dim backlight function void autoDim() { int light = analogRead(LDR_PIN); // read light - if (light > LIGHT_LEVEL) { // low light + if (light > LIGHT_LEVEL) { // low light (dark) low_count++; - if (low_count > 10) { - low_count = 10; // low amb light 10 times count + if (low_count > 6) { + low_count = 6; if (!dim) { - ledcWrite(backlightChannel, 50); // dim light + targetBacklight = 50; // smooth fade target: dim dim = true; #ifdef SERIAL_DEBUG - Serial.println(F("Backlight -> dim")); + Serial.println(F("Backlight -> target dim (50)")); #endif - } // if !dim - } // if low_count + } + } high_count = 0; - } else { + } else { // bright light (day) high_count++; - if (high_count > 10) { - high_count = 10; // high amb light 10 times count + if (high_count > 6) { + high_count = 6; if (dim) { - ledcWrite(backlightChannel, 255); // brightest + targetBacklight = 255; // smooth fade target: bright dim = false; #ifdef SERIAL_DEBUG - Serial.println(F("Backlight -> bright")); + Serial.println(F("Backlight -> target bright (255)")); #endif - } // if dim - } // if high_count + } + } low_count = 0; - } // if light > lightlevel + } + updateBacklightFade(); } // autodim //--------------------------- /*############################################*/ diff --git a/sketch/obd2_gauge/obd2_gauge.ino b/sketch/obd2_gauge/obd2_gauge.ino index b67fc28..640ba0c 100644 --- a/sketch/obd2_gauge/obd2_gauge.ino +++ b/sketch/obd2_gauge/obd2_gauge.ino @@ -413,6 +413,8 @@ recent_client_addr : {0x00,0x00,0x00,0x00,0x00,0x00} array of bytes[6] /*###################################*/ void loop() { + updateBacklightFade(); // non-blocking smooth backlight fade handler + // SCAN TOUCHSCREEN & BUTTON // - Single click on a quadrant: toggles between clock (arc) and number // - Double click on a quadrant: puts that instrument in full screen (or exits full screen)