I want to be able to display the altitude in ft ranging from 0ft to 20000ft on my LCD module. The numbers are read from a potentiometer that I can rotate to change the values. At the moment the potentiometer displays from 0 to 1023 but I need to scale these appropriately so they read from 0-20000ft. As well as this I want the line below to display a 'bar graph' to represent the altitude increasing. The LCD has 20 blocks per line so the bar graph can range from 1 block to 20 blocks.
sprintf(buf, "Altitude: %d ", DELVAL2); // display altitude level
lcd_putxy(1,0,buf);
for (delay = 0; delay < 50000; delay++); // introduce a delay
sprintf(buf, "*", DELVAL2 ); // display bar graph
lcd_putxy(2,0,buf);
for (delay = 0; delay < 50000; delay++); // introduce a delay
This is my code so far. It reads the value from the potentiometer DELVAL2
and displays it on the LCD. Can someone please explain a method of how I can scale the data appropriately to produce an altitude and bar graph.
To scale
[0...1023]
to[0...20000]
multiply by 20,000 and then divide by 1023.Code uses
long
multiplication asint
may only be 16-bit on a micro-controller. Not necessary ifINT_MAX == 0x7FFFFFFF
.The
+ 1023/2
is to provide a rounded conversion.