Hello, this is my first post on this forum. I am a new arduino user, and through some tutorial I had been guided to use map() function to scale my output from <0,1024> to <1,5>.

Getting weird results I came to the conclusion this function is not working properly OR its use is different than I would thought. I would appreciate if somebody took a closer look to this topic or explained to me why this function is done this way. Here is what I mean:

Having the formula this function uses:

y = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

for example input <200,1000> and output<2,5> I would want this function to work like this:

If:

xe<200,400) y=2

xe<400,600) y=3

xe<600,800) y=4

xe<800,1000) y=5

This seems to be the ideal way you would like to see mapping function work. For every equal interval of input value you get desired output value. Although, how it really works for these values is:

xe<200,466) y=2

xe<466,732) y=3

xe<732,998) y=4

xe<998,1000) y=5

And now this is really weird. I mean mostly the last output value - if you estimate the input values not perfectly when it comes to the upper bound, you will not be able to acquire last out put value. I am positive that the equation given in the topic linked is really the formula used in the function(I mean, it is not ‘rewrite’ mistake) because this is circa what I got using this function in my program.

What seems to be the problem here, is that this functions acts like for n desired ranges it scales it to n-1.

And now my question is: Is this something deeper than I can actually see and does it make any sense? [quess no]. Why would this be correct? In my opinion it’s just wrong…really wrong.

I wrote myself a function which satisfies my purposes, and it’s a little bit different. However, I have not checked how it works with negative numbers.

y = (x - in_min) * (out_max - out_min +1) / (in_max - in_min) + out_min

Thank you for your reply