#include <LiquidCrystal.
h>
// LCD pins: RS, EN, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int fuelSensorPin = A0; // Analog pin connected to fuel sensor
// Adjust these to match your sensor’s calibration
const int emptyValue = 100; // Value when tank is empty
const int fullValue = 900; // Value when tank is full
void setup() {
lcd.begin(16, 2);
lcd.print(" Fuel Level Gauge ");
delay(2000);
lcd.clear();
}
void loop() {
int sensorValue = analogRead(fuelSensorPin);
// Convert sensor value to %
int fuelPercent = map(sensorValue, emptyValue, fullValue, 0, 100);
fuelPercent = constrain(fuelPercent, 0, 100);
lcd.clear();
if (fuelPercent <= 15) {
// Always show LOW FUEL
lcd.setCursor(0, 0);
lcd.print("!!! LOW FUEL !!!");
// Flash percentage only
lcd.setCursor(0, 1);
lcd.print("Fuel: ");
static bool showPercent = true; // toggle state
if (showPercent) {
lcd.print(fuelPercent);
lcd.print("% ");
} else {
lcd.print(" "); // blank out % area
}
showPercent = !showPercent; // toggle for next loop
delay(500);
} else {
// Normal display
lcd.setCursor(0, 0);
lcd.print("Fuel: ");
lcd.print(fuelPercent);
lcd.print("% ");
// Bar graph
lcd.setCursor(0, 1);
int bars = fuelPercent / 10;
for (int i = 0; i < 10; i++) {
if (i < bars) {
lcd.write(byte(255)); // full block
} else {
lcd.print(" ");
}
}
delay(1000);
}
}