I need assistane understanding the multiMap() function found in the playground.
I also have 2 arrays, one is a series of known analogRead points and the second is a list of corresponding output results that I want with interpolation between.
I do not understand the structure and meaning of the multiMap() declaration statement in line 1. Can someone please break down what each of the 4 items are?
thanks
The post is at
http://arduino.cc/playground/Main/MultiMapThe code:
int multiMap(int val, int* _in, int* _out, uint8_t size)
{
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
if (val <= _in[0]) return _out[0];
if (val >= _in[size-1]) return _out[size-1];
// search right interval
uint8_t pos = 1; // _in[0] allready tested
while(val > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (val == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}
Usage
Some snippets shows how multiMap() can be used:
//My calibrated distance sensor - SHARP 2Y0A02 F 9Y
// out[] holds the values wanted in cm
int out[] = {150,140,130,120,110,100, 90, 80, 70, 60, 50, 40, 30, 20};
// in[] holds the measured analogRead() values for defined distances
int in[] = { 90, 97,105,113,124,134,147,164,185,218,255,317,408,506};
val = analogRead(A0);
cm = multiMap(val, in, out, 14);