Please help me understand how the progress bar increments and de-increments according to the analog input (a potentiometer on pin A0). The code is from https://www.electronicsblog.net/. Don't understand how the if, for, while, case, break etc are used to make the progress bar work. I understand the theory of the functions and statements etc but don't understand how it works together to manipulate the progress bar. Note the code change after case 1. Need a line by line explanation. Please help. Many thanks.
//All code
//[code lang=”arduino”]
//https://www.electronicsblog.net/
//Arduino LCD horizontal progress bar using custom characters
#include <LiquidCrystal.h>
#define lenght 16.0
double percent=100.0;
unsigned char b;
unsigned int peace;
// custom charaters
// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7, 8);// change to this for my LCD wiring scheme
byte p1[8] = {
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10};
byte p2[8] = {
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18};
byte p3[8] = {
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C};
byte p4[8] = {
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E};
byte p5[8] = {
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F};
void setup() {
delay(100);
lcd.createChar(0, p1);
lcd.createChar(1, p2);
lcd.createChar(2, p3);
lcd.createChar(3, p4);
lcd.createChar(4, p5);
lcd.begin(16, 2);
}
void loop()
{
lcd.setCursor(0, 0);
//ADC conversion
unsigned int value = analogRead(0);
percent = value/1024.0*100.0;
lcd.print(value);
lcd.print(" – ");
lcd.print(percent);
lcd.print(" % ");
lcd.setCursor(0,1);
double a=lenght/100*percent;
// drawing black rectangles on LCD
if (a>=1) {
for (int i=1;i<a;i++) {
lcd.write(4);
b=i;
}
a=a-b;
}
peace=a*5;
// drawing charater’s colums
switch (peace) {
case 0:
break;
case 1:
//lcd.write(0);
lcd.print((char)0); // This change was made according to instructions at end of sketch
break;
case 2:
lcd.write(1);
break;
case 3:
lcd.write(2);
break;
case 4:
lcd.write(3);
break;
}
//clearing line
for (int i =0;i<(lenght-b);i++) {
lcd.print(" ");
}
;
}
//
//Update for Arduino IDE 1.0 and newer version users
//lcd.write(0); leads to “call of overloaded ‘write(int)’ is ambiguous” //error, so please replace it to lcd.print((char)0);
//This entry was posted in Arduino and tagged bar, Hitachi HD44780, LCD, progress on February 8, //2011.
ElectronicsBlog_LCD.ino (1.83 KB)