#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16x2 display
int i = 0;
unsigned long previousMillis = 0; // will store last time
unsigned long backlightMillis = 0; // stores time of last backlight change
bool backlightState = true; // stores the current state of the backlight (on or off)
const long interval = 3000; // interval at which to change the info on LCD (milliseconds)
const long backlightOffDelay = 10000; // time after which the backlight turns off (milliseconds)
const long backlightOnDelay = 10000; // time to keep the backlight on (milliseconds)
const int buttonPin = 19; // GPIO pin for the button
bool lastButtonState = LOW; // previous button state
bool buttonPressed = false; // flag for button press
float voltageBattery = 0.0; // VBatt
float voltageOut = 0.0; // VOut
float currentBattery = 0.0; // IBatt
float currentOut = 0.0; // IOut
float powerBattery = 0.0; // PBatt
float powerOut = 0.0; // POut
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // set button pin as input with pull-up
lcd.init();
lcd.backlight();
lcd.print("DIY UPS project");
delay(2000);
}
void loop() {
unsigned long currentMillis = millis();
bool buttonState = digitalRead(buttonPin); // read the state of the button
// Check for button press to toggle backlight on/off
if (buttonState == LOW && lastButtonState == HIGH) {
// Button pressed, toggle backlight
backlightState = !backlightState;
if (backlightState) {
lcd.backlight(); // turn backlight on
backlightMillis = currentMillis; // reset backlight timer
} else {
lcd.noBacklight(); // turn backlight off
}
delay(200); // debounce delay
}
lastButtonState = buttonState; // update last button state
// Manage backlight off after 10 seconds if no button press
if (backlightState && currentMillis - backlightMillis >= backlightOffDelay) {
lcd.noBacklight(); // turn off backlight after 10 seconds
backlightState = false;
}
// Keep backlight on for 10 seconds after button press
if (!backlightState && currentMillis - backlightMillis >= backlightOnDelay && currentMillis - backlightMillis < backlightOffDelay) {
lcd.backlight(); // turn on backlight for 10 seconds
backlightState = true;
backlightMillis = currentMillis; // reset the timer
}
// Change the LCD display every 3 seconds
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
measure();
switch(i) {
case 0:
infoOne();
break;
case 1:
infoTwo();
break;
case 2:
infoThree();
break;
}
i = (i + 1) % 3;
}
delay(100);
}
void measure() { // TODO: Replace with INA219 code
voltageBattery = (random(1200, 1350) / 100);
voltageOut = (random(1200, 1350) / 100);
}
void infoOne() {
lcd.clear();
lcd.print("Vbat: ");
lcd.print(voltageBattery); // example
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print("Ibat: ");
lcd.print("-500"); // example
lcd.print("mA");
lcd.setCursor(15, 1);
lcd.print("1");
}
void infoTwo() {
lcd.clear();
lcd.print("Vout: ");
lcd.print(voltageOut); // example
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print("Iout: ");
lcd.print("480"); // example
lcd.print("mA");
lcd.setCursor(15, 1);
lcd.print("2");
}
void infoThree() {
lcd.clear();
lcd.print("Pbat: ");
lcd.print("-6200"); // example
lcd.print("mW");
lcd.setCursor(0, 1);
lcd.print("Pout: ");
lcd.print("5856"); // example
lcd.print("mW");
lcd.setCursor(15, 1);
lcd.print("3");
}