piclist 1999\02\22\032556a >
Thread: How to display 0 - 100% on an LED bar
www.piclist.com/techref/displays.htm?key=led
BY : Mark Willis email (remove spam text)

For the generic case you can always do it like this, even if it's
non-linear results you want, I'll assume you can figure it out from
here:

If (Input < threshold_00) return 0x00000000;  //  Light 0 LEDs
If (Input < threshold_01) return 0x00000001;  //  Light 1 LED
...
If (Input < threshold_29) return 0x1FFFFFFF;  //  Light all but last
return 0x3FFFFFFF;  //  Light 'em all

(i.e. you could do a logarithmic response like a VU meter this way.)

But, you have huge inconvenient 30-bit numbers to return there -
there's a far easier way, return 0 through 30 and use your shift
register to create the 30-bit numbers.

Also, If it's a linear function, it's really nice if you can do this
using shifts and multiplies (shifts instead of CPU intensive divides.)
So to map 0..65535 to 0..30 LEDs, you want to maybe multiply by 15 then
divide (by right shifting) by 32768, with a little rounding that'll work
out right.  (Makes sense to divide by 256 first, i.e. throw out the low
byte, then do a 16-bit multiply by 15, then add a rounding factor &
finally divide by 128 again, right?)

Now take your 0..30 number "N" - fill the shift register with an
initial 1 and shift it N-1 times over to the left (initial fill with 0