// https://forum.arduino.cc/t/am-i-on-the-right-track/1354652/
// https://arduinointro.com/lcdcustomcharacter
uint8_t battery[][8] = { // array of custom 5x8 characters
{14, 17, 17, 27, 21, 27, 17, 31}, // battery[0] = "x" not recharging
{14, 17, 17, 17, 17, 17, 17, 31}, // battery[1] = 0/6 empty
{14, 17, 17, 17, 17, 17, 31, 31}, // battery[2] = 1/6
{14, 17, 17, 17, 17, 31, 31, 31}, // battery[3] = 2/6
{14, 17, 17, 17, 31, 31, 31, 31}, // battery[4] = 3/6
{14, 17, 17, 31, 31, 31, 31, 31}, // battery[5] = 4/6
{14, 17, 31, 31, 31, 31, 31, 31}, // battery[6] = 5/6
{14, 31, 31, 31, 31, 31, 31, 31} // battery[7] = 6/6 full
};
byte batteryArraySize = sizeof(battery) / sizeof(battery[0]);
byte iconCounter; // generic iconCounter for battery icon animation
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define RECHARGER_PIN 3
float reading[5]; // five readings: V, A, Hz, %, recharge
unsigned long iconTimer, iconTimeout = 125;
bool connected; // recharger is connected
void setup() {
lcd.init(); // I2C runs init() before createChar(), non-I2C runs createChar() before begin()
lcd.backlight(); // turn backlight on
pinMode(RECHARGER_PIN, INPUT_PULLUP); // configure Recharger pin
for (int i = 0; i < batteryArraySize; i++) // eight "battery" custom characters...
lcd.createChar(i, battery[i]); // ...stored in "battery[]" array
displayLobo(); // show welcome screen
displayIcons(); // show the battery states
flashBacklight();
delay(2000);
lcd.clear();
displayGauge(); // populate the gauge
}
void loop() {
readSensors(); // read all sensors
displaySensors(); // display all sensors
}
void readSensors() {
for (byte i = 0; i < 4; i++) // four analog inputs
reading[i] = analogRead(i + 14); // A0 through A3 (D14 - D17)
}
void displaySensors() {
// for wokwi simulation with four potentiometers
displayVolts(map(reading[0], 0, 1023, 0, 18)); // volts
displayAmps(map(reading[1], 0, 1023, 0, 100)); // amps
displayHertz(map(reading[2], 0, 1023, 0, 160)); // hertz
displayCharge(map(reading[3], 0, 1023, 0, 60)); // charge
// live
// displayVolts(reading[0]); // volts
// displayAmps(reading[1]); // amps
// displayHertz(reading[2]); // hertz
// displayCharge(reading[3]); // charge
bool rechargerConnected = digitalRead(RECHARGER_PIN); // read button pin
delay(150);
if (rechargerConnected == LOW) // connected
connected = 1;
else
connected = 0;
displayRecharging(connected); // recharging = (0) connected or (1) not connected
}
void displayGauge() {
lcd.setCursor(6, 0); lcd.print("V");
lcd.setCursor(6, 1); lcd.print("A");
lcd.setCursor(12, 0); lcd.print("Hz");
lcd.setCursor(12, 1); lcd.print("%");
lcd.setCursor(15, 1); lcd.write(0); // "x" not recharging
}
void displayVolts(float value) {
lcd.setCursor(0, 0);
spacepad(value);
lcd.print(value, 2);
}
void displayAmps(float value) {
lcd.setCursor(0, 1);
spacepad(value);
lcd.print(value, 2);
}
void displayHertz(int value) {
lcd.setCursor(9, 0);
spacepad(value);
lcd.print(value);
}
void displayCharge(int value) {
lcd.setCursor(9, 1);
spacepad(value);
lcd.print(value);
}
void displayRecharging(bool connected) {
lcd.setCursor(15, 1); // battery icon location
if (connected) { // if the recharger is connected
if (millis() - iconTimer > iconTimeout) { // start battery icon loop
iconTimer = millis(); // reset timer
iconCounter++;
if (iconCounter == batteryArraySize)
iconCounter = 1; // "empty" icon is "1" (disconnect is "0")
lcd.setCursor(15, 1);
lcd.write(iconCounter);
}
} else {
lcd.write(0);
}
}
void spacepad(float value) {
if (value < 100.0) lcd.print(" ");
if (value < 10.0) lcd.print(" ");
}
void displayLobo() {
lcd.clear(); // clear LCD
lcd.setCursor(2, 0);
// k i l l i t l o b o
byte lobo[] = {171, 234, 218, 218, 32, 234, 197, 32, 218, 219, 226, 219};
for (byte i = 0; i < sizeof(lobo) / sizeof(lobo[0]); i++)
lcd.write(lobo[i]);
// lcd.print("Kill it Lobo"); // another way to display this
}
void displayIcons() {
lcd.setCursor(4, 1);
for (int i = 0; i < batteryArraySize; i++)
lcd.write(i);
}
void flashBacklight() {
for (int i = 0; i < 3; i++) { // flash backlight three times
lcd.noBacklight(); delay(250);
lcd.backlight(); delay(250);
}
}
V
A
Hz
%
BAT