#include <LiquidCrystal_I2C.h>
#include <dht.h>
#define ECHO_PIN 2
#define TRIG_PIN 3
#define BUZZER_PIN 4
#define RED_LED_PIN 13
#define YELLOW_LED_PIN 12
#define GREEN_LED_PIN 11
#define DHT22_PIN 5
LiquidCrystal_I2C lcd(0x27, 20, 4);
dht DHT;
void setup() {
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(DHT22_PIN, INPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(YELLOW_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
tone(BUZZER_PIN, 300, 500);
lcd.setCursor(4, 0);
lcd.print("Early Flood");
lcd.setCursor(4, 1);
lcd.print("Detection with");
lcd.setCursor(0, 2);
lcd.print("weather info system");
delay(2000);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Haziq Rohaizan");
lcd.setCursor(2, 1);
lcd.print("52103322068");
delay(3000);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
lcd.clear();
}
float getDepth() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return 400 - (duration * 0.034 / 2);
}
void displayStatus(float depth) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Depth :");
lcd.setCursor(7, 0);
lcd.print(depth);
lcd.setCursor(14, 0);
lcd.print("cm");
}
void blinkLED(int led_pinbut) {
digitalWrite(led_pinbut, HIGH);
delay(1000); // Blink duration
digitalWrite(led_pinbut, LOW);
delay(1000); // Blink duration
}
bool isRaining(float hum, float temp) {
// Define the conditions for rain based on humidity and temperature
if (hum > 80.0 && temp < 26.00) {
return true; // Return true if conditions indicate rain
}
return false; // Return false otherwise
}
void loop() {
float depth = getDepth();
int chk = DHT.read22(DHT22_PIN);
float temp = DHT.temperature;
float hum = DHT.humidity;
if (depth > 370) {
blinkLED(RED_LED_PIN);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
displayStatus(depth);
lcd.setCursor(0, 1);
lcd.print("Status :");
lcd.setCursor(9, 1);
lcd.print("!Danger!");
lcd.setCursor(2, 2);
lcd.print("Weather Status:");
if(isRaining(hum,temp)){
lcd.setCursor(4, 3);
lcd.print("Raining");
}else{
lcd.setCursor(4, 3);
lcd.print("Not Raining");
}
tone(BUZZER_PIN, 800, 1000);
delay(1000);
} else if (depth >= 320 && depth < 370) {
digitalWrite(RED_LED_PIN, LOW);
blinkLED(YELLOW_LED_PIN);
digitalWrite(GREEN_LED_PIN, LOW);
displayStatus(depth);
lcd.setCursor(0, 1);
lcd.print("Status :");
lcd.setCursor(9, 1);
lcd.print("Alert");
lcd.setCursor(2, 2);
lcd.print("Weather Status:");
if(isRaining(hum,temp)){
lcd.setCursor(4, 3);
lcd.print("Raining");
}else{
lcd.setCursor(4, 3);
lcd.print("Not Raining");
}
delay(1000);
} else {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
displayStatus(depth);
lcd.setCursor(0, 1);
lcd.print("Status :");
lcd.setCursor(9, 1);
lcd.print("Safe");
int chk = DHT.read22(DHT22_PIN); // Assuming DHT22_PIN is the pin connected to the DHT22 sensor
lcd.setCursor(2, 2);
lcd.print("Weather Status:");
if(isRaining(hum,temp)){
lcd.setCursor(4, 3);
lcd.print("Raining");
}else{
lcd.setCursor(4, 3);
lcd.print("Not Raining");
}
delay(1000);
}
lcd.clear();
delay(100);
}