Actually with a bit of thought we can simplify things even more. Try this:-
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //initialize the library, numbers of the interface pins
int analogInput = 1; //analog pin A1
float VoltageIn = 0.0; // Used to calculate and display voltage
int refresh = 500; //refresh the display every half second
int i = 0; //variable used for clearing our byte 'graph'
int value = 0; //variable to store value from analogRead
void setup()
{
pinMode(analogInput, INPUT); //setting analog pin mode to input
lcd.begin(16, 2); //setting up the LCD screens rows and colums
lcd.print("Voltage=");
}
void loop(){
value = analogRead(analogInput); //reading the value on A1
VoltageIn = (value * 5.0) / 1024.0;
lcd.setCursor(8, 0); //printing the result to the LCD display
lcd.print(VoltageIn);
lcd.print(" V");
delay(refresh); //refreshing the screen
voltmetergraph(); //calling my function voltmetergraph
}
void voltmetergraph()
{
int position = map(value, 0, 1023, 0, 15);
lcd.setCursor(0,1); // move the cursor to the start of the second line
lcd.print(" "); // clear the second line by writing 15 spaces
lcd.setCursor(position, 1); // Move to the correct position on the line
lcd.write(255); // print a block
}
This works because value has a range of 0 to 1023 and the map converts this to the range 0 to 15 which can be used to position the block on the line.
If you want to draw a bar rather than a single block use this code for voltmetergraph():-
void voltmetergraph()
{
int position = map(value, 0, 1023, 0, 15);
lcd.setCursor(0,1); // move the cursor to the start of the second line
lcd.print(" "); // clear the second line by writing 15 spaces
for (int j = 0; j <= position; j++)
{
lcd.setCursor(j, 1);
lcd.write(255); // print a block
}
}
Ian