Skip to content

Instantly share code, notes, and snippets.

@Duckle29
Last active December 16, 2015 00:09
Show Gist options
  • Select an option

  • Save Duckle29/5345368 to your computer and use it in GitHub Desktop.

Select an option

Save Duckle29/5345368 to your computer and use it in GitHub Desktop.
A function for making progress bars on the arduino. Plenty of bugs to work out, but not necessarily deal breaking. Further explanation in the comments, since github doesn't allow github markdown in the description.
byte bar1[8] = {
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b10000,
0b10000
};
byte bar2[8] = {
0b11000,
0b11000,
0b11000,
0b11000,
0b11000,
0b11000,
0b11000,
0b11000
};
byte bar3[8] = {
0b11100,
0b11100,
0b11100,
0b11100,
0b11100,
0b11100,
0b11100,
0b11100
};
byte bar4[8] = {
0b11110,
0b11110,
0b11110,
0b11110,
0b11110,
0b11110,
0b11110,
0b11110
};
byte bar5[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
void progBar(LiquidCrystal lcd, int percent, int parts, int subparts)
{
int totalParts = subparts * parts;
int segmentProgress = totalParts * percent / 100;
int x = segmentProgress / subparts;
for(int i=0; i<x; i++)
{
lcd.write((byte)4);
}
switch(segmentProgress % subparts)
{
case 0:
break;
case 1:
lcd.write((byte)0);
break;
case 2:
lcd.write((byte)1);
break;
case 3:
lcd.write((byte)2);
break;
case 4:
lcd.write((byte)3);
break;
}
int z = parts-x;
Serial.println(z);
for(int i=0; i<z; i++)
{
lcd.print(" ");
}
}
@Duckle29
Copy link
Author

Duckle29 commented Apr 9, 2013

List of known bugs:

  • It makes some curious characters on the LCD. Characters that I haven't specified in the code.
  • Using a mapped value from 0 -> 100 it works great, but when using a for loop, it makes a very weird bug.

Usage:

To use this function you first need to set up the custom characters. All you need to do is to go to your setup function, and do:
lcd.createChar(0, bar1);
lcd.createChar(1, bar2);

do that for bar3, 4, and 5 too.

  • Replace lcd with the name your LCD object has.
  • You will also need to include the Liquid Crystal library, and setup your LCD as usual.
  • Then use the function is showBar(LiquidCrystal, int, int, int);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment