PBasic Help
G McMurry
Posts: 134
in BASIC Stamp
Part of my current project, is to clean up some of my pBasic code.
I want to convert 0-255 to a percent of 255.
I have a fuel sender that shows me FULL at DEC 255 and EMPTY at DEC 000.
The variable is FUEL
I am trying [fuel = 100/(255/fuel) . I would like 128 to show me 050 or 50%.
I know I am having integer math problems. I need another method of making this conversion.
Anyone have a suggestion? Sorry, I was surfing during the 60's in So Cal during my high school math classes and my major @ university was music...
Greg
I want to convert 0-255 to a percent of 255.
I have a fuel sender that shows me FULL at DEC 255 and EMPTY at DEC 000.
The variable is FUEL
I am trying [fuel = 100/(255/fuel) . I would like 128 to show me 050 or 50%.
I know I am having integer math problems. I need another method of making this conversion.
Anyone have a suggestion? Sorry, I was surfing during the 60's in So Cal during my high school math classes and my major @ university was music...
Greg
Comments
By multiplying your level by 100 first, you take care of the fractional problem. The only caveat is making sure that intermediate values in your calculations will fit into 16 bits. In this case, the largest intermediate value is 25500, so that's not a problem.
I use variables in a string so for me:
NemoString(18) = 100 * Nemostring(18)/255
Thanks Jon!