#include <OneWire.h>
#include <DallasTemperature.h>
// Define the pins for the components
const int yellowLED = 2;
const int greenLED = 3;
const int blueLED = 4;
const int redLED = 5;
const int buzzer = 6;
const int temperatureSensorPin = A0;
// Define the temperature thresholds
const int lowTempThreshold = 35;
const int midTempThreshold = 50;
const int highTempThreshold = 90;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(temperatureSensorPin);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup() {
// Initialize the digital pins
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(buzzer, OUTPUT);
// Start serial communication for debugging
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop() {
// Request temperature readings
sensors.requestTemperatures();
// Read the temperature from the sensor
float tempCelsius = sensors.getTempCByIndex(0);
// Print temperature to serial monitor
Serial.print("Temperature: ");
Serial.print(tempCelsius);
Serial.println(" C");
// Check temperature thresholds and control the lights and buzzer accordingly
if (tempCelsius < lowTempThreshold) {
// Blink yellow LED
blinkLED(yellowLED);
} else if (tempCelsius >= lowTempThreshold && tempCelsius < midTempThreshold) {
// Blink green LED
blinkLED(greenLED);
} else if (tempCelsius >= midTempThreshold && tempCelsius < highTempThreshold) {
// Blink blue LED
blinkLED(blueLED);
} else if (tempCelsius >= highTempThreshold && tempCelsius < 100) {
// Blink red LED
blinkLED(redLED);
} else {
// Blink red LED and sound buzzer
blinkLED(redLED);
soundBuzzer();
}
}
// Function to blink an LED
void blinkLED(int pin) {
digitalWrite(pin, HIGH);
delay(500);
digitalWrite(pin, LOW);
delay(500);
}
// Function to sound the buzzer
void soundBuzzer() {
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
delay(1000);
}