# Arduino map function

There might already be a map function in NETMF but I’m not able to find it, so I stole some code from Arduino.
The map function is useful when you need to map a value from a known range over to another range. For example my control system gives me rudder values for my boat in the range -40 to +40 and I needed to map this over to servo period values ranging from 800 to 2200.
The map function looks like this;

``````private uint map(int x, int in_min, int in_max, int out_min, int out_max)
{
int Puls = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
return (uint)Puls;
}
``````

And it is used like this;

``````
if (_newPos != _currentPos)
{
_currentPos = _newPos;
uint Puls = map(_currentPos, -40, 40, 800, 2200);
Rudder.SetPulse(20 * 1000 * 1000, Puls * 1000);
}
``````

Yeah this has been talked about before. I don’t think one exists yet. Feel free to code one up though.

Chris already posted this (if I’m right).
You might want to add a project page for it so that future users can find it easily?

Well I couldnt find it. -Is it me or is the search function on this forum rather useless?

Search pointed me to this topic, so I do not agree with the search being useless. It works fine if you ask me?

See Chris’ post (3rd one)
[url]http://www.tinyclr.com/forum/4/231/[/url]

``````private long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

A follow up question to the search function.
How does one search for the word ‘map and not get 100% relevance on remap, maping, mapped ect.

I don’t really think there really is a way to manipulate the search in that way.

Me neither.