Scaling variables

Andy Lehmann

New Member
Hi

I want to scale my potentiometer values from "0-255" to "0-100" because I want to display it on the LCD display as a percentage, which would normally require multiplying the variable by 0.39..... but you can't do that very easily(I don't know how).

If anyone could let me know how to scale stuff and or multiply by decimals or send me a link to somewhere I could find out easily it would be greatly appreciated.

Andy
 

Buzby

Senior Member
Welcome to the forum !

'Times 0.39' is the same as 'times 39, then divide by 100', so put your pot value into a word, multiply by 39, divide by 100, Bob's your uncle !.

for w0 = 0 to 255 step 10

w2 = w0 * 39 / 100

sertxd(#w0, " ",#w2, cr ,lf)
next
 

hippy

Technical Support
Staff member
To scale it's divide by 255, multiply by 100. Or multiply by 100, then divide by 255, which is equivalent.

Your 0.39 is that 100/255 but isn't entirely accurate. So why not just multiply by 100 then divide by 255 which will be as accurate as it can get ...

ReadAdc ADC_PIN, b0
b1 = b0 * 100 / 255

The problem in doing this is where the raw number multiplied by another exceeds the maximum word value of 65535. In the above the maximum 'b0' value is 255, multiplied by 100 that's 25500, lower than the maximum so no problem.

Where it will exceed the maximum the multiplier and divisor can be reduced equally which gives the same result. For example 100 and 255 can both be divided by 5 giving 20 and 51, so this is exactly the same as above ...

ReadAdc ADC_PIN, b0
b1 = b0 * 20 / 51

Another trick is to find the number which multiplies the divisor to be 65536, then multiply the multiplier by that as well. For example, 100 / 255 is roughly the same as 25699 / 65536.

Multiplying anything by 25699 will quickly exceed the maximum and it's not possible to even specify 65536 as a divisor because it's above the maximum value, but the '**' operator of the PICAXE avoids the overflow and provides that division, so another alternative to the case you have is ...

ReadAdc ADC_PIN, b0
b1 = b0 ** 25699
 

Andy Lehmann

New Member
Thank you for the help and the welcome ;)

I think the problem is within the syntax itself, (probably an obvious mistake). but here is what I have tried.

"{LET} Xper = Xvar * 100
{LET} Xper = Xvar / 255"
 

lbenson

Senior Member
Did you mean either

Xper = Xvar * 100
Xper = Xper / 255

or

Xvar = Xvar * 100
Xper = Xvar / 255

or

Xper= Xvar * 100 / 255

Where both Xvar and Xper are defined as word variables?

If not, or if your problem is not fixed, can you explain it more fully?
 

inglewoodpete

Senior Member
Thank you for the help and the welcome ;)

I think the problem is within the syntax itself, (probably an obvious mistake). but here is what I have tried.

"{LET} Xper = Xvar * 100
{LET} Xper = Xvar / 255"
To add to what lbenson has posted, your code above assigns a value to Xper and then immediately overwrites it without referencing the temporary value.
 

AllyCat

Senior Member
Hi,

Also, you do appreciate that the curly braces { } mean that you can either write "LET", or may omit it and just write "Xper = Xvar * 100" etc.?

Cheers, Alan.
 
Top