 # Map command

hi, can somebody explain to me how does the "map" command work? i don't understand the example... at all

http://arduino.cc/en/Reference/Map

``````Example

/* Map an analog value to 8 bits (0 to 255) */
void setup() {}

void loop()
{
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);
}
``````

This reads the voltage on pin o and puts it into a variable called val.
val can range from 0 to 1023. If you need a different value, like for an LED from 0 to 255.

map takes the value of val and converts to to the new range of values from 0 to 255.

val = map(val, 0, 1023, 0, 255);
0, 1023 is the range of values that a reading from analogRead(0) could be.
0,255 is the range of values you are converting the value to.

Hope this helps.

map(x, a,b, c,d);

imagine a function that maps the range [a..b] on [c...d] on which value will x be mapped?

y = map(a, a,b, c,d); ==> y = c y = map(b, a,b, c,d); ==> y = d y = map(x, a,b, c,d); and x between a and b ==> y is between c and d

does this help?

It translates a value from one range into another. So for instance if you wanted to convert from farenheight to centigrade you would use something like

Important note:

if the x is outside the range [a..b]

then map(x, a,b, c,d) will be outside the range c..d

The map function takes in 5 type long variables.

The first one is [u]your[/u] value. The next two are the supposed high and low range that value is said to go. the last two are the high and low ranges you want to go to.

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; }

So if you have a value that only goes from 0 to 9, and lets say you want to scale that value from 50 to 5000.

``````for( byte i = 0; i < 10; i++)
{
long result = map ( i, 0, 9, 50, 5000);
Serial.print(i);
Serial.print(": ");
Serial.println(result);
}
``````

The map function will equally scale your 0 - 9 range to 50 - 5000. Output:

0: 50 1: 600 2: 1150 3: 1700 4: 2250 5: 2800 6: 3350 7: 3900 8: 4450 9: 5000

Thank you pauly! I came here wondering what map did and you gave me my answer. Thank you!

If you want to have a smooth curve as example to ramp the motor speed slowely up (like ease in/out) you can use my little script i wrote after the map function here: https://forum.arduino.cc/index.php?topic=559445.0