Newbie help - Floating points or math prob?

This is my first post, but I did try to research this as best I could before posting. I just can't figure out what I'm doing wrong here.

The purpose of this app is to do a table lookup from a resistance table and tell me what the temperature is based on that resistance. I can't even get to the lookup part yet because I can't get this thing to do the simple math required to get my resistance values. When I try to do a floating point operation something goes awry. I'd appreciate any help I can get.

Thanks from a first-timer.

Here is the output:

Pit= 0 aval=954 vaval= ? raval= ?
Pit=10752 aval=954 vaval= ? raval= ?
Pit=10752 aval=954 vaval= ? raval= ?
Pit=10752 aval=954 vaval= ? raval= ?
Pit=10752 aval=954 vaval= ? raval= ?
Pit=10752 aval=954 vaval= ? raval= ?
Pit=10752 aval=954 vaval= ? raval= ?

Here is the program:

#define NUMTEMPS 342

unsigned int pit_temp = 0;
unsigned int setpoint = 100;

short temptable[NUMTEMPS][2] = {  // Cooper Atkins Resistance Table
{-40,336450},
{-39,324270},
{-38,312580},
{-37,301350},
{-36,290560},
{-35,280200},
{-34,270250},
{-33,260680},
{-32,251490},
{-31,242660},
{-30,234170},
{-29,226010},
{-28,218160},
{-27,210610},
{-26,203350},
{-25,196370},
{-24,189650},
{-23,183180},
{-22,176960},
{-21,170980},
{-20,165210},
{-19,159660},
{-18,154320},
{-17,149170},
{-16,144210},
{-15,139440},
{-14,134840},
{-13,130410},
{-12,126140},
{-11,122030},
{-10,118060},
{-9,114240},
{-8,110550},
{-7,107000},
{-6,103570},
{-5,100260},
{-4,97072},
{-3,93996},
{-2,91027},
{-1,88163},
{0,85399},
{1,82731},
{2,80156},
{3,77670},
{4,75269},
{5,72951},
{6,70712},
{7,68550},
{8,66461},
{9,64443},
{10,62403},
{11,60609},
{12,58788},
{13,57028},
{14,55326},
{15,53681},
{16,52091},
{17,50553},
{18,49065},
{19,47627},
{20,46235},
{21,44889},
{22,43586},
{23,42326},
{24,41106},
{25,39926},
{26,38783},
{27,37677},
{28,36606},
{29,35569},
{30,34565},
{31,33592},
{32,32650},
{33,31738},
{34,30854},
{35,29999},
{36,29169},
{37,28366},
{38,27587},
{39,26832},
{40,26100},
{41,25391},
{42,24703},
{43,24037},
{44,23390},
{45,22763},
{46,22155},
{47,21565},
{48,20993},
{49,20438},
{50,19899},
{51,19376},
{52,18869},
{53,18377},
{54,17899},
{55,17435},
{56,16985},
{57,16548},
{58,16123},
{59,15711},
{60,15311},
{61,14922},
{62,14544},
{63,14177},
{64,13820},
{65,13474},
{66,13137},
{67,12810},
{68,12492},
{69,12183},
{70,11883},
{71,11591},
{72,11307},
{73,11031},
{74,10762},
{75,10501},
{76,10247},
{77,10000},
{78,9759.70},
{79,9526},
{80,9298.60},
{81,9077.30},
{82,8862},
{83,8652.40},
{84,8448.50},
{85,8250},
{86,8056.80},
{87,7868.70},
{88,7685.60},
{89,7507.30},
{90,7333.70},
{91,7164.60},
{92,7000},
{93,6839.70},
{94,6683.50},
{95,6531.40},
{96,6383.20},
{97,6238.80},
{98,6098.20},
{99,5961.10},
{100,5827.60},
{101,5697.40},
{102,5570.60},
{103,5446.90},
{104,5326.40},
{105,5208.90},
{106,5094.30},
{107,4982.70},
{108,4873.80},
{109,4767.60},
{110,4664},
{111,4563},
{112,4464.50},
{113,4368.40},
{114,4274.70},
{115,4183.20},
{116,4094},
{117,4007},
{118,3922.10},
{119,3839.20},
{120,3758.30},
{121,3679.40},
{122,3602.30},
{123,3527.10},
{124,3453.70},
{125,3382},
{126,3312},
{127,3243.70},
{128,3177},
{129,3111.80},
{130,3048.20},
{131,2986.10},
{132,2925.40},
{133,2866.20},
{134,2808.30},
{135,2751.70},
{136,2696.40},
{137,2642.50},
{138,2589.70},
{139,2538.20},
{140,2487.80},
{141,2438.60},
{142,2390.50},
{143,2343.50},
{144,2297.50},
{145,2252.60},
{146,2208.70},
{147,2165.70},
{148,2123.80},
{149,2082.70},
{150,2042.60},
{151,2003.30},
{152,1964.90},
{153,1927.40},
{154,1890.70},
{155,1854.70},
{156,1819.60},
{157,1785.20},
{158,1751.60},
{159,1718.60},
{160,1686.40},
{161,1654.80},
{162,1623.90},
{163,1593.70},
{164,1561.20},
{165,1535.30},
{166,1507},
{167,1479.30},
{168,1452.20},
{169,1425.70},
{170,1399.80},
{171,1374.40},
{172,1349.50},
{173,1325.20},
{174,1301.40},
{175,1278},
{176,1255.20},
{177,1232.80},
{178,1210.90},
{179,1189.50},
{180,1168.50},
{181,1147.90},
{182,1127.70},
{183,1108},
{184,1088.70},
{185,1069.70},
{186,1051.10},
{187,1033},
{188,1015.10},
{189,997.66},
{190,980.54},
{191,963.77},
{192,947.33},
{193,931.22},
{194,915.43},
{195,899.95},
{196,884.78},
{197,869.91},
{198,885.33},
{199,841.04},
{200,827.03},
{201,813.3},
{202,799.83},
{203,786.63},
{204,773.68},
{205,760.99},
{206,748.54},
{207,736.33},
{208,724.36},
{209,712.61},
{210,701.09},
{211,689.79},
{212,678.71},
{213,667.8},
{214,657.8},
{215,646.59},
{216,636.28},
{217,626.17},
{218,616.25},
{219,606.51},
{220,596.96},
{221,587.58},
{222,578.38},
{223,569.34},
{224,560.48},
{225,551.77},
{226,543.23},
{227,534.84},
{228,526.64},
{229,518.53},
{230,510.59},
{231,502.79},
{232,495.14},
{233,487.62},
{234,480.24},
{235,472.98},
{236,465.86},
{237,458.86},
{238,451.99},
{239,445.24},
{240,438.61},
{241,432.09},
{242,425.69},
{243,419.4},
{244,413.22},
{245,407.15},
{246,401.19},
{247,395.32},
{248,389.56},
{249,383.9},
{250,378.33},
{251,372.85},
{252,367.48},
{253,362.19},
{254,356.99},
{255,351.88},
{256,346.86},
{257,341.92},
{258,337.06},
{259,332.29},
{260,327.6},
{261,322.98},
{262,318.44},
{263,313.98},
{264,309.59},
{265,305.27},
{266,301.03},
{267,296.86},
{268,292.75},
{269,288.72},
{270,284.74},
{271,280.83},
{272,276.99},
{273,273.21},
{274,269.48},
{275,265.82},
{276,262.21},
{277,258.67},
{278,255.18},
{279,251.74},
{280,248.36},
{281,245.03},
{282,241.76},
{283,238.53},
{284,235.36},
{285,232.24},
{286,229.17},
{287,226.15},
{288,223.18},
{289,220.25},
{290,217.37},
{291,214.53},
{292,211.74},
{293,208.99},
{294,206.28},
{295,203.62},
{296,201},
{297,198.41},
{298,195.87},
{299,193.37},
{300,190.9},
{301,188.47}
};


