// After running the simulator, click on the DS18B20 chip to change the temperature
// Chip by bonnyr, source code: https://github.com/bonnyr/wokwi-ds1820-custom-chip/
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(10);
DallasTemperature sensors(&oneWire);
float lastTemperature = 0; // Variable to store the last temperature reading
unsigned long lastCheckTime = 0; // Variable to store the last time data was checked
unsigned long lastIncreaseTime = 0; // Variable to store the time when temperature started increasing
const unsigned long checkInterval = 5000; // Interval for checking temperature data (in milliseconds)
const unsigned long increaseThreshold = 10000; // Threshold for detecting increase trend (1 hour in milliseconds)
void setup() {
Serial.begin(9600);
sensors.begin();
}
void loop() {
unsigned long currentTime = millis();
// Check if it's time to read temperature data
if (currentTime - lastCheckTime >= checkInterval) {
lastCheckTime = currentTime;
sensors.requestTemperatures(); // Request temperature data
float currentTemperature = sensors.getTempCByIndex(0); // Read temperature in Celsius
// Check if temperature reading is valid
if (currentTemperature != -127.00) {
Serial.print("Temperature: ");
Serial.print(currentTemperature);
Serial.println(" °C");
// Determine trend
if (currentTemperature > lastTemperature) {
Serial.println("Uptrend");
if (lastIncreaseTime == 0) {
lastIncreaseTime = currentTime; // Start the increase trend timer
} else if (currentTime - lastIncreaseTime >= increaseThreshold) {
Serial.println("Temperature has been increasing!"); // Trigger message if temperature has been increasing for 1 hour
}
} else if (currentTemperature < lastTemperature) {
Serial.println("Downtrend");
lastIncreaseTime = 0; // Reset the increase trend timer
} else {
Serial.println("No change");
lastIncreaseTime = 0; // Reset the increase trend timer
}
lastTemperature = currentTemperature; // Update last temperature reading
} else {
Serial.println("Error: Failed to read temperature data");
}
}
}