From 8714c13a2f916e139e1552f2de1801ae14854459 Mon Sep 17 00:00:00 2001 From: Gabriel Osorio Date: Sun, 28 May 2023 23:48:35 -0300 Subject: [PATCH] Dim LCD backlight after 6 seconds --- wakie.ino | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/wakie.ino b/wakie.ino index 25ba5f8..2844523 100644 --- a/wakie.ino +++ b/wakie.ino @@ -49,6 +49,8 @@ bool alarmDeactivated = false; #define LCD_LED 10 uint8_t lcdLedState = HIGH; +int backlightDimTimeout = 6000; // Milliseconds +unsigned long nextBacklightDimTimestamp = 0; #define PIEZO 11 const uint8_t noteBitmapRows = 2; @@ -75,6 +77,8 @@ void setup() { void loop() { readRtc(); + handleLcdBacklightDimming(); + if (currentMinute != lastMinute) { lastMinute = currentMinute; renderDisplay(); @@ -89,7 +93,6 @@ void loop() { // Outside of the alarm window reset the // alarmDeactivated check if it's enabled if (alarmDeactivated) { - Serial.println("Resetting button"); alarmDeactivated = false; } } @@ -214,6 +217,27 @@ void handleCurrentNoteOff() { delay(noteDuration); } +void handleLcdBacklightDimming() { + if (lcdBacklightIsOn()) { + if (nextBacklightDimTimestamp == 0) { + // Arm a timeout to dim the backlight after the set interval + nextBacklightDimTimestamp = millis() + backlightDimTimeout; + } + + // Check if the backlight timeout was reached + if (millis() >= nextBacklightDimTimestamp) { + disableLcdBacklight(); + } + } else { + // Disarm dimming timeout + nextBacklightDimTimestamp = 0; + } +} + +bool lcdBacklightIsOn() { + return lcdLedState == HIGH; +} + void enableLcdBacklight() { lcdLedState = HIGH; digitalWrite(LCD_LED, lcdLedState); @@ -227,4 +251,4 @@ void disableLcdBacklight() { void toggleLcdBacklight() { lcdLedState = !lcdLedState; digitalWrite(LCD_LED, lcdLedState); -} \ No newline at end of file +}