1. Map function
map fuction in arduinofunction:
map(value,fromLow,fromHigh,toLow,toHigh);
Re-maps a number from one range to another range
- a value of fromLow would get mapped to toLow
- a value of fromHigh to toHigh
- value in between to values in between
It's Parameters
+value: the number to map
+fromLow: the Lower bound of the value's current range
+fromHigh: the Upper bound of the value's current range
+toLow: the Lower bound of the value's target range
+toHigh: the Upper bound of the value's target range
Example:
void setup() {}
void loop()
{
int val = analogRead(0);
val = map(val, 0, 1023, 0, 255);
analogWrite(13, val);
}
fromLow to toLow (0 to 0);
fromHigh to toHigh (1023 to 255);
Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers, for example
y = map(x, 0, 1023, 255, 0);
The function also handles negative numbers well, so that this example
y = map(x, 1, 100, 0, -255);
is also valid and works well.
thank you! for watching
0 Comments