Assuming a reasonably narrow temperature range, you could consider using an array to store the last time a certain temperature has been measured using the temperature as the index (possibly with some offset correction). The minimum is the index of the first value in the array set less than 24 hours ago. By iterating over the array backwards the maximum can be found.
[edit]
Here is a possible implementation.
long readTemperature() {
return random(-20, 30);
}
unsigned long pMillis() {
unsigned long const now {millis()};
return now ? now : 1; // Timestamp 0 is reserved.
}
template <size_t n> size_t findMinimum(unsigned long const (& measurements)[n], unsigned long const timespan) {
unsigned long const now {pMillis()};
for (size_t i {0}; i < n; ++i) {
if (measurements[i] and now - measurements[i] < timespan) {
return i;
}
}
return n; // Should be unreachable.
}
template <size_t n> size_t findMaximum(unsigned long const (& measurements)[n], unsigned long const timespan) {
unsigned long const now {pMillis()};
for (size_t i {n}; i--;) {
if (measurements[i] and now - measurements[i] < timespan) {
return i;
}
}
return n; // Should be unreachable.
}
void setup() {
Serial.begin(9600);
}
void loop() {
static unsigned long measurements[50] {};
long const offset {20}; // Temperature is between -20°C and 30°C.
long const temperature {readTemperature()};
measurements[temperature + offset] = pMillis();
Serial.print("Current: ");
Serial.print(temperature);
Serial.print(", Minima (3s, 6s): ");
Serial.print(findMinimum(measurements, 3000) - offset); // Minimum over the last 3s.
Serial.print(", ");
Serial.print(findMinimum(measurements, 6000) - offset); // Minimum over the last 6s.
Serial.print(", Maxima (3s, 6s): ");
Serial.print(findMaximum(measurements, 3000) - offset); // Maximum over the last 3s.
Serial.print(", ");
Serial.println(findMaximum(measurements, 6000) - offset); // Maximum over the last 6s.
delay(1000);
}
On a (simulated) Mega2560, this is the output.
Current: -13, Minima (3s, 6s): -13, -13, Maxima (3s, 6s): -13, -13
Current: 29, Minima (3s, 6s): -13, -13, Maxima (3s, 6s): 29, 29
Current: 3, Minima (3s, 6s): -13, -13, Maxima (3s, 6s): 29, 29
Current: -12, Minima (3s, 6s): -12, -13, Maxima (3s, 6s): 29, 29
Current: 10, Minima (3s, 6s): -12, -13, Maxima (3s, 6s): 10, 29
Current: 2, Minima (3s, 6s): -12, -13, Maxima (3s, 6s): 10, 29
Current: 24, Minima (3s, 6s): 2, -12, Maxima (3s, 6s): 24, 29
Current: 8, Minima (3s, 6s): 2, -12, Maxima (3s, 6s): 24, 24
Current: 3, Minima (3s, 6s): 3, -12, Maxima (3s, 6s): 24, 24
Current: -11, Minima (3s, 6s): -11, -11, Maxima (3s, 6s): 8, 24
Current: 20, Minima (3s, 6s): -11, -11, Maxima (3s, 6s): 20, 24
Current: -5, Minima (3s, 6s): -11, -11, Maxima (3s, 6s): 20, 24
...
You can add as many periods (last hour, last day, last week, etc.) as you like. The maximum time span can be extended by storing seconds or minutes instead of milliseconds (e.g., store pMillis() / 1000, this can also be used to cut down on memory requirements instead (e.g., change the type of measurements to uint16_t[]).