Skip to content

Instantly share code, notes, and snippets.

@evanre
Created August 30, 2018 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanre/3955c0ddf4b25ae01f5df04e2818d3e5 to your computer and use it in GitHub Desktop.
Save evanre/3955c0ddf4b25ae01f5df04e2818d3e5 to your computer and use it in GitHub Desktop.
Re-maps a number from one range to another
/**
* map_value()
*
* Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.
* @url: https://www.arduino.cc/reference/en/language/functions/math/map/
*
* @param $val integer - the number to map
* @param $in_min integer - the lower bound of the value’s current range
* @param $in_max integer - the upper bound of the value’s current range
* @param $out_min integer - the lower bound of the value’s target range
* @param $out_max integer - the upper bound of the value’s target range
*
* @return float|int - The mapped value.
*/
function map_value($val, $in_min, $in_max, $out_min, $out_max) {
return floor(($val - $in_min) * ($out_max - $out_min) / ($in_max - $in_min) + $out_min);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment