#include <LiquidCrystal.h>
#define led 13
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // Data pins for LCD.
int pot; // 0 - 1023 potantiometer analog value.
float voltage;
byte bar[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
}; // The "bar" of bar graph.
void setup(){
lcd.clear(); // Clears the LCD screen.
pinMode(led, OUTPUT);
lcd.begin(16, 2); // We have 16 columns and 2 rows.
lcd.createChar(0, bar); // Creating the "bar" character using the byte type variable.
}
void loop(){
pot = analogRead(A0); // Reads the analog value from potantiometer.
delay(20);
voltage = pot*(5.00/1023); // Voltage value changes from 0 to 5 linearly with pot.
float wait = map(voltage, 0, 5, 2000, 200); // 0V --> 2000ms, 5V --> 200ms.
lcd.setCursor(0, 0); // First line first column.
lcd.print(voltage);
lcd.setCursor(4, 0);// First line 5th column.
lcd.print("V");
delay(20);
int end = map(pot, 0, 1023, 0, 15); // Bar graph's limit increases/decreases with pot.
lcd.setCursor(0,1); // Second line first column.
lcd.print(" "); // Instead of clearing the whole screen just clear the second line.
for(int i=0; i<end+1; i++){
lcd.setCursor(i,1); // Selects every column on the second line untill it reaches the "end".
lcd.write(byte(0)); // Writes the "bar" characther we created.
}
// delay(100); // Alternatively, if you uncomment this line and comment the last one LED will blink every
// 2 - 0.2 seconds, but it wont stay off for 2 - 0.2 seconds.
digitalWrite(led, HIGH);
delay(wait); // LED blink speed is controlled by the variable wait.
digitalWrite(led, LOW);
delay(wait); // Delay of 200ms to 2000ms for each period(ON/OFF).
}