#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// 5 xil darajadagi tayoqchalar (custom character)
byte bars[6][8] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // 0% (bo‘sh)
{0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10}, // 20%
{0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18}, // 40%
{0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C}, // 60%
{0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E}, // 80%
{0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F} // 100%
};
int potPin = A0;
void setup() {
lcd.init();
lcd.backlight();
// Har bir tayoqchani xotiraga yozish
for (int i = 0; i < 6; i++) {
lcd.createChar(i, bars[i]);
}
}
void loop() {
int val = analogRead(potPin);
int percent = map(val, 0, 1023, 0, 100);
lcd.setCursor(0, 0);
lcd.print("Ovoz: ");
lcd.print(percent);
lcd.print("% "); // Bo‘sh joy qoldirish uchun
lcd.setCursor(0, 1);
int fullBars = percent / 10; // 10% ga bitta katak
int remainder = percent % 10; // 0-9 oraliqda qolgan qism
// To‘la tayoqchalarni chiqarish
for (int i = 0; i < fullBars; i++) {
lcd.write(byte(5)); // 100% tayoqcha
}
// Qisman to‘lgan tayoqcha
if (fullBars < 16) {
int barLevel = remainder / 2; // 0-5 oralig‘ida
lcd.write(byte(barLevel));
fullBars++;
}
// Qolganini bo‘sh kataklar bilan to‘ldirish
for (int i = fullBars; i < 16; i++) {
lcd.write(byte(0)); // Bo‘sh tayoqcha
}
delay(200);
}