aboutsummaryrefslogtreecommitdiffstats
path: root/docs/source/lang/map.rst
blob: 79122b3a688c34376388632d02b79d26dce22c75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
.. highlight:: cpp

.. _lang-map:

map()
=====

Re-maps a number from one range to another.

.. contents:: Contents
   :local:

Library Documentation
---------------------

.. doxygenfunction:: map

Discussion
----------

``map()`` does not constrain values to within the range, because
out-of-range values are sometimes intended and useful. The
:ref:`constrain() <lang-constrain>` macro 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 that ``map()`` 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.

The ``map()`` function uses integer math (its arguments and return
values all have type :ref:`long <lang-long>`), so it will not generate
fractions, when the math might indicate that it should do so.
Fractional remainders are truncated, and are not rounded or averaged.

Example
-------

::

    /* Map an ADC reading (12 bits) to 16-bit PWM (0 to 65,535) */

    void setup() {
        pinMode(0, INPUT_ANALOG);
        pinMode(9, PWM);
    }

    void loop() {
        int val = analogRead(0);
        val = map(val, 0, 4095, 0, 65535);
        analogWrite(9, val);
    }


See Also
--------

-  :ref:`constrain() <lang-constrain>`

.. include:: cc-attribution.txt