#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address for 16x2 LCD
const int triggerPin = 14; // Ultrasonic sensor trigger pin
const int echoPin = 12; // Ultrasonic sensor echo pin
const int buzzerPin = 5; // Buzzer pin
const int greenLED = 15; // Green LED pin
const int yellowLED = 2; // Yellow LED pin
const int redLED = 4;
const int LED = 18;
const int floatSwitch = 13;
const int maxDistance = 400; // Maximum range of the ultrasonic sensor in cm
const int numParts = 3; // Number of parts to divide the range into
const int partDistance = maxDistance / numParts; // Distance for each part
int moodScore = 0;
void setup() {
Serial.begin(9600);
Wire.begin(21, 22);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("FLOOD MONITORING");
lcd.setCursor(2, 1);
lcd.print(" SYSTEM");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LMGI(ECE)");
lcd.setCursor(2, 1);
lcd.print("(2021-2025)");
delay(3000);
lcd.clear();
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
long duration, distance;
// Trigger the ultrasonic sensor
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Measure the time it takes for the pulse to return
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = (duration / 2) / 29.1;
// Determine the water level description
String waterLevel = "Unknown";
if (distance < partDistance) {
waterLevel = "RED ALERT";
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(2000);
digitalWrite(buzzerPin, LOW);
delay(2000);
digitalWrite(buzzerPin, HIGH);
delay(2000);
digitalWrite(buzzerPin, LOW);
} else if (distance < partDistance * 2) {
waterLevel = "DANGER";
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
digitalWrite(buzzerPin, HIGH);
delay(3000);
digitalWrite(buzzerPin, LOW);
}
else {
waterLevel = "NORMAL";
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(buzzerPin, LOW);
delay(3000);
}
// Display the water level and distance on the LCD with proper formatting
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALERT: ");
lcd.print(waterLevel);
lcd.setCursor(2, 1);
lcd.print("LEVEL: ");
lcd.print(distance);
lcd.print(" cm");
// Delay for one second between readings
int potValue = analogRead(floatSwitch);
// Map the potentiometer value (0-1023) to the moodScore range (0-10)
moodScore = map(potValue, 0, 1023, 0, 10);
// Print the current moodScore to the serial monitor
Serial.print("Mood Score: ");
Serial.println(moodScore);
// Check if moodScore is below 5
if (moodScore < 5) {
// Turn on the LED
digitalWrite(LED, HIGH);
// Activate the buzzer
tone(buzzerPin, 1000); // You can change the frequency as needed
// Display "Diffuser On" on LCD
} else {
// Turn off the LED
digitalWrite(LED, LOW);
// Deactivate the buzzer
noTone(buzzerPin);
}
}