#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// define custom characters - every character is 5x8 "pixels"
byte gauge_left[8] = { B11111, B10000, B10000, B10000, B10000, B10000, B10000, B11111}; // left part of empty gauge [
byte gauge_center[8] = { B11111, B00000, B00000, B00000, B00000, B00000, B00000, B11111}; // center part of empty gauge _
byte gauge_right[8] = { B11111, B00001, B00001, B00001, B00001, B00001, B00001, B11111}; // right part of empty gauge ]
byte gauge_fill[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111}; // filled gauge █
// int cpu_gauge; // value for the CPU gauge
// int gpu_gauge; // value for the GPU gauge
// char buffer[10]; // helper buffer to store C-style strings (generated with sprintf function)
byte bot_fuel_1[8] = {
0b11111,
0b10001,
0b10001,
0b11111,
0b11101,
0b11111,
0b11111,
0b11111,
};
byte bot_fuel_2[8] = {
0b00000,
0b00000,
0b11000,
0b01100,
0b00100,
0b00100,
0b00100,
0b11100,
};
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
lcd.createChar(4,bot_fuel_1);
lcd.createChar(5,bot_fuel_2);
// initialize the 16x2 lcd module
lcd.createChar(0, gauge_left); // create special character on position 0, gauge left [
lcd.createChar(1, gauge_center); // create special character on position 1, gauge center _
lcd.createChar(2, gauge_right); // create special character on position 2, gauge right ]
lcd.createChar(3, gauge_fill); // create special character on position 3, gauge fill █
// enable backlight for the LCD module
}
void loop() {
int value = analogRead(A0); //đọc giá trị điện áp ở chân A0
//(value luôn nằm trong khoảng 0-1023)
Serial.println(value); //xuất ra giá trị vừa đọc
int voltage;
voltage = map(value,0,1023,0,5000); //chuyển thang đo của value
//từ 0-1023 sang 0-5000 (mV)
Serial.println(voltage); //xuất ra điện áp (đơn vị là mV)
Serial.println(); //xuống hàng
lcd.setCursor(0,0);
int f;
// 8 characters to draw the gauge --- ████___]
float voltage_gauge_step = 5000.0/10.0; // 100% is the maximum number, gauge is 8 characters wide, calculate one step
for (int j=0; j<10; j++) {
if (voltage <= voltage_gauge_step*j) { // value is smaller than step*i, draw "empty" character
if (j==0)
{
lcd.write(byte(0)); // [ first cell, opening bracket
f=500;
}
else if (j==9)
{
lcd.write(byte(2)); // ] last cell, closing bracket
}
else
{
lcd.write(byte(1)); // _ any other cell, lines top and bottom
}
}
else {
lcd.write(byte(3)); // value is bigger than step*i, draw filled character - █
f=(j+1)*50+500;
}
}
lcd.setCursor(0, 1);
lcd.write(byte(4));
lcd.write(byte(5));
lcd.print(" Fuel: ");
lcd.print(f);
lcd.print(" l");
delay(50);
}