#include <Wire.h>

// LM75A I2C address
#define LM75A_ADDRESS 0x48     // Defines the 12C address of the Sensor

unsigned long startTime;       // Start time of the 10-minute interval
float minTemp, maxTemp;        // Minimum and maximum temperature in the 10-minute interval
float sumTemp;                 // Sum of temperature readings in the 10-minute interval
int numReadings;               // Number of temperature readings in the 10-minute interval

void setup() {
  Serial.begin(9600);  // Set the baud rate to match the Serial Monitor
  Wire.begin();        
  
  startTime = millis();   // Initialize the start time
  minTemp = 0.0;          // Initialize the minimum temperature
  maxTemp = 0.0;          // Initialize the maximum temperature
  sumTemp = 0.0;          // Initialize the sum of temperatures
  numReadings = 0;        // Initialize the number of readings
}

void loop() {                            // This is the function that is looping
  unsigned long currentTime = millis();  // Current time
  
  if (currentTime - startTime >= 60000) {                      // Normally would be 60000 not 1000, but for demoonstration its 1000
    float averageTemp = sumTemp / numReadings;                // Calculates the average temperature
    
    printSummary(currentTime, minTemp, maxTemp, averageTemp); // Print the 10-minute summary
    
    startTime = currentTime;                                  // Reset the variables for the next 10-minute interval
    minTemp = 0.0;
    maxTemp = 0.0;
    sumTemp = 0.0;
    numReadings = 0;
  }
  

float temperature = readTemperature();   // Reads temperature
  
  if (numReadings == 0) {                  // Gets the min and max temperature
    minTemp = temperature;                 // Sets current temp as min and max
    maxTemp = temperature;
  } else {
    if (temperature < minTemp) {           // Reads if the temperature is smallest yet, if yes, sets as min
      minTemp = temperature;
    }
    if (temperature > maxTemp) {           // Reads if the temperature is largest yet, if yes, sets as max
      maxTemp = temperature;
    }
  }
  
  sumTemp += temperature;  // Add temperature to the sum
  numReadings++;
  
  delay(1000);  // Delay for 1 second before taking the next temperature reading
}

float readTemperature() {                   // Function to read temperature from LM75A sensor
  Wire.beginTransmission(LM75A_ADDRESS);
  Wire.write(0x00);                         // Temperature register address for LM75A
  Wire.endTransmission();

  Wire.requestFrom(LM75A_ADDRESS, 2);
  if (Wire.available()) {
    byte msb = Wire.read();                       //most significant byte read from the sensor
    byte lsb = Wire.read();                       //leasst significant byte read from the sensor
    int rawTemperature = ((msb << 8) | lsb) >> 5;
    float temperature = rawTemperature * 0.125;
    return temperature;
  }

  return 0.0;
}

void printSummary(unsigned long currentTime, float minTemp, float maxTemp, float averageTemp) { // Function to print the 10-minute summary
  int minutesPassed = (currentTime - startTime) / 60000;                                        // Calculate the minutes passed since the start of the interval
  
  Serial.print("Time Passed: ");
  Serial.print(minutesPassed);
  Serial.print(" minutes | ");
  Serial.print("Lowest Temperature: ");
  Serial.print(minTemp);
  Serial.print(" °C | ");
  Serial.print("Highest Temperature: ");
  Serial.print(maxTemp);
  Serial.print(" °C | ");
  Serial.print("Average Temperature: ");
  Serial.print(averageTemp);
  Serial.println(" °C");
}
