#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27, 20 columns, 4 rows
const int analogInputPin = A0; // Analog input pin for the microphone sensor
const int graphWidth = 19; // Width of the bar graph
byte G[8] = {
B11111,
B00000,
B11111,
B11111,
B11111,
B11111,
B00000,
B11111
};
byte T[8] = {
B11000,
B11100,
B11110,
B11111,
B11111,
B11110,
B11100,
B11000
};
void setup() {
lcd.begin(20, 4); // Initialize the LCD
lcd.createChar(1, G);
lcd.createChar(2, T);
lcd.clear();
lcd.print(" Arduino VU Meter");
delay(2000);
}
void loop() {
displayVolume();
//delay(100); // Adjust the delay based on your application
}
void displayVolume() {
int sensorValue = analogRead(analogInputPin);
int volume = map(sensorValue, 0, 1023, 3, 20);
lcd.setCursor(0, 3);
lcd.print("VU ");
lcd.setCursor(volume + 1, 3);
for (int i = volume; i < 20; i++) {
lcd.write(byte(0));
}
lcd.setCursor(3, 3);
for (int i = 3; i < volume; i++) {
if (i != volume - 1) {
lcd.write(1);
} else {
lcd.write(2);
}
}
}