#define SENSOR_PIN 28 // Analog pin where the temperature sensor is connected
int highestValue = -273; // Lowest possible temperature in Celsius
int lowestValue = 1000; // Highest possible temperature in Celsius
unsigned long startTime = 0; // Variable to store the start time
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
startTime = millis(); // Record the start time
}
void loop() {
// Check if 5 seconds have elapsed since the start
if (millis() - startTime < 5000) {
// Read the analog value from the temperature sensor
int sensorValue = analogRead(SENSOR_PIN);
// Convert the analog value to temperature in Celsius
float temperature = (sensorValue / 1023.0) * 5000; // Convert to millivolts
temperature = (temperature - 500) / 10; // Convert to Celsius
// Track the highest and lowest values
if (temperature > highestValue) {
highestValue = temperature;
}
if (temperature < lowestValue) {
lowestValue = temperature;
}
}
// Print the current temperature and calibration results after 5 seconds
else {
Serial.print("Calibration Completed! Highest Temperature: ");
Serial.print(highestValue);
Serial.print(" °C, Lowest Temperature: ");
Serial.println(lowestValue);
// In a real-world application, you may want to perform additional actions here
// For simulation purposes, let's just stop the loop
//while (true) {} // Infinite loop to stop further execution
}
delay(1000); // Delay for 1 second before next reading
}
Loading
ds18b20
ds18b20