// Global Variables const int thermistorPin = 34; const int numSamples = 600000; // How many samples in 10 minutes const unsigned long printInterval = 1000; const unsigned long summaryInterval = 10 * 60 * 1000; // 10 minutes const int numSummaries = 144; // Total 10 minute in 24 hours const unsigned long oneDay = 24 * 60 * 60 * 1000; // 24 Hours // Variables used to calculate temp int Vo; float R1 = 20000; float R2; float logR2, T; float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07; float temperature; int totalValue = 0; float totalAverage = 0; // To keep time unsigned long startTime = 0; unsigned long lastPrintTime = 0; unsigned long lastSummaryTime = 0; int summaryIndex = 0; struct summary { // Struct to store values easily float minValue = 0; float maxValue = 0; float average = 0; }; struct summary readings[144]; // 144 array for all 10 minutes in 24 hours void getSamples() { // Function to get the values of the last 10 minutes float sumValue = 0; float minValue = 1000; // Initialize very high so the first data is lower float maxValue = -1000; // Initialize very low so the first data is higher int sampleCount = 0; for (int i = 0; i < numSamples; i++) { // Goes through every sample in the last 10 minutes // Calculates temperature Vo = analogRead(thermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); R2 = R2 * -1.0; logR2 = log(R2); temperature = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); temperature = temperature - 273.15; temperature = temperature - 10.0; if (temperature < minValue) { minValue = temperature; // Update minValue if a lower value is found } if (temperature > maxValue) { maxValue = temperature; // Update maxValue if a higher value is found } totalValue = totalValue + temperature; // To find average of all 24 hours sumValue = sumValue + temperature; // To find average of 10 minutes sampleCount++; } readings[summaryIndex].minValue = minValue; // Assigns minValue into the struct readings[summaryIndex].maxValue = maxValue; // Assigns maxValue into the struct readings[summaryIndex].average = sumValue / numSamples; // Assigns average into the struct summaryIndex++; } void printLastSummary() { // Prints previous 10 minute summary struct summary lastSummary; lastSummary = readings[summaryIndex - 1]; // Minus 1 to go to previous summary Serial.println("Last 10-Minute Summary:"); Serial.print("Min Value in Celsius: "); Serial.println(lastSummary.minValue); Serial.print("Min Value in Fahrenheit: "); Serial.println((lastSummary.minValue * 9 / 5) + 32); Serial.print("Max Value in Celsius: "); Serial.println(lastSummary.maxValue); Serial.print("Max Value in Fahrenheit: "); Serial.println((lastSummary.maxValue * 9 / 5) + 32); Serial.print("Average in Celsius: "); Serial.println(lastSummary.average); Serial.print("Average in Fahrenheit: "); Serial.println((lastSummary.average * 9 / 5) + 32); Serial.println(); } void printAllSummaries() { // Goes through the 144 array and prints all 144 summaries. for (int i = 0; i < numSummaries; i++) { Serial.print("Summary "); Serial.print(i + 1); Serial.println(":"); Serial.print("Min Value in Celsius: "); Serial.println(readings[i].minValue); Serial.print("Min value in Fahrenheit: "); Serial.println((readings[i].minValue * 9 / 5) + 32); Serial.print("Max Value in Celsius: "); Serial.println(readings[i].maxValue); Serial.print("Max value in Fahrenheit: "); Serial.println((readings[i].maxValue * 9 / 5) + 32); Serial.print("Average in Celsius: "); Serial.println(readings[i].average); Serial.print("Average in Fahrenheit: "); Serial.println((readings[i].average * 9 / 5) + 32); } } void printSummaries() { // Prints all previous summaries for (int i = 0; i < numSummaries; i++) { if (readings[i].average != 0 && readings[i].minValue != 0 && readings[i].maxValue != 0){ // Only prints if data isn't 0 so it doesn't print empty indexes Serial.print("Summary "); Serial.print(i + 1); Serial.println(":"); Serial.print("Min Value in Celsius: "); Serial.println(readings[i].minValue); Serial.print("Min value in Fahrenheit: "); Serial.println((readings[i].minValue * 9 / 5) + 32); Serial.print("Max Value in Celsius: "); Serial.println(readings[i].maxValue); Serial.print("Max value in Fahrenheit: "); Serial.println((readings[i].maxValue * 9 / 5) + 32); Serial.print("Average in Celsius: "); Serial.println(readings[i].average); Serial.print("Average in Fahrenheit: "); Serial.println((readings[i].average * 9 / 5) + 32); } } } void setup() { // Setup my code pinMode(thermistorPin, INPUT); // Sets my pin 34 so it inputs data Serial.begin(9600); // 9600 baud startTime = millis(); // Uses millis to set start time lastPrintTime = startTime; lastSummaryTime = startTime; } void loop() { unsigned long currentTime = millis(); // Initializes Current time to millis unsigned long elapsedTime = currentTime - startTime; // Calculates Elapsed time // Calculates temperature Vo = analogRead(thermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); R2 = R2 * -1.0; logR2 = log(R2); T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); T = T - 273.15; T = T - 10.0; // Prints temp every second Serial.print("Celsius: "); Serial.println(T); delay(1000); // Read user input char userInput = Serial.read(); // Check if the user wants summaries if (userInput == 'Y' || userInput == 'y') { // Call the function to get samples and print summaries printSummaries(); } if (currentTime - lastSummaryTime >= summaryInterval) { // Checks to see if 10 minutes has passed getSamples(); printLastSummary(); lastSummaryTime = currentTime; // Updates last SummaryTime to when it printed } if (elapsedTime >= oneDay) { // Checks to see if 24 hours has passed printAllSummaries(); totalAverage = totalValue / 86400; Serial.print("The total average for all 24 hours in Celsius is: "); Serial.println(totalAverage); Serial.print("The total average for all 24 hours in Fahrenheit is: "); Serial.println((totalAverage * 9 / 5) + 32); startTime = currentTime; // Resets for the next 24 hours summaryIndex = 0; // Resets for the next 24 hours } }