Created
August 30, 2018 15:35
-
-
Save evanre/3955c0ddf4b25ae01f5df04e2818d3e5 to your computer and use it in GitHub Desktop.
Re-maps a number from one range to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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