Pages: [1]   Go Down
Author Topic: what is Map instruction do ???  (Read 332 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 109
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1255
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33328
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 211
Posts: 13488
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
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 -
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: