I'm writing my own thermistor program that obtains the resistence of the thermistor in a voltage divider using E = Ei (R1/R1+R2) and solving for R1.
I think the code for obtaining the resistence works fine.
Then, I found a comparable resistence/temperature table and I am storing the table into two arrays. One to hold the resistence and the other to hold the temperature.
Ok.
Here's where it messes up. I want to iterate through the length of the array take the difference between my thermistor resistence and the resistences of the table. I take the absolute value of the difference. The smallest difference means that it is closest to the true value of the thermistor. So, I take the index of the resistence closest t the thermistor resistence and plug that index into another array which contains the equivalent temperature that corresponds to the index.
PROBLEM IS: When I subtract the resistence of the thermistor with the resistence in the array, I get absolutely BRAIN DEAD STUPID numbers like Arduino failed kindergarden.
ex: 120000 - 320000 = +16000 or 120000 - 285000 = -32000.
WTF.
I tried using various data types: int, double, and float and I get the same weird result.
Here's my code using doubles:
I have taken a picture of the Serial Monitor Output.
Used code tags for c
I think the code for obtaining the resistence works fine.
Then, I found a comparable resistence/temperature table and I am storing the table into two arrays. One to hold the resistence and the other to hold the temperature.
Ok.
Here's where it messes up. I want to iterate through the length of the array take the difference between my thermistor resistence and the resistences of the table. I take the absolute value of the difference. The smallest difference means that it is closest to the true value of the thermistor. So, I take the index of the resistence closest t the thermistor resistence and plug that index into another array which contains the equivalent temperature that corresponds to the index.
PROBLEM IS: When I subtract the resistence of the thermistor with the resistence in the array, I get absolutely BRAIN DEAD STUPID numbers like Arduino failed kindergarden.
ex: 120000 - 320000 = +16000 or 120000 - 285000 = -32000.
WTF.
I tried using various data types: int, double, and float and I get the same weird result.
Here's my code using doubles:
I have taken a picture of the Serial Monitor Output.
C:
int thermPin = A0;
int tOutputTableF[] = {37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79,81,83,85,87,89,91,93,95,97,99,101,103,105,107,109,111,113};
int tOutputTableO[] = {302466,285206,269035,253877,239664,226331,213819,201971,190946,180588,170853,161700,153092,144992,137367,130189,123368,117000,110998,105338,100000,94963,90208,85719,81479,77438,73654,70076,66692,63491,60461,57594,54878,52306,49847,47538,45349,43273,41303};
int temperature = 0;
int finalIndex = 0;
void setup()
{
pinMode(thermPin, INPUT);
Serial.begin(9600);
}
void loop()
{
double x = analogRead(thermPin);
double y = ((5.0/1023.0)*x);
double z = ((y*100000.0)/(5.0*(1-((y/5.0))))); //Get the Resistence of the Thermistor
//Estimate temperature based on temperature-resistance table
int indexA;
int indexB;
int difference1;
int difference2;
boolean first;
double cat = 0;
for(int i = 0; i<40; i++)
{
//Calculate resistence difference
int indexAValue = tOutputTableO[I];
int indexBValue = tOutputTableO[i+1];
difference1 = (z - indexAValue); // Subtraction values are ALL WRONG by a huge magnitude.
difference2 = (z - indexBValue); //all wrong
double d1 = abs(difference1);
double d2 = abs(difference2);
Serial.println(i);
Serial.println(z);
Serial.println(difference1);
Serial.println(difference2);
if(d2<d1)
{
finalIndex = i+1;
//Serial.println(finalIndex);
//cat = d2;
}
}
temperature = tOutputTableF[finalIndex];
delay (1000);
}
Attachments
-
206.7 KB Views: 4
Last edited by a moderator: