Reading PT100 probe

Hello everyone, i am new to arduino and electronics so please be gentle :smiley:
This is the code i am using to read a PT100 probe with a 220 ohm resistor.
I would like to make the code smaller and better can anyone help.
Thanks.

/*******************************/
/********* Julio MGM ***********/
/** Use it but don't abuse it **/
/*******************************/


const int ThermPin = 0 ;
const int Resistor = 217 ;
/* */
const float Alpha = 0.003850000000000;
const float Delta = 1.499900000000000;
const float Beta  = 0.108630000000000;
const float A = 0.003907746150000;
const float B = -0.000000577461500;
float C = 0 ;
/* */
const int temptotmax = 5;
int   temptotcnt = 0;
float temptotval = 0;
/* */
int calcin = millis();
/* */

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  temptotcnt = temptotcnt + 1 ;

  if (temptotcnt <= temptotmax) {
    float ohms = pt100 (ThermPin);
    temptotval = temptotval + readtemp(ohms);
    delay (50);
  }
  else {
      Serial.print("Temperature: ");
      Serial.print(temptotval/temptotmax);
      Serial.print("ºC  -  Time Used: ");
      Serial.println(millis() - calcin);
      
      delay (1000);
      temptotcnt = 0 ;
      temptotval = 0 ;
      calcin = millis();
  }

}

float pt100(int readpin) {
  float ohms = analogRead(readpin);
  float Vout = ohms * (5.0 / 1023.0);
  return Resistor * 1/(5.0/Vout - 1);
}

float readtemp(float ohms) {
  /* */
  float pttest1 = 0 ;
  float pttest2 = 0 ;
  float temptest1 = 0 ;
  float temptest2 = 0 ;
  /* */

  if (ohms < 100){C = (Alpha * Beta / pow(100, 4));}
  /* */
  if (ohms < 60) {temptest1 = -200;}
  else {
    if (ohms < 100) {temptest1 = -100;}
    else {
      if (ohms < 139) {temptest1 = 0;}
      else {
        if (ohms < 176) {temptest1 = 100;}
        else {
          if (ohms < 214) {temptest1 = 200;}
          else {
            if (ohms < 248) {temptest1 = 300;}
            else {
              if (ohms < 281) {temptest1 = 400;}
              else {
                if (ohms < 315) {temptest1 = 500;}
                else {
                  if (ohms < 346) {temptest1 = 600;}
                  else {
                    if (ohms < 376) {temptest1 = 700;}
                    else {temptest1 = 800;}
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  /* */
  while(pttest1 < ohms){
    temptest1 = temptest1 + 1 ;
    pttest1   = 100 * (1 + A * temptest1 + B * pow(temptest1, 2) - 100 * C * pow(temptest1, 3) + C * pow(temptest1, 4)) ;
    if (pttest1 < ohms) {pttest2 = pttest1;}
  }
  temptest2 = temptest1 - 1;
  return temptest2 + (ohms - pttest2) / (pttest1-pttest2) * (temptest1 - temptest2);
}

The code looks quite organized. I would replace the long if-else construct by a for loop with arrays:

#define NUMVALS 11
int16_t ohmcheck[NUMVALS] = { 60, 100, 139, 176, 214, 248, 281, 315, 346, 376, 32767 };
int16_t outvalues[NUMVALS] = { -200, -100, 0, 100, 200, 300, 400, 500, 600, 700, 800 };

uint8_t i;
for (i = 0; i < NUMVALS; i++) {
  if (ohms < ohmcheck[i]) {
    temptest1 = outvalues[i];
    break;
  }
}

Thanks.
Changed the ifs to for and calculated the mode instead of average.

/*******************************/
/********* Julio MGM ***********/
/** Use it but don't abuse it **/
/*******************************/
#include "QuickStats.h"
QuickStats stats;
/*  */
const int ThermPin = 0 ;
const int Resistor = 217 ;
/* */
const float Alpha = 0.003850000000000;
const float Delta = 1.499900000000000;
const float Beta  = 0.108630000000000;
const float A = 0.003907746150000;
const float B = -0.000000577461500;
float C = 0 ;
/* */
const int tempmax = 5;
int   tempcnt = 0;
float tempval[] = {0, 0, 0, 0, 0};
/* */

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {

  if (tempcnt < tempmax) {
    float ohms = pt100 (ThermPin);
    tempval[tempcnt] = readtemp(ohms);
    tempcnt = tempcnt + 1 ;
    delay (50);
  }
  else {
      float modetemp = stats.mode(tempval,tempmax,0.00001);
      /*  */
      Serial.print("TEMPERATURES : ");
      uint8_t i;
      for (i = 0; i < tempmax; i++) {
        Serial.print(tempval[i]);
        Serial.print(" - ");
        
      }

      if (modetemp == 0) {Serial.print("LAST");}
      else {Serial.print("MODE");}
      /*  */
      if (modetemp == 0) {modetemp = tempval[tempcnt-1];}
      /*  */
      Serial.print(" - ");
      Serial.println(modetemp);
      /*  */
      delay (1000);
      tempcnt = 0 ;
  }
}

float pt100(int readpin) {
  float ohms = analogRead(readpin);
  float Vout = ohms * (5.0 / 1023.0);
  return Resistor * 1/(5.0/Vout - 1);
}

float readtemp(float ohms) {
  /* */
  float pttest1 = 0 ;
  float pttest2 = 0 ;
  float temptest1 = 0 ;
  float temptest2 = 0 ;
  /* */
  if (ohms < 100){C = (Alpha * Beta / pow(100, 4));}
  /* */
  #define NUMVALS 11
  int16_t ohmcheck[NUMVALS] = { 60, 100, 139, 176, 214, 248, 281, 315, 346, 376, 32767 };
  int16_t outvalues[NUMVALS] = { -200, -100, 0, 100, 200, 300, 400, 500, 600, 700, 800 };
  uint8_t i;
  for (i = 0; i < NUMVALS; i++) {
    if (ohms < ohmcheck[i]) {
      temptest1 = outvalues[i];
      break;
    }
  }
  /* */
  while(pttest1 < ohms){
    temptest1 = temptest1 + 1 ;
    pttest1   = 100 * (1 + A * temptest1 + B * pow(temptest1, 2) - 100 * C * pow(temptest1, 3) + C * pow(temptest1, 4)) ;
    if (pttest1 < ohms) {pttest2 = pttest1;}
  }
  temptest2 = temptest1 - 1;
  return temptest2 + (ohms - pttest2) / (pttest1-pttest2) * (temptest1 - temptest2);
}