Map to decimal

I want to nap a value from 0-1023 to 0-1. When I do that with the default map function, I either get 0 or 1. How can I get like 0.23 or 0.9?

Use fmap, or simply divide by 1024.0.

map uses integer arithmetic.

Map() returns a long integer, not a floating point number. Multiply your input number by 100, do the map, then divide the output to a float value.

Untested code:

float result = float(map(val*10, 0, 1023, 0, 100))/100;

Here's something I found on GitHub. Again, untested:

double mapf(double val, double in_min, double in_max, double out_min, double out_max) {
    return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

You’d probably want map(val*10, 0, 1024, 0, 100)

Thank you! Have a nice day!!!!

TheMemberFormerlyKnownAsAWOL: Use fmap, or simply divide by 1024.0.

map uses integer arithmetic.

There's an fmap function? Divide by 1024 is too easy. But it works if the out has to be between 0 and .99999999

You might want to look at this forum post also:

https://forum.arduino.cc/index.php?topic=115303.0

This is the code that works:

float fmap(float x, float a, float b, float c, float d)
{
      float f=x/(b-a)*(d-c)+c;
      return f;
}