pxt-calliope/docs/reference/pins/map.md

33 lines
1.2 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Map
Re-maps a number from one range to another. That is, a value of ``from low`` would get mapped to ``to low``, a value of ``from high`` to ``to high``, values in-between to values in-between, etc.
Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The `math->clamp` function may be used either before or after this function, if limits to the ranges are desired.
```sig
pins.map(0, 0, 4, 0, 1023);
```
### Parameters
* ``value``: [Number](/microbit/reference/types/number) - the value to map
* ``from low``: [Number](/microbit/reference/types/number) - lower bound of the origin interval
* ``from high``: [Number](/microbit/reference/types/number) - upper bound of the origin interval
* ``to low``: [Number](/microbit/reference/types/number) - lower bound of the target interval
* ``to high``: [Number](/microbit/reference/types/number) - upper bound of the target interval
## Example
Map the value read from the analog pin ``P0`` to an LED index between ``0`` and ``4``.
```blocks
let value1 = pins.analogReadPin(AnalogPin.P0)
let index = pins.map(value1, 0, 1023, 0, 4)
led.plot(0, index)
```
### See also
[analog read pin](/microbit/reference/pins/analog-read-pin)