#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
#define BUZZER_PIN 3
#define LED_PIN 4
#define TEMPERATURE_THRESHOLD 30.0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
sensors.begin();
}
void loop() {
float temperature = getTemperature();
Serial.print("Temperature: ");
Serial.println(temperature);
if (temperature > TEMPERATURE_THRESHOLD) {
activateAlarm();
} else {
deactivateAlarm();
}
delay(1000);
}
float getTemperature() {
sensors.requestTemperatures();
return sensors.getTempCByIndex(0);
}
void activateAlarm() {
tone(BUZZER_PIN, 1000); // Play a 1000Hz tone on the buzzer
digitalWrite(LED_PIN, HIGH);
}
void deactivateAlarm() {
noTone(BUZZER_PIN); // Turn off the buzzer
digitalWrite(LED_PIN, LOW);
}