Page 1 of 1
Scaling ranges of numbers
Posted: 15 Jul 2011, 03:39
by mattallen37
With almost any program that involves a motor speed or sensor value, I end up scaling numbers. I don't know if anyone is interested, but for those who are, I
posted on
my blog an NXC function that does the math. Using this, I don't have to come up with everything I need every time. You just enter the original number as well as the lowest and highest the value can be and the lowest and highest you want it to be, and it returns the scaled result.
When I say "lowest and highest you want it to be", I mean the two numbers you want it scaled to. It doesn't matter if the first is higher than the second.
Re: Scaling ranges of numbers
Posted: 15 Jul 2011, 08:42
by HaWe
could you please explain a little more, maybe with some examples? I don't understand your function so far.
Re: Scaling ranges of numbers
Posted: 15 Jul 2011, 11:00
by hergipotter
doc-helmut wrote:could you please explain a little more, maybe with some examples? I don't understand your function so far.
there is an example in the blog post.
Re: Scaling ranges of numbers
Posted: 15 Jul 2011, 11:17
by HaWe
can't find any. can you post it all in code tags? Is easier to read those codes here in this thread anyway.
Re: Scaling ranges of numbers
Posted: 15 Jul 2011, 17:29
by mattallen37
It's a function that takes a range of numbers, and scales it into a different range. Imagine you were to make a PID control loop for something like line following. If the control loop was looking for error values of -5 to 5, and your sensor returned values in the range of 74 to 192, you could scale it like this:
Code: Select all
char error = ScaleRange(Sensor, 74, 192, -5, 5);
Re: Scaling ranges of numbers
Posted: 15 Jul 2011, 17:48
by HaWe