Make a good progress-bar?

Hi:)

I want to show a progress bar on a display 64x128px, special here is, that i have to always use "complete rows) so the 128px can be adressed individually, but the 64px only in blocks of 8, so "row" is 0,1,2,3,4,5,6,7 not in px like "20".

My current code is like this:

//init
int bar = sampletime/10;    //sample time in ms divided by 10 to get 10% steps
int last_bar = millis();
int bar_level = 85 ;

//event if 100% (sample time reached)
bar_level = 85;
last_bar=millis();



//loop

if (  (millis()+bar) > last_bar ) {

bar_level = bar_level+4; //125px - 85px = 40 /10 = 4px per 10%

DOG.rectangle(85,1,125,1,0); //draw an empty rectangle to clear just that part of the screen

DOG.rectangle(85,1,bar_level,1,255); //draw a filled rectangle to the current position

last_bar =millis();

This works currently, but has a lot of disadvantages

  • if somewhere in the code a DOG.clear (clears complete display) is calles, the bar is invisible until the next 10% step is arrived. This can be up to 1min!
  • When using a short sample period (5s,10s) its clearly visible, if used a long period (10min) it will be "frozen" up to 1min, so it would be better if the steps were smaller at high sample times.

Could someone help me make this function better?

PS. Here is an explanation how the display call works:

DOG.rectangle(start_col, start_page, end_col, end_page, pattern

so if you want to draw rectangle that goes from 0 to 50px, and is 8px high, you have to use:

DOG.rectangle(0,0,50,1,pattern);

pattern can be from 0-255, 0 is "invisible", 255 is black, all in between is some kind of bit pattern

if somewhere in the code a DOG.clear (clears complete display) is calles, the bar is invisible until the next 10% step is arrived. This can be up to 1min!

You need to have a function that clears the screen AND redraws the progress bar. Call that function instead of DOG.clear(). Make that function call another function, drawProgressBar(percentComplete), that actually draws the progress bar.

The argument may actually be a global, instead of passed to the function.

It's not like someone else is going to be sneaking code onto the Arduino that calls DOG.clear() without you knowing it.

DOG?

When using a short sample period (5s,10s) its clearly visible, if used a long period (10min) it will be "frozen" up to 1min, so it would be better if the steps were smaller at high sample times.

You are limited in the number of steps by the number of pixels. It is not clear why your step size is now 4 pixels. At any point, you know (apparently) what percentage of the task is complete. Have your function that does the drawing do it based on percent complete. A 1% will not always result in a visible change.

PaulS:
You are limited in the number of steps by the number of pixels. It is not clear why your step size is now 4 pixels. At any point, you know (apparently) what percentage of the task is complete. Have your function that does the drawing do it based on percent complete. A 1% will not always result in a visible change.

PaulS:
You need to have a function that clears the screen AND redraws the progress bar. Call that function instead of DOG.clear(). Make that function call another function, drawProgressBar(percentComplete), that actually draws the progress bar.

The argument may actually be a global, instead of passed to the function.

It's not like someone else is going to be sneaking code onto the Arduino that calls DOG.clear() without you knowing it.

DOG?
You are limited in the number of steps by the number of pixels. It is not clear why your step size is now 4 pixels. At any point, you know (apparently) what percentage of the task is complete. Have your function that does the drawing do it based on percent complete. A 1% will not always result in a visible change.

I have choosen 4, because its 10% of the complete bar (40px wide), the smalles visible change would be 1, but this would be 4%.

the Clear can be executed for many reasons, like other parts of the code that require major display changes, so i guess the best option would be to add an "or" in the bar function like "|| hastoupdate == 1"; and then skip the adding and just draw?