Jul 26, 2019

mapFloat(): Arduino's map() function extended to allow float variables

A function to extend range mapping to float/double variables.


// map one numerical span to another with floating point values
double mapFloat (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;
}

EX:  map an ADC range to normalized decimal:

    x_norm = mapFloat(adc_val, 0, 4095,  0.00, 1.00);



No comments: