Loading...
Searching...
No Matches

Rescaling and limiting numbers: map() and constrain(). More...

Functions

 map (value, fromLow, fromHigh, toLow, toHigh)
 Rescales a number from one range to another.
 constrain (value, low, high)
 Limits a number to a range.

Detailed Description

Rescaling and limiting numbers: map() and constrain().

Function Documentation

◆ map()

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

Rescales a number from one range to another.

With whole numbers the result is a whole number, rounded towards zero as on an Arduino; with any float it is a float. Not clamped: use constrain().

Parameters
valueThe number to rescale.
fromLowBottom of the range value is in.
fromHighTop of the range value is in.
toLowBottom of the new range.
toHighTop of the new range.
Returns
The rescaled number.
Example
analogWrite(3, map(analogRead(A0), 0, 1023, 0, 255))

◆ constrain()

constrain ( value,
low,
high )

Limits a number to a range.

Parameters
valueThe number.
lowThe smallest result.
highThe largest result.
Returns
low if value is below it, high if above it, otherwise value.
Example
level = constrain(reading, 0, 255) # keeps level in analogWrite()'s range