unsigned long startTime; float currentMin; float currentMax; float currentSum; int samples; float tenMins[600]; float tenMinAverages[300]; void setup() { Serial.begin(112500); delay(2000); startTime = millis(); Serial.println("-------------------- PROGRAM START --------------------"); currentMin = 1000; currentMax = -20; samples = 0; currentSum = 0; int i; for(i=0; i < 300; i++){ tenMinAverages[i] = 0; } for(i=0; i < 600; i++){ tenMins[i] = 0; } } void loop() { float reading; // voltage read by thermistor unsigned long interval; // time between readings in ms char input; // input taken to generate report interval = 1000; if(millis() >= startTime + interval){ // watiting 1000 ms startTime = millis(); // resetting the timer reading = analogRead(33); // read the pin Serial.println(returnCelsius(reading)); // print the temperature setMax(returnCelsius(reading)); // pass the temperature to various functions to find averages, min, max, last 10 min array and running sum setMin(returnCelsius(reading)); samples++; // increment number of samples taken lastTenMins(returnCelsius(reading)); sumNewTemp(returnCelsius(reading)); averagesList(); } if(Serial.available() > 0){ // checking for input needed to generate report input = Serial.read(); if(input == '0'){ report(); } } } float returnCelsius(float reading){ // converts voltage reading to degrees celsius | This function is adapted from code written by user "E" at https://arduinodiy.wordpress.com/2015/11/10/measuring-temperature-with-ntc-the-steinhart-hart-formula/ float resistance; float temperature; resistance = (4095/reading) - 1; resistance = 10000.0*resistance; temperature = resistance/10000.0; temperature = log(temperature); temperature /= 3950; temperature += 1.0 / 298.15; temperature = 1.0 / temperature; temperature -= 273.15; return temperature; } float setMax(float temp){ // keeps track of maximum temperature recorded if(temp > currentMax){ currentMax = temp; } return currentMax; } float setMin(float temp){ // keeps track of minimum temperature if(temp < currentMin){ currentMin = temp; } return currentMin; } void lastTenMins(float latestTemp){ // this function manages an array that contains the readings over the last 10 mins int i; int j; if(samples < 600){ // first fills the array tenMins[samples - 1] = latestTemp; } else { for(i=0; i<600; i++){ // then for every new value passed once the array is full, shift every value array to the left once j = i - 1; if (j == -1){ continue; } tenMins[j] = tenMins[i]; } tenMins[599] = latestTemp; // then set the last value to the newest value } } float averageTenMins(){ //average of the last 10 mins int i; float sum; sum = 0; for(i=0; i < 600; i++){ sum += tenMins[i]; } return sum/600; } float cumulativeAverage(){ // calculates total average based on the running sum return currentSum/samples; } void averagesList(){ // maintains an array of the average of every 10 minute period that has passed if(samples != 0 && samples%600 == 0){ tenMinAverages[(samples/600) - 1] = averageTenMins(); } } float sumNewTemp(float temperature){ // maintains the running sum of temperatures currentSum += temperature; return currentSum; } void report(){ // prints a report to console int i; Serial.print("Program has been logging for: "); Serial.print(millis()/60000); Serial.println(" minutes"); Serial.print("Max recorded Temperature: "); Serial.print(currentMax); Serial.println("oC"); Serial.print("Min recorded Temperature: "); Serial.print(currentMin); Serial.println("oC"); Serial.print("Overall average Temperature: "); Serial.print(cumulativeAverage()); Serial.println("oC"); Serial.print("Average Temperature over last 10 minutes: "); Serial.print(averageTenMins()); Serial.println("oC"); Serial.println("Here are the 10 minute averages since program began: (all in degrees celsius)"); for(i=0; i < 200; i++){ if(tenMinAverages[i] != 0.0){ Serial.print("~~~"); Serial.println(tenMinAverages[i]); } else { continue; } } }