I feel as though I'm missing something obvious here but I'm trying to use multiMap to interpolate data and it's only outputting 2 significant figures for the results.
When I use the regular map function it will output 4 significant figures which is what I'm after.
float calibration[] = { 0, 2, 5, 10, 14, 15 };
float sensorValue[] = { 0, 65, 213, 534, 759, 802 };
float distance;
float input;
void setup() {
Serial.begin(9600);
}
void loop() {
input = analogRead(A0);
//float distance = map(input,0,802,0,1500) / 100.; //Calibrate here. map(zero value during calibration, high number during calibration, 0, 1500)
float distance = FmultiMap(input, sensorValue, calibration, 6);
Serial.println(distance);
delay(10);
}
//****float multiMap
float FmultiMap(float input, float* sensorValue, float* calibration, uint8_t size)
{
// take care the value is within range
// input = constrain(input, sensorValue[0], sensorValue[size-1]);
if (input <= sensorValue[0]) return calibration[0];
if (input >= sensorValue[size-1]) return calibration[size-1];
// search right interval
uint8_t pos = 1; // sensorValue[0] already tested
while(input > sensorValue[pos]) pos++;
// this will handle all exact "points" in the _in array
if (input == sensorValue[pos]) return calibration[pos];
// interpolate in the right segment for the rest
return map(input, sensorValue[pos-1], sensorValue[pos], calibration[pos-1], calibration[pos]);
}