Go Down

Topic: what is Map instruction do ??? (Read 397 times) previous topic - next topic

dany151

Kindly ... i want to figure out how MAP instruction work in details pls

marco_c

map() takes an input range and maps it to an output range (ie, it scales).

So if the input value was 2 and the range was between 0 and 10, then mapping it to the new range 0 to 100 will return the value 20. Note that if you had 11 in the first instance it would return 110, so you often need to use constrain() if you do not want these values outside the full range.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Grumpy_Mike

Map is a function used by people who can not do kindergarden maths.

retrolefty


Kindly ... i want to figure out how MAP instruction work in details pls


I believe it is included in the arduino core file WMath.cpp and is a function:

Code: [Select]
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;
}


Lefty

robtillaart


Map is for linear mapping. One should note it has a some peculiar behaviour.
To understand its behaviour better run simple tests where you map the index of a for loop to a certain output range:

Code: [Select]

for (int i=-10; i<1100; i++) // typical analogRead values + a bit more
{
  Serial.print(i);
  Serial.println("\t");
  Serial.println(map(i, 0, 1023, 0,100));
}

for (int i=-30; i<200; i++) // temperature range
{
  Serial.print(i);
  Serial.println("\t");
  Serial.println(map(i, 0, 100, 32, 212));
}

for (int i=0; i<100; i++)
{
  Serial.print(i);
  Serial.println("\t");
  Serial.println(map(i, 0, 200, 0,10));
}


Do other tests especially have a look at the behaviour around the endpoints of the ranges.

If you need to do non-linear mapping, MultiMap() can help you, see - http://playground.arduino.cc/Main/MultiMap -
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up