Hello everyone, i am new to arduino and electronics so please be gentle ![]()
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);
}