Map()

From Microduino Wiki
Jump to: navigation, search
long map  ( long  x,  
  long  in_min,  
  long  in_max,  
  long  out_min,  
  long  out_max   
 )    

Geometric mapping

Map the x during [in_min, in_max] to [out_min, out_max].


Parameters:

  • x The value to be mapped
  • in_min Mapping interval
  • in_max Mapping interval
  • out_min Interval after mapping
  • out_max Interval after mapping

For example, in the following code, map is used to map the analog value from [0,1023] to [0,255]:

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

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

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

[Return to Arduino Syntax Manual]