#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 or the address of your module
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address and dimensions if your module is different
// Define LED pins
const int redLED = 7;
const int greenLED = 6;
const int yellowLED = 8;
// Define ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 10;
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight(); // Turn on the backlight
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Measure distance using ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
// Flood alert based on water level (distance)
lcd.clear();
if (distance >= 100 && distance <= 150) {
lcd.print("Safe");
digitalWrite(greenLED, HIGH);
delay(5000); // Safe for 5 seconds
digitalWrite(greenLED, LOW);
} else if (distance >= 50 && distance < 100) {
lcd.print("Caution!");
digitalWrite(yellowLED, HIGH);
delay(3000); // Caution for 3 seconds
digitalWrite(yellowLED, LOW);
} else if (distance < 50) {
lcd.print("Danger! Evacuate!!");
digitalWrite(redLED, HIGH);
delay(2000); // Danger for 2 seconds
digitalWrite(redLED, LOW);
} else {
lcd.print("Safe");
}
}