Skip to content

Commit

Permalink
Dim LCD backlight after 6 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielosorio committed May 29, 2023
1 parent d14d954 commit 8714c13
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions wakie.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -75,6 +77,8 @@ void setup() {
void loop() {
readRtc();

handleLcdBacklightDimming();

if (currentMinute != lastMinute) {
lastMinute = currentMinute;
renderDisplay();
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -227,4 +251,4 @@ void disableLcdBacklight() {
void toggleLcdBacklight() {
lcdLedState = !lcdLedState;
digitalWrite(LCD_LED, lcdLedState);
}
}

0 comments on commit 8714c13

Please sign in to comment.