Bob Hruska
TVWBB Fan
The table lookup method is a very popular way of doing it and it saves a lot of CPU cycles if you don't have them to spare.
I used the equation just to try something new.
Here is another example that I've seen someone else using on the arduino with the ET-73 probes:
<pre class="ip-ubbcode-code-pre">
// Lookup table data for a Maverick Industries ET73 probe pulled up to 5 volts with a 22k resistor
const prog_uint16_t table[] PROGMEM = {968, 952, 936, 919, 898, 873, 845, 817, 786, 751, 713, 675, 638, 599, 560,
516, 477, 436, 403, 371, 338, 310, 283, 257, 233, 212, 190, 172, 156, 141,
128, 116, 105};
/**
* Gets the value of the specified table entry
* @param index Table index
* @return table value for the given index
*/
int tableEntry(int index) {
return pgm_read_word(table + index);
}
/**
* Gets the temperature for the given raw A/D value using the provided table
* @param rawValue the raw A/D converter value (0 - 1023)
@ @param tableData pointer to the lookup table data
* @return the corresponding temperature in tenths of degrees F (or -1 or 9999 if out of range)
*/
int convert(int rawValue) {
// Check for a value that is outside the range of the lookup table and
// return the special value (also makes sure the subsequent while loop
// doesn't go off into the weeds)
if (rawValue >= tableEntry(0)) {
return -1;
}
if (rawValue <= tableEntry(TABLE_SIZE - 1)) {
return 9999;
}
// Locate pair of table entries that the raw value lies between
int i = 0;
while (rawValue < tableEntry(i + 1)) {
i++;
}
// Interpolate between the values in the lookup table
// Fractional portion of the value between the two table entries (percent)
int fraction = (100 * (tableEntry(i) - rawValue)) / (tableEntry(i) - tableEntry(i+1));
// Compute and return the temperature value
return (MIN_TEMP * 10) + (SPAN * ((i * 100) + fraction) / 10);
}
</pre>
I used the equation just to try something new.
Here is another example that I've seen someone else using on the arduino with the ET-73 probes:
<pre class="ip-ubbcode-code-pre">
// Lookup table data for a Maverick Industries ET73 probe pulled up to 5 volts with a 22k resistor
const prog_uint16_t table[] PROGMEM = {968, 952, 936, 919, 898, 873, 845, 817, 786, 751, 713, 675, 638, 599, 560,
516, 477, 436, 403, 371, 338, 310, 283, 257, 233, 212, 190, 172, 156, 141,
128, 116, 105};
/**
* Gets the value of the specified table entry
* @param index Table index
* @return table value for the given index
*/
int tableEntry(int index) {
return pgm_read_word(table + index);
}
/**
* Gets the temperature for the given raw A/D value using the provided table
* @param rawValue the raw A/D converter value (0 - 1023)
@ @param tableData pointer to the lookup table data
* @return the corresponding temperature in tenths of degrees F (or -1 or 9999 if out of range)
*/
int convert(int rawValue) {
// Check for a value that is outside the range of the lookup table and
// return the special value (also makes sure the subsequent while loop
// doesn't go off into the weeds)
if (rawValue >= tableEntry(0)) {
return -1;
}
if (rawValue <= tableEntry(TABLE_SIZE - 1)) {
return 9999;
}
// Locate pair of table entries that the raw value lies between
int i = 0;
while (rawValue < tableEntry(i + 1)) {
i++;
}
// Interpolate between the values in the lookup table
// Fractional portion of the value between the two table entries (percent)
int fraction = (100 * (tableEntry(i) - rawValue)) / (tableEntry(i) - tableEntry(i+1));
// Compute and return the temperature value
return (MIN_TEMP * 10) + (SPAN * ((i * 100) + fraction) / 10);
}
</pre>