Problem with MAP

I tried to MAP something, but it ended up like this.

readL = map(readL, 0, 1023, 0, 65000); Serial.println(readL);

-6763
-6763
-6763
-6763
-6763
-6763
-6763
-6827
-6827
-6827
-6827
-6827
-6827
-6827

Hello
a short cutout:
The map() function uses **integer** math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.

Let me guess - readL is an "int".

Please remember to use code tags when posting all of your code

@kevinxiao0, your topic has been moved to a more suitable location on the forum.

I really dislike the map function, it just obscures something you can do better.
eg readL = readL * 64 or readL = readL <<6

Perhaps you mean 65000L and perhaps you made readL int rather than long?

Oh! The irony.

"So if your project ... please consider avoiding map() and implementing the calculations manually in your code yourself."

Its clear in this case the OP has not considered what the results will be for a particular input.
The map() is doing this: readL = int() readL * 63.4765625

so there will be errors in readL

This is why I dont like map(); because when it gives unexpected results the user - who has not given the conversion any consideration - cant see why.

and does the map() function convert the adc reading correctly? (n+0.5 *range/resolution) (no)

and does the OP expect to now have a resolution of 1 part in 65000?

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