void setup() {

      Serial.begin(9600);
      pinMode(9, OUTPUT);
}

void loop() {
      pit_temp = thermister_temp(analogRead(5));
        delay (1000);
}

int thermister_temp(int aval) {

   int Z;
   char line[40];
   
   float voltage_aval = aval / 1024 * 5.0;
   float resistance_aval = (1000 * voltage_aval) / (5.0 - voltage_aval);

   sprintf(line, "Pit=%3d aval=%3d vaval=%3f raval=%3f", pit_temp, aval, voltage_aval, resistance_aval);
   Serial.println(line);

   for (int i=NUMTEMPS; i>0; i--)
   {
      if (temptable[i][1] >= resistance_aval)
      {
         Z  = temptable[i+1][0];
         break;
      }
   }
   return (Z);
}

Please edit your post and put code tags around the source code. The tags can be inserted by clicking the # button above the edit window.

Your array is declared to contain shorts, yet you are initializing it with values like 336450 and 7164.60. Perhaps some research into the type and range of values that a short can hold is in order.

Some research into the format string for the printf command is in order, too. What does %3f mean?

As a newbie I guess I had the RTFM coming.. Those mistakes were actually from the tons of times I tried modifying this thing to try and get sane results, though I obviously still have mistakes or this thing would work.

Here is the same thing in a more condensed format:

void setup() {

      Serial.begin(9600);
      pinMode(9, OUTPUT);
}

void loop() {

   char line[80];
   
   int aval = (analogRead(5));
   
   float voltage_aval = aval / 1024 * 5.0;
   float resistance_aval = (1000 * voltage_aval) / (5.0 - voltage_aval);

   sprintf(line, "aval=%3d vaval=%f raval=%f", aval, voltage_aval, resistance_aval);
   Serial.println(line);

   delay (1000);
}

Output:

aval=955 vaval=? raval=?
aval=954 vaval=? raval=?
aval=954 vaval=? raval=?
aval=954 vaval=? raval=?
aval=954 vaval=? raval=?
aval=954 vaval=? raval=?
aval=954 vaval=? raval=?
aval=954 vaval=? raval=?

Small changes to get the math working...

   float voltage_aval = aval / 1024[glow].0[/glow] * 5.0;
   float resistance_aval = (1000[glow].0[/glow] * voltage_aval) / (5.0 - voltage_aval);

sprintf does NOT support floating-point numbers. You'll have to use something else to output the results.

Thanks Badly! The sprintf was only being used for debugging so I'll worry about that later. Bottom line, adding the decimal fixed my problem and I'm running now.

If you guys only knew how many hours I spent on this! The simple things...

Alan