Hey i have some code to print some value from a accelerometer and i map the accel value to a value between 0 - 255 but when i serial.print the value it exceeds the range even after i used constrain fucntion.
If the input value is outside the from range, the output value will be outside the to range. If that is an issue, using constrain() (properly) is a good idea.
if performance is an issue you can merge constrain and map quite easily.
I wrote this function some time ago.
long strictMap(long val, long inmin, long inmax, long outmin, long outmax)
{
if (val <= inmin) return outmin;
if (val >= inmax) return outmax;
return (val - inmin)*(outmax - outmin)/(inmax - inmin) + outmin;
}
I only do not like the name, thought of constrainedMap() (too long) CMap() (too vague) ...