# multiMap not accurate?

Hello everyone. I am newer to programming but have learned ALOT the last few weeks just from reading and trial and error. I have almost my whole project done but then got hang up when I decided to add one extra feature; a transmission temperature sensor.

My circuit works fine, I am using a voltage divider with a automotive temp sending unit.
My analogRead values are trending properly when I Serial.print them ie: 212 degrees (out array) = about 556 (in array) and 68 degrees = about 945.
The problem seems to happen when the conversion to temp takes place “temp = multiMap(val, in, out, 19);”
At 68 degrees (room temp) the LCD or Serial shows 68 degrees, at 212 degrees (boiling) the LCD or Serial shows 333 degrees while showing the proper value of 556 for analogRead.

Any idea what is going on? My complete code is attached. The multMap function is all the way at the bottom.
Thank you for any help.

Working.ino (11.9 KB)

Just warmed up a pot of water while watching "val" and "temp" the jump in temp seems to be happening the worse between 808 and 733. At a val of 733, temp reads 211. At a val of 808, temp reads 155. At a val of 841, temp reads 132.

Please post your code in code tags. People don't necessarily want to download your code into their IDE to read it. I won't. What happens if you use the regular map function? Do you need multiMap there?

I understand. My complete code is too long and goes of the allowed character amount. I will try to go through and shorten it up to just the pertenent parts.

Its a non linear input. I have 19 data points for temp/ohm that I have converted to the analogRead value/temp. I need to fill in the spaces between. This requires multiMap correct? I am attempting to use reMap right now but I dont know if I will get a different result.

I understand now. Check your in and out arrays around the array index where the jump occurs. How did you come by the values in the two arrays?

Here is the shortened code. Should make it relatively easy to find the issue.

``````//Temp Sensor Config
// out[] holds the values wanted in temp
int out[] = { 0, 32, 68,100,120,140,150,170,190,200,210,212,220,230,250,280,300,320,340 };
// in[] holds the measured analogRead() values for defined distances
int in[]  = { 1024,980,945,940,897,841,808,733,651,612,563,556,524,482,403,302,248,204,170 };
int val;
int temp;

// --------------------------------------------------------------------------------------------------------------------------^

void setup() {
Serial.begin(9600);
}

// --------------------------------------------------------------------------------------------------------------------------^

void loop() {

temp = multiMap(val, in, out, 19);

Serial.println(val);

Serial.println(temp);
delay(1000);

}

// --------------------------------------------------------------------------------------------------------------------------^

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]);
}
``````

I used this voltage divider calculator here. http://javey.net/voltage-divider.html and the ohm values for the sensor here. http://www.chevytalk.org/fusionbb/showtopic.php?tid/261872/ 5v for input. 100ohm resistor for R1 and the temp sensor value for R2. That gave my my output voltage. I took output voltage x 1024 / 5. That gave me my analogRead value. Upon testing the sensor in a pot of water from room temp to boiling, I found the values in the array were almost spot on but the multiMap function isnt returning the proper temp values.

From the library on playground.

Furthermore the input array must be a monotone increasing array of values.

I noticed that in the examples on that page that the In array is always an increasing set of values, but the out array could be backwards (decreasing).

Good eye! Let me see if that works.

You sir are a genius! Such an easy fix. Thank you very much. Works Perfect.