Scaling ranges of numbers

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Scaling ranges of numbers

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Scaling ranges of numbers

Post by HaWe »

could you please explain a little more, maybe with some examples? I don't understand your function so far.
hergipotter
Posts: 48
Joined: 12 Jan 2011, 18:40
Contact:

Re: Scaling ranges of numbers

Post 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.
link to my youtube channel under my avatar!
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Scaling ranges of numbers

Post 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.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Scaling ranges of numbers

Post 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);
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Scaling ranges of numbers

Post by HaWe »

8-)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests