#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // DS18B20 data pin
#define BUZZER_PIN 5 // Buzzer pin
#define BUTTON_PIN 15 // Push button pin
#define TEMP_SETPOINT 25 // Temperature setpoint in Celsius
#define TEMP_THRESHOLD 1 // Temperature deviation threshold in Celsius
#define BUZZER_DURATION 1000 // Duration of buzzer sound in milliseconds
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempSensor;
bool alarmActive = false;
bool alarmSnoozed = false;
unsigned long snoozeStartTime = 0;
const unsigned long snoozeDuration = 60000; // Snooze duration in milliseconds (1 minute)
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
sensors.begin();
sensors.getAddress(tempSensor, 0);
sensors.setResolution(tempSensor, 12);
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempC(tempSensor);
if (temperature != DEVICE_DISCONNECTED_C && !alarmSnoozed) {
Serial.print("Temperature: ");
Serial.println(temperature);
if (temperature > TEMP_SETPOINT + TEMP_THRESHOLD || temperature < TEMP_SETPOINT - TEMP_THRESHOLD) {
activateAlarm();
} else {
deactivateAlarm();
}
}
if (digitalRead(BUTTON_PIN) == LOW) {
// Snooze button is pressed
if (!alarmSnoozed) {
alarmSnoozed = true;
snoozeStartTime = millis();
Serial.println("Alarm snoozed.");
}
}
if (alarmSnoozed && millis() - snoozeStartTime >= snoozeDuration) {
// Snooze duration elapsed, reset alarm snooze
alarmSnoozed = false;
Serial.println("Alarm snooze ended.");
}
delay(1000); // Delay to avoid excessive looping
}
void activateAlarm() {
if (!alarmActive) {
alarmActive = true;
digitalWrite(BUZZER_PIN, HIGH);
delay(BUZZER_DURATION);
digitalWrite(BUZZER_PIN, LOW);
Serial.println("Alarm activated.");
}
}
void deactivateAlarm() {
if (alarmActive) {
alarmActive = false;
digitalWrite(BUZZER_PIN, LOW);
Serial.println("Alarm deactivated.");
}
}