Floating Point Using Map Function

Hi all,

I just wanted to share some code that will allow floating point operations while using the "map" function.
Posted below are some helpful resources.

``````void setup() {

Serial.begin(9600);

}

void loop() {

sensorVal = mapf(sensorVal, sensorMin, sensorMax, -15, 15);
Serial.println(sensorVal);
delay(50);

}

float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
float result;
result = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
return result;
}
``````

Hmmm. Useful, but interesting that it only takes a 'long' as the first parameter.
Not hard to change though:-

``````float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

or both:-

``````float mapf(long x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

Better define them as double, makes it portable for uProc that support 64 bit floats too

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

robtillaart:
Better define them as double, makes it portable for uProc that support 64 bit floats too

``````double mapf(double x, double in_min, double in_max, double out_min, double out_max)
``````

{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

That makes good sense. And still allows passing a floating point variable as the first parameter.
Dunno why the original only used 'long' as the first parameter. Very limiting.

If you just want to read a sensor, then use:

``````// if the bottom of the scale is always zero

// otherwise
``````

I Tried the Double version like this;

double mapf(double ShoEncGlobal, double 0, double 92073, double 0, double halfPI)
{
return (ShoEncGlobal - 0) * (halfPI - 0) / (92073 - 0) + 0;
}

But it does not compile. I get

expected ',' or '...' before numeric constant

In a function deckaration you CAN'T use values, you have to use variable names. Only in the using of a function you can use numbers, the values of the passed variables, the ones you have declarated in declaration.
You can only use some default arguments (variables not necessary passed and that if they aren't they assume a specific value), but to do it you have to made a prototype of the function.
A prototype is like a declaration (only the first line) anded by a ; and located global. In prototypes you can add the default values, like this
function_type function_name (undefault parameter type u p n,... ...,... ..., default parameter type default parameter name=default parameter default value,dpt dpn=dpdv,...,...,...);
You can't have a undefoult parameter after a defoult one