Jump to content

Maping sets of numbers function

Recommended Posts

Simple mapping function, used to "map" a value from one set of numbers to another. has been very use-full for mapping analog sensor readings to a usable range of numbers. its helped me in the past for a range of different projects

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

map is taken from the Arduino.cc site


value: the number to map


fromLow: the lower bound of the value's current range


fromHigh: the upper bound of the value's current range


toLow: the lower bound of the value's target range


toHigh: the upper bound of the value's target range




The mapped value.


Map an analog value to 8 bits (0 to 255)

void loop()


int val = analogRead(0);

val1 = map(val2, 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;

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...