#include #include #define MILLIS_IN_TEN_MINUTES 600000 // 10 minutes * 60 seconds * 1000 ms // Global variables unsigned long lastReadingMillis = 0; unsigned long lastAverageMillis = 0; //These variables track min, max, and avg temperature values, keep count of the number of readings as well as calculate the total temperature sum and overall avg temperature. float minTemp = 1000; float maxTemp = -1000; float totalTemp = 0; int totalReadings = 0; float overallAverage = 0; // LM75 (temperature sensor being used) M2M_LM75A lm75; // Initializes the I2c Temperature sensor void initI2C() { Wire.begin(21, 22); //SDA=21, SCL=22, what the wires correspond to } // This initializes the serial communication with a baud rate of 9600. void initSerial() { Serial.begin(9600); } // Function to read temperature from sensor float readTemperature() { return lm75.getTemperature(); } // This function updates statistics for temperature readings by accumulating the total temperature, incrementing the total number of readings. void updateStatistics(float temp) { totalTemp += temp; totalReadings++; //this calculates the overall average temperature overallAverage = totalTemp / totalReadings; //updating the minimum and maximum temperature values if necessary. if (temp < minTemp) minTemp = temp; if (temp > maxTemp) maxTemp = temp; } // This function prints the current temperature value in Celsius units over the serial communication. void printCurrentTemp(float temp) { Serial.print("Current Temperature: "); Serial.print(temp); Serial.println(" °C"); } // Print average, min, max temperatures every 10 minutes void printTemperatureStats() { float averageTemp = totalTemp / totalReadings; totalTemp = 0; totalReadings = 0; //prints the average temperature over the last 10 minutes in Celsius units over the serial communication. Serial.print("Average Temperature over last 10 minutes: "); Serial.print(averageTemp); Serial.println(" °C"); // prints the lowest recorded temperature in Celsius units over the serial communication. Serial.print("Lowest temperature recorded: "); Serial.print(minTemp); Serial.println(" °C"); //prints the highest recorded temperature in Celsius units over the serial communication. Serial.print("Highest temperature recorded: "); Serial.print(maxTemp); Serial.println(" °C"); //prints the overall average temperature in Celsius units over the serial communication. Serial.print("Overall average temperature: "); Serial.print(overallAverage); Serial.println(" °C"); } // this is the setup function where it initializes the I2C communication and serial communication. void setup() { initI2C(); initSerial(); } // This is the loop function that runs repeatedly. It declares an unsigned long variable "currentMillis" and assigns it the value of the current millisecond count using the millis() function. void loop() { unsigned long currentMillis = millis(); if (currentMillis - lastReadingMillis >= 5000) { lastReadingMillis = currentMillis; float temp = readTemperature(); updateStatistics(temp); // printCurrentTemp(temp); // Commented out this line to stop printing every 5 seconds } // Calculate and print average, min, max every 10 minutes if (currentMillis - lastAverageMillis >= MILLIS_IN_TEN_MINUTES) { lastAverageMillis = currentMillis; printTemperatureStats(); } }