greencardigan
Senior Member
Hi, I've just run into a little math problem.
I need to read a temp value between a min and max value and then convert it to a value between a different two values.
For example, a temp value of 432 converts to a speed of 50 and a temp value of 480 converts to speed of 200.
I came up with the code below. Is this the best way to go about this??
I need to read a temp value between a min and max value and then convert it to a value between a different two values.
For example, a temp value of 432 converts to a speed of 50 and a temp value of 480 converts to speed of 200.
I came up with the code below. Is this the best way to go about this??
Code:
SYMBOL MAXTEMP = 480
SYMBOL MINTEMP = 432
SYMBOL MAXSPEED = 200
SYMBOL MINSPEED = 50
SYMBOL TEMP = w1
SYMBOL SPEED = w6
readtemp12 4, TEMP
w2 = MAXTEMP - MINTEMP 'temp range
w3 = MAXSPEED - MINSPEED 'speed range
w4 = w3 / w2 'interger part of ratio
SPEED = TEMP - MINTEMP * w4 'convert integar part
w4 = w3 // w2 'remainder of ratio
SPEED = TEMP - MINTEMP * w4 / w2 + SPEED 'convert remainder part + interger part
SPEED = SPEED + MINSPEED