Mapping one range of numbers to another range
Posted: Tue Feb 09, 2021 8:49 pm
(from Zim)
Changing ranges, like "0 - 1024" to "0 - 3.3" is fairly straight forward, but when the ranges don't start with 0, its more involved.
cicciocb set me straight on this a while back....
eg.
> inMin to inMax is the range to be measured eg. 6 - 120
> out = converted output
> outMin to outMax is the range of the desired output eg. 1 - 5
out = ((in - inMin) / (inMax - inMin) * (outMax - outMin)) + outMin
eg. if sensor reads 15 out of 120 and you want it in a "1 - 5 equivalent"
out = (in - inMin) / (inMax - inMin) * (outMax - outMin) + outMin
out = (15 - 6) / (120 - 6) * (5 - 1) + 1
out = 1.3
Changing ranges, like "0 - 1024" to "0 - 3.3" is fairly straight forward, but when the ranges don't start with 0, its more involved.
cicciocb set me straight on this a while back....
eg.
> inMin to inMax is the range to be measured eg. 6 - 120
> out = converted output
> outMin to outMax is the range of the desired output eg. 1 - 5
out = ((in - inMin) / (inMax - inMin) * (outMax - outMin)) + outMin
eg. if sensor reads 15 out of 120 and you want it in a "1 - 5 equivalent"
out = (in - inMin) / (inMax - inMin) * (outMax - outMin) + outMin
out = (15 - 6) / (120 - 6) * (5 - 1) + 1
out = 1.3