// Constants const int thermistorPin = 35; // Analog pin connected to the thermistor #define ROLLING_AVERAGE_SIZE 60 #define INVALID_TEMPERATURE -999.0 #define INTERVAL_10_MINUTES 600000 #define INTERVAL_24_HOURS 86400000 // Variables unsigned long lastMeasurementTime = 0; // Time of the last temperature measurement unsigned long lastSummaryTime = 0; // Time of the last summary output unsigned long lastStreamTime = 0; // Time of the last stream output unsigned long measurementInterval = 1000; // Interval between temperature measurements (in milliseconds) unsigned long summaryInterval = 600000; // Interval between summary outputs (in milliseconds) unsigned long streamInterval = 1000; // Interval between streaming outputs (in milliseconds) unsigned long lastAverageUpdateTime = 0; // Time of the last update to averages float rollingAverage = 0.0; // Rolling average of temperature readings float highestTemperature = -273.15; // Highest temperature recorded float lowestTemperature = 1000.0; // Lowest temperature recorded float overallAverage = INVALID_TEMPERATURE; // Overall average temperature since the start of time float temperatureReadings[ROLLING_AVERAGE_SIZE]; // Array to store temperature readings for rolling average int currentIndex = 0; // Index to keep track of the current position in the array int numValidReadings = 0; // Number of valid temperature readings float tenMinuteAverages[144]; // Array to store 10-minute averages for the past 24 hours // Function to read temperature from the thermistor with a 10K resistor and 10K thermistor float readTemperature() { int rawValue = analogRead(thermistorPin); float voltage = rawValue * (3.3 / 4095.0); // Convert raw value to voltage (assuming 3.3V reference voltage) // Assuming a linear relationship between voltage and temperature float temperature = (voltage - 0.5) * 100.0; // Assuming 10 mV per degree Celsius return temperature; } void setup() { Serial.begin(9600); } void loop() { unsigned long currentTime = millis(); // Temperature measurement if (currentTime - lastMeasurementTime >= measurementInterval) { float temperature = readTemperature(); if (!isnan(temperature)) { float temperatureFaranheit = temperature; // Update highest and lowest temperatures if (temperatureFaranheit > highestTemperature) { highestTemperature = temperatureFaranheit; } if (temperatureFaranheit < lowestTemperature) { lowestTemperature = temperatureFaranheit; } // Update rolling average rollingAverage = calculateRollingAverage(temperatureFaranheit); lastMeasurementTime = currentTime; } } // Summary printing if (currentTime - lastSummaryTime >= summaryInterval) { printSummary(); lastSummaryTime = currentTime; } // Streaming if (currentTime - lastStreamTime >= streamInterval) { streamTemperature(); lastStreamTime = currentTime; } // User command handling if (Serial.available() > 0) { char command = Serial.read(); if (command == 'S' || command == 's') { printSummary(); printTenMinuteAverages(); } else { Serial.println("Invalid command. Please enter 'S' or 's' for summary."); } while (Serial.available() > 0) { Serial.read(); // Clear the buffer } } // Update 10-minute averages if (currentTime - lastAverageUpdateTime >= INTERVAL_10_MINUTES) { updateTenMinuteAverages(); lastAverageUpdateTime = currentTime; } } // Function to calculate the rolling average of temperature readings float calculateRollingAverage(float newTemperature) { temperatureReadings[currentIndex] = newTemperature; currentIndex = (currentIndex + 1) % ROLLING_AVERAGE_SIZE; if (numValidReadings < ROLLING_AVERAGE_SIZE) { numValidReadings++; } float sum = 0.0; for (int i = 0; i < numValidReadings; i++) { sum += temperatureReadings[i]; } return sum / numValidReadings; } // Function to print the summary of temperature statistics void printSummary() { float averageTemperature = calculateAverageTemperature(); overallAverage = calculateOverallAverage(); Serial.println("----- Summary -----"); Serial.print("Highest Temperature: "); Serial.print(highestTemperature); Serial.println(" °F"); Serial.print("Lowest Temperature: "); Serial.print(lowestTemperature); Serial.println(" °F"); Serial.print("Overall Average: "); Serial.print(overallAverage); Serial.println(" °F"); Serial.println("-------------------"); Serial.println(); } // Function to print the 10-minute averages of temperature readings void printTenMinuteAverages() { Serial.println("----- 10-Minute Averages -----"); int numAverages = sizeof(tenMinuteAverages) / sizeof(tenMinuteAverages[0]); for (int i = 0; i < numAverages; i++) { Serial.print("Interval "); Serial.print(i); Serial.print(": "); if (tenMinuteAverages[i] != INVALID_TEMPERATURE) { Serial.print(tenMinuteAverages[i]); Serial.println(" °F"); } else { Serial.println("No valid readings within this interval."); } } Serial.println("-----------------------------"); Serial.println(); } // Function to calculate the average temperature float calculateAverageTemperature() { float sum = 0.0; int count = 0; for (int i = 0; i < numValidReadings; i++) { if (temperatureReadings[i] != INVALID_TEMPERATURE) { sum += temperatureReadings[i]; count++; } } if (count > 0) { return sum / count; } else { return INVALID_TEMPERATURE; } } // Function to calculate the overall average temperature float calculateOverallAverage() { float sum = 0.0; int count = 0; for (int i = 0; i < numValidReadings; i++) { if (temperatureReadings[i] != INVALID_TEMPERATURE) { sum += temperatureReadings[i]; count++; } } if (count > 0) { return sum / count; } else { return INVALID_TEMPERATURE; } } // Function to stream the current temperature void streamTemperature() { float currentTemperature = readTemperature(); Serial.print("Temperature: "); Serial.print(currentTemperature); Serial.println(" °F"); } // Function to update the 10-minute averages void updateTenMinuteAverages() { int numAverages = sizeof(tenMinuteAverages) / sizeof(tenMinuteAverages[0]); // Shift the averages array to the right for (int i = numAverages - 1; i > 0; i--) { tenMinuteAverages[i] = tenMinuteAverages[i - 1]; } // Calculate the new average and store it in the first interval float averageTemperature = calculateAverageTemperature(); tenMinuteAverages[0] = averageTemperature; }