const int thermistorPin = 35; #define MEASUREMENT_INTERVAL 1000 #define MAX_READINGS_IN_10_MIN 600// 1 reading per second * 60 sec min * 10 min = 600 #define AVG_TEMP_IDX 0 #define MAX_TEMP_IDX 1 #define MIN_TEMP_IDX 2 #define OVERALL_AVG_IDX 3 float highestTemperature = -273.15f; // Highest temperature recorded float lowestTemperature = 1000.0; // Lowest temperature recorded unsigned long lastMeasurementTime = 0; float currentSetOfReadings[MAX_READINGS_IN_10_MIN]; // Stores 0 - 599 temp readings, 1 reading per second * 60 sec min * 10 min = 600 float resultsSummaryTable[216][4]; // There are 6 ten min interval in an hour, 6 times 24 = 144, add 72 (half day) to prevent overflow // which makes it 216. Each row is Average temp, max temp, min temp, overall average int currentReadingIdx = 0; int currentSummarySetIdx = 0; float readTemperature() { //This is the formual ito covert the data of temprture from the 10k resister into degrees int rawValue = analogRead(thermistorPin); float voltage = rawValue * (3.3 / 4095.0); float temperature = (voltage - 0.5) * 100.0; return temperature; } void setup() { Serial.begin(9600); } float calcuateRollingAverage() {//This is to find the rolling average of the data points and the averages coming in. float rollingAvg = 0.0; for (int i = 0; i <= currentSummarySetIdx; i++) { rollingAvg += resultsSummaryTable[i][0]; } return rollingAvg / (currentSummarySetIdx + 1); //this is to find the overall average taking the sum of all of the 10 min averages at the time then divding by the number of rows ther are in the array } void saveSummary() { // here we are setting the avereages and data of highest and lowest into our 2d array which in esence is creating rows of the data we are collecting float avgTempLast10Min = 0.0; for (int i = 0; i < MAX_READINGS_IN_10_MIN; i++) { avgTempLast10Min += currentSetOfReadings[i];// sum up all the reading last 10 min } avgTempLast10Min = avgTempLast10Min / MAX_READINGS_IN_10_MIN; // divide by the number of readings to get average resultsSummaryTable[currentSummarySetIdx][0] = avgTempLast10Min; resultsSummaryTable[currentSummarySetIdx][1] = highestTemperature; // record the set of readings resultsSummaryTable[currentSummarySetIdx][2] = lowestTemperature; if (currentSummarySetIdx == 0) { resultsSummaryTable[currentSummarySetIdx][3] = avgTempLast10Min; } else { resultsSummaryTable[currentSummarySetIdx][3] = calcuateRollingAverage(); } currentSummarySetIdx++; // move the index to record next set of results from 10 min recording if(currentSummarySetIdx == 216){// incase of overflow the index resets to 0 currentSummarySetIdx = 0; } } void resetAllTrackingVaribles() { currentReadingIdx = 0; highestTemperature = -273.15f; // Highest temperature recorded lowestTemperature = 1000.0; // Lowest temperature recorded } void loop() { unsigned long currentTime = millis(); if (currentTime - lastMeasurementTime >= MEASUREMENT_INTERVAL) { float temperature = readTemperature(); Serial.print("Reading: "); Serial.print(currentReadingIdx); Serial.print(" current temp: "); Serial.print(temperature); Serial.println(" °F"); currentSetOfReadings[currentReadingIdx] = temperature; // record the current reading if (temperature > highestTemperature) { // Update highest and lowest temperatures highestTemperature = temperature; } if (temperature < lowestTemperature) { lowestTemperature = temperature;// Update highest and lowest temperatures } currentReadingIdx++; // increment index to prepare for next reading if (currentReadingIdx == MAX_READINGS_IN_10_MIN) { saveSummary(); resetAllTrackingVaribles();// after we save a set of summary results, we need to re-initialize all our variables } lastMeasurementTime = currentTime; } // User command handling if (Serial.available() > 0) { char command = Serial.read(); if (command == 'S' || command == 's') { printSummary(); } else { } while (Serial.available() > 0) { Serial.read(); // Clear the buffer } } } void printSummary() { // the print display of all of our results Serial.println(); Serial.println("Summary"); Serial.println("10 min Average Temp\tHighest Temp\tLowestTemp\tOverall Average"); for (int i = 0; i < currentSummarySetIdx; i++) { Serial.print(resultsSummaryTable[i][0]); Serial.print(" °F \t"); Serial.print(resultsSummaryTable[i][1]); Serial.print(" °F \t"); Serial.print(resultsSummaryTable[i][2]); Serial.print(" °F \t"); Serial.print(resultsSummaryTable[i][3]); Serial.println(" °F "); } }