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);
}