top of page

11 Math Functions

map()

Scales a number from one range to another.


Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired.

Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers, for example

y = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so that this example

y = map(x, 1, 50, 50, -100);

is also valid and works well.

Note: The map() function uses integer math so will not generate fractions. Fractional remainders are truncated, and are not rounded or averaged.

map(value, fromLow, fromHigh, toLow, toHigh)


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


Returns

The scaled value.




© 2021 Odyssey Navigator. All rights reserved.

bottom of page