Map (value, Fromlow, Fromhigh, Tolow, Tohigh)
Description
Re-maps a number from one range to another. That's, a value offromlow would get mapped to Tolow, a value of Fromhigh to tohigh,values in-between to values in-between , etc.
Transforms a number from one range to another.
Does not constrain values to within the range, because out-of-rangevalues is sometimes intended and useful. The constrain () Functionmay is used either before or after this function, if limits to theranges is desired.
Values are not forced to be scoped, because the values of the hyper-range are often useful. If you need to make a limit on the scope. You can use the constrain () function before or after this function.
Note that the ' lower bounds ' of either range may be larger orsmaller than the ' upper bounds ' so the ' map () function may Usedto reverse a range of numbers, for example
Note that the "nether" in two ranges is larger or lower than the upper bound, so that map () can be used to invert a range, for example
y = map (x, 1, 50, 50, 1);
The function also handles negative numbers well, so that thisexample
Functions can also handle negative numbers, such as
y = map (x, 1, 50, 50,-100);
is also valid and works well.
Also effective and correct
The map () function uses integer math so would not be generatefractions when the math might indicate that it should does so. Fractional remainders is truncated, and is not rounded oraveraged.
The map () function uses integral types, so no fractions are generated, and fractions are truncated, not full or average (?). )
Parameters parameters
Value:the number to map
The value to map
Fromlow:the lower bound of the value ' s current range
The lower bound of the value now
Fromhigh:the upper bound of the value ' s current range
The upper bound of the value now
Tolow:the lower bound of the value ' s target range
The lower bound of the value target range
Tohigh:the upper bound of the value ' s target range
Upper bounds of the value target range
Returns return value
The mapped value.
The mapped value
Example
void Setup () {}
void Loop ()
{
int val = analogread (0); Read the value of 0 port
val = map (val, 0, 1023, 0, 255);//map from 0-1023 to 0-255
Analogwrite (9, Val);//write the mapped value to 9 port
}
Appendix Appendix
For the mathematically inclined, here's the whole function
For mathematically speaking, this is the whole function
Long map (long x, long in_min, long In_max, long out_min, Longout_max)
{
Return (x-in_min) * (out_max-out_min)/(in_max-in_min) +out_min;
}
Map of the Arduino practical mathematical function ()