Maz95:
Thanks man, big help and got it running in the first try! But the bar seem to fill 15 full characters (or blocks ) at 5 volts, and i need to be full at maximum 1.1 volts!, which part of the (dividers?) i need to tune to fullfill all blocks to my desired voltage, im still a little confused to that.. plus in a reddit forum they suggested me using the 3.3 volts reference as it can be more accurate reading this value an using the AREF pin.. Is this true or isnt worth the change?
Thanks for the reading and the help, i need indeed more arduino code understanding...
You'll lose a little accuracy only using 1.1V out of 5V but for your purposes -- a relatively coarse bar graph and voltage display, it probably won't matter. If you're using this data for logging or adjusting fuel trims then I would recommend a different reference.
For now, with some revised scaling:
#include <LiquidCrystal.h>
//make 5 custom characters that fill in a character space from left to right
//to give more progressive bar graph
unsigned char bars[][8] =
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, //_____
{ 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 }, //|____
{ 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18 }, //||___
{ 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c }, //|||__
{ 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e }, //||||_
{ 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f } //|||||
};
int Vpin = 0;
int
nReading,
nlastReading;
float V;
//internal representation of the bar
byte
grBar[16];
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // pines RS, E, D4, D5, D6, D7 de modulo 1602A
void setup()
{
lcd.begin(16, 2); // inicializa a display de 16 columnas y 2 lineas
//use the arrays above to create 5 custom characters representing fractional fills for the bar graph
for( int i=0; i<6; i++ )
lcd.createChar( i, bars[i] );
lcd.setCursor(0,0); //col,row
lcd.print("SondaV= ");
//Serial.begin(9600); //debug/dev
nlastReading = 0;
}//setup
void loop()
{
static unsigned long
timeUpdate = 0;
unsigned long
timeNow;
timeNow = millis();
if( timeNow - timeUpdate >= 100 ) //update frequently (10 times a second) for smooth bar graph
{
timeUpdate = timeNow;
nReading = analogRead( Vpin );
//only update if current reading not the same as last
if( nReading != nlastReading )
{
nlastReading = nReading;
V = (float)nReading*5.0/1024.0;
lcd.setCursor( 8, 0 );
lcd.print( V, 2 );
//Serial.println( V ); //debug/dev
//draw the bar graph
UpdateBar( V );
}//if
}//if
}//loop
void UpdateBar( float volts )
{
int
i,
numBlanking,
numfullbars;
float
fnumSubBars;
//draw the bar graph in its entirety
//assume that full scale voltage reading is 1.1V and that this equates to 15 full bars
// if volts = 0.45
// # of full bars = (int)(0.45 * 13.6364) = (int)(6.136) = 6
//
// The remainder will be used to fractionally fill the next bar
// Our character set allows up to 5 "sub-bars"
// the remainder of the # full bars calc
// multiplier: 0.9 * x = 5; x = 5.5
// 5.5 * ((volts * 13.6364) - numfullbars )
// 5.5 * (6.136 - 6)
// 5.5 * 0.136 = 0.750 (int = 4)
//
//
if( volts > 1.1 )
volts = 1.1;
numfullbars = (int)(volts * 13.6364); //number of "full" bars (custom char #5)
fnumSubBars = 5.5 * ((volts * 13.6364) - numfullbars ); //last char is 0-5, custom char for partial fill of last bar
if( numfullbars < 15 )
numBlanking = 15 - numfullbars - 1;
else
numBlanking = 0;
//erase the array representing the bar by setting custom char 0 (blank)
memset( grBar, 0, sizeof( grBar ) );
//draw in the number of full bars using character 5
for( i=0; i<numfullbars; i++ )
grBar[i] = 5;
//in the last active bar, add the sub-bar value
grBar[i] = (byte)fnumSubBars;
//draw the bar to the LCD
lcd.setCursor( 0, 1 );
for( i=0; i<sizeof( grBar ); i++ )
{
lcd.write( grBar[i] );
//Serial.print( grBar[i] ); //debug/dev
}//for
//Serial.println(); //debug/dev
}//DoBar