#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the I2C address and LCD dimensions
// Custom level indicator characters (0-100%)
byte levelIcons[6][8] = {
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111
},
{
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111
},
{
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111
},
{
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
B11111
},
{
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
B11111,
B11111
},
{
B00000,
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
}
};
void setup() {
lcd.begin(16, 2); // Initialize the LCD
for (int i = 0; i < 6; ++i) {
lcd.createChar(i, levelIcons[i]); // Create custom characters for level indicators
}
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0); // Set the cursor to the beginning
}
void loop() {
for (int percentage = 0; percentage <= 100; ++percentage) {
displayLevelIndicator(percentage); // Display the level indicator
delay(50); // Adjust the delay for the animation speed
}
delay(1000); // Pause for 1 second
// Clear the LCD after reaching 100%
lcd.clear();
delay(500); // Additional delay for clarity
}
void displayLevelIndicator(int percentage) {
int numBlocks = map(percentage, 0, 100, 0, 5); // Map percentage to the number of blocks (0-5)
lcd.clear(); // Clear the LCD
// Display the level indicator on the upper row
lcd.setCursor(0, 0);
for (int i = 0; i < numBlocks; ++i) {
lcd.write(byte(i)); // Display the custom character based on the level
}
lcd.print(" ");
// Additional information (percentage) on the lower row
lcd.setCursor(0, 1);
lcd.print("Level: ");
lcd.print(percentage);
lcd.print("%");
delay(100);
}