Hi everyone,
I was interested in creating a Bar Graph on my LCD (using the standard Library)... Googling and searching on the forum i found some interesting stuff, including a fantastic library.
Anyway i found them too complicated and I just decided to write a simple and user-friendly function which can be implemented in the sketch code simply copying and pasting it, without having to include a whole library, you just need to initialize the LCD library with the pins connected to the display.
It has four parameters:
LCD_progress_bar (int row, int var, int minVal, int maxVal)
The Row to use, the Variable to adapt to, the Minimum and the Maximum value of the variable.
void LCD_progress_bar (int row, int var, int minVal, int maxVal)
{
int block = map(var, minVal, maxVal, 0, 16); // Block represent the current LCD space (modify the map setting to fit your LCD)
int line = map(var, minVal, maxVal, 0, 80); // Line represent the theoretical lines that should be printed
int bar = (line-(block*5)); // Bar represent the actual lines that will be printed
/* LCD Progress Bar Characters, create your custom bars */
byte bar1[8] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10};
byte bar2[8] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18};
byte bar3[8] = { 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C};
byte bar4[8] = { 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E};
byte bar5[8] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};
lcd.createChar(1, bar1);
lcd.createChar(2, bar2);
lcd.createChar(3, bar3);
lcd.createChar(4, bar4);
lcd.createChar(5, bar5);
for (int x = 0; x < block; x++) // Print all the filled blocks
{
lcd.setCursor (x, row);
lcd.write (1023);
}
lcd.setCursor (block, row); // Set the cursor at the current block and print the numbers of line needed
if (bar != 0) lcd.write (bar);
if (block == 0 && line == 0) lcd.write (1022); // Unless there is nothing to print, in this case show blank
for (int x = 16; x > block; x--) // Print all the blank blocks
{
lcd.setCursor (x, row);
lcd.write (1022);
}
}
I know it is very simple, but it works fine and smoothly.
I hope this topic will help someone (like me) who is searching for an easy solution to use in his project.
It's not necessary to say that any advices, improvements or critics will be well accepted.
(remember to use a little delay in the loop (e.g. delay (40) to avoid a poor quality printing on the display)