#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // DHT sensor type
#define RED_PIN 9
#define GREEN_PIN 10
#define BLUE_PIN 11
#define BUZZER_PIN 3
DHT_Unified dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
noTone(BUZZER_PIN); // Ensure the buzzer is initially off
}
void loop() {
sensors_event_t event;
dht.temperature().getEvent(&event);
float temperature = event.temperature;
if (!isnan(temperature)) {
Serial.print("Temperature: ");
Serial.println(temperature);
if (temperature < 18.0) {
// Set the RGB LED to blue
setColor(0, 0, 255);
} else if (temperature >= 18.0 && temperature <= 30.0) {
// Set the RGB LED to yellow
setColor(255, 255, 0);
} else {
// Set the RGB LED to red
setColor(255, 0, 0);
}
if (temperature > 40.0) {
// Trigger the buzzer when temperature is greater than 40 degrees
tone(BUZZER_PIN, 1000); // You can adjust the tone frequency
} else {
noTone(BUZZER_PIN); // Turn off the buzzer
}
} else {
Serial.println("Failed to read temperature from the sensor.");
}
delay(2000); // You can adjust the delay as needed
}
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}