map number
Use: | map( val, from1, from2, to1, to2 ) | |
Description: | scales a number from one range to another | |
Parameters: | val | number to be mapped |
from1 | input range value 1 | |
from2 | input range value 2 | |
to1 | output range value 1 | |
to2 | output range value 2 | |
Returns: | The number val mapped to the output range | |
Related: | adc() |
Use the map command to scale a number from one range to another
A number will be mapped from the input range to the output range. So that a number that is from1 will be mapped to to1, and a number from2 will be mapped to to2
Example
x = 855
y = map(x, 0, 1023, 1, 100) # maps a number in the range 0-1023 to the range 1-100
print y # prints 85
This example takes a number x which is in the range of 0 to 1023, and maps it to a number that is in the range 0 to 100.
This is useful when converting an analog number from the adc which is in the range of 0 to 1023, and it is required to convert the number so that it is in the range of 1 – 100.