//www.diyusthad.com
#include <LiquidCrystal.h>
const int rs = A0, en = A1, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define movAvgcnt 40 //amount of moving average cycles
#define AnTempInPin A2 // Voltage of temerature sensor In analogue
#define IntrptPin 13
int val = 0;
int Tarr[movAvgcnt];
int Tmax = -1260;
int Tmin = 1270;
bool IntrptBtnPressed = false;
long Tacc = 0;
unsigned int Tcount = 0;
void setup() {
int TempVIn = 0; //Voltage of tepmerature sensor
pinMode(AnTempInPin, INPUT);
//pinMode(IntrptPin, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(IntrptPin), button_ISR, LOW);
lcd.begin(16, 2);
for (byte i1 = 0; i1 < movAvgcnt; i1++) {
TempVIn = analogRead(AnTempInPin);
Tarr[i1] = TempVIn;
}
Tmin=GetTempPT1000Sensor(Average(Tarr, movAvgcnt));
Tmax=GetTempPT1000Sensor(Average(Tarr, movAvgcnt));
lcd.clear();
lcd.setCursor(0,0);
lcd.print("min:");
lcd.setCursor(8,0);
lcd.print("max:");
lcd.setCursor(0,1);
lcd.print("Act:");
lcd.setCursor(8,1);
lcd.print("Avg:");
}
void loop() {
int TempVIn = 0; //Voltage of tepmerature sensor
float Temp = 0;
float Tavg = 0;
MoveArrDataLeft(Tarr, movAvgcnt);
TempVIn = analogRead(AnTempInPin);
Tarr[movAvgcnt - 1] = TempVIn;
Temp = GetTempPT1000Sensor(Average(Tarr, movAvgcnt));
// Temp = Average(Tarr, movAvgcnt);
if (Tmax < Temp) {
Tmax = Temp;
}
if (Tmin > Temp) {
Tmin = Temp;
}
Tcount++; //Tcount=Tcount+1
if (Tcount==65530) {
Tacc=0;
Tcount=1;
}
Tacc += Temp; //Tacc=Tacc+Tarr[movAvgcnt-1]
Tavg = Tacc / Tcount;
// lcd.clear();
lcd.setCursor(4,0);
// lcd.print(" ");
lcd.setCursor(4,0);
lcd.print(dtoTempStr(Tmin));
lcd.setCursor(12,0);
// lcd.print(" ");
lcd.setCursor(12,0);
lcd.print(dtoTempStr(Tmax));
lcd.setCursor(4,1);
// lcd.print("Act:");
lcd.print(dtoTempStr(Temp));
lcd.setCursor(12,1);
// lcd.print("Avg:");
lcd.print(dtoTempStr(Tavg));
delay(10);
}
void MoveArrDataLeft(int T[], byte Ubnd) {
// the array is passed as a pointer by the menas of 'varName[]' (any value inside the array barckets are ignored ie. 'varName[10]' = 'varName[]')
// any changes in the array remain after the function has been finsihed
for (byte i = 0; i < (Ubnd - 1); i++) {
T[i] = T[i + 1];
}
}
float Average(int T[], byte UBnd) {
float Tvar = 0;
for (byte i = 0; i < UBnd; i++) {
Tvar += T[i];
};
Tvar = Tvar / (float)UBnd;
return Tvar;
}
float GetTempPT1000Sensor(int R) {
return 0.506*R-50.237;
}
String dtoTempStr(float Temp){
char s[4];
if (Temp<100) {
dtostrf(round(Temp*10)/10,2,1,s);
}
if (Temp<10) {
dtostrf(round(Temp*10)/10,1,1,s);
s[3]=" ";
s[4]=0;
}
if (Temp>=100) {
dtostrf(round(Temp),3,0,s);
s[3]=" ";
s[4]=0;
}
return s;
}