Explanation of map function is wrong

I found dozens of Youtube tutorials and they all follow your guide on Map ( ), but the function behaves not as it should behave. I know the internals are perfectly logical from a programmers perspective with its head into integer divides, but they are NOT from an engineers perspective.

The most demo-ed example is to map a potentiometer to a LED PWM value. 99% of tutorials does this:

output=map(input,0,1023,0,255);

But because of integer divide behaviour, this does not lead to the desired result. Value 0-4 turn into 0, where only 1023 becomes 255. Another example:

output=map(input,0,1023,0,100);

This example will NEVER give 100 as outcome!
I found by testing all sort of combinations, we ALWAYS need to add 1 to both high ends of the ranges, or subtract 1 if the range is in reverse order.
so the LED PWM example done right is (with perfect 1 outcome on every 4 input values evenly distributed):

output=map(input,0,1024,0,256);

and if you want to remap it to say 100 to -100, you should do

output=map(input,0,1024,100,-101);

Can someone please fix the documentation on this?

blnkjns:
The most demo-ed example is to map a potentiometer to a LED PWM value. 99% of tutorials does this:

output=map(input,0,1023,0,255);
output = input / 4;

There, fixed.

I wouldn't constrain() your research to the use of map(), the Arduino reference would be the first pace I'd check.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.