// Pin number for the thermistor const int thermistorPin = 35; // Thermistor parameters const double thermistorNominal = 10000.0; const double temperatureNominal = 25.0; const double betaCoefficient = 3950.0; const double seriesResistor = 10000.0; // Duration settings const unsigned long timerDuration = 14400000; // 4 hours in milliseconds const unsigned long rollingAverageDuration = 600000; // 10 minutes in milliseconds // Variables for tracking time unsigned long startTime; unsigned long lastAverageTime; unsigned long lastPrintedTime; unsigned long currentTime; unsigned long elapsedTime; // Array to store temperature readings int temperatureData[240 * 60]; // Store 1 value per second for 4 hours int dataCount = 0; // Variables for temperature statistics double maxTemperature = -273.15; // Initialize to absolute zero double minTemperature = 1000.0; // Initialize to a high value double totalTemperature = 0.0; // Initialize the sum of temperatures void setup() { // Initialize serial communication Serial.begin(38400); // Record the start time startTime = millis(); // Initialize the last recorded average time and last printed time lastAverageTime = startTime; lastPrintedTime = startTime; } // Function to calculate the rolling average of temperature double calculateRollingAverage() { double allData = 0; int numSamples = dataCount; // Limit the number of samples for the rolling average if (numSamples > rollingAverageDuration / 1000) { numSamples = rollingAverageDuration / 1000; } // Calculate the sum of the selected samples for (int i = 0; i < numSamples; ++i) { allData += temperatureData[dataCount - 1 - i]; } // Calculate the average double average = allData / numSamples; return average; } void loop() { // Get the current time and elapsed time currentTime = millis(); elapsedTime = currentTime - startTime; // Check if the 4-hour duration has been reached if (elapsedTime >= timerDuration) { // 4-hour duration reached, stop the program // Print highest temperature Serial.print("Highest temperature: "); Serial.println(maxTemperature); // Print lowest temperature Serial.print("Lowest temperature: "); Serial.println(minTemperature); // Calculate and print overall average temperature double averageTemperature = totalTemperature / dataCount; Serial.print("Overall average temperature: "); Serial.println(averageTemperature); // Exit the loop and end the program return; } // Check if it's time to calculate the rolling average if (currentTime - lastAverageTime >= rollingAverageDuration) { // Time to calculate rolling average double rollingAvg = calculateRollingAverage(); // Print the rolling average Serial.print("10-minute Rolling average: "); Serial.println(rollingAvg); // Update the last recorded average time lastAverageTime = currentTime; } // Check if it's time to record and print a new temperature reading if (currentTime - lastPrintedTime >= 1000 && dataCount < (timerDuration / 1000)) { // Read and record temperature every second // Read the raw value from the thermistor int rawValue = analogRead(thermistorPin); // Calculate the resistance of the thermistor double resistance = seriesResistor * (4095.0 / rawValue - 1.0); // Calculate the temperature using the Steinhart-Hart equation double steinhart; steinhart = resistance / thermistorNominal; steinhart = log(steinhart); steinhart /= betaCoefficient; steinhart += 1.0 / (temperatureNominal + 273.15); steinhart = 1.0 / steinhart; steinhart = steinhart - 273.15; steinhart = steinhart * 1.80; // Convert the temperature to Celsius double temperature = steinhart + 30.0; // Update the maximum temperature if needed if (temperature > maxTemperature) { maxTemperature = temperature; } // Update the minimum temperature if needed if (temperature < minTemperature) { minTemperature = temperature; } // Add the temperature to the total sum totalTemperature += temperature; // Store the temperature reading in the array temperatureData[dataCount] = temperature; ++dataCount; // Print the current temperature Serial.println(temperature); // Update the last printed time lastPrintedTime = currentTime; } }