#include <Adafruit_Sensor.h>
#include <DHT.h>
#define BUZZER_PIN 14 // Replace with the pin your buzzer is connected to
#define LDR_PIN 26 // Replace with the pin your LDR sensor is connected to
const int DHT_PIN = 15;
#define RAIN_ANALOG 34
#define RAIN_DIGITAL 16
DHT dhtSensor(DHT_PIN, DHT22);
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LDR_PIN, INPUT);
pinMode(RAIN_ANALOG, INPUT);
pinMode(RAIN_DIGITAL, INPUT);
analogReadResolution(10);
}
void loop() {
// Read temperature and humidity from DHT22
float temperature = dhtSensor.readTemperature();
float humidity = dhtSensor.readHumidity();
Serial.println("Temp: " + String(temperature, 2) + "°C");
Serial.println("Humidity: " + String(humidity, 1) + "%");
Serial.println("---");
// Read LDR sensor value
int ldrValue = analogRead(LDR_PIN);
Serial.println("LDR Value: " + String(ldrValue));
// Read rain sensor values
int rainAnalogValue = analogRead(RAIN_ANALOG);
bool isRaining = digitalRead(RAIN_DIGITAL);
Serial.print("Rain Analog Value: ");
Serial.println(rainAnalogValue);
if (isRaining) {
Serial.println("It's raining!");
tone(BUZZER_PIN, 1000); // Play a tone for rain condition
} else {
noTone(BUZZER_PIN); // Turn off the buzzer if no rain condition
}
// Check conditions and control buzzer
if (temperature >= 40.0 && humidity >= 60.0) {
tone(BUZZER_PIN, 1500); // Play a tone for DHT22 conditions
} else if (ldrValue > 300) {
tone(BUZZER_PIN, 2000); // Play a tone for LDR conditions
} else {
noTone(BUZZER_PIN); // Turn off the buzzer if no conditions are met
}
delay(2000); // Adjust this delay as needed
}