#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 BUTTON_PIN 7
#define DHT22_PIN 5
LiquidCrystal_I2C lcd(0x27, 20, 4);
dht DHT;
bool displayWaterLevel = true; // Flag to toggle between water level and humidity info
bool lastButtonState = true; // Variable to store the last button state
bool bahayaStatusDisplayed = false; // Flag to track if Bahaya status has been displayed
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(BUTTON_PIN, INPUT_PULLUP); // Set the button pin as input with internal pull-up resistor
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("humidity info system");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Haziq Rohaizan");
lcd.setCursor(0, 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();
}
void displayHumidity() {
int chk = DHT.read22(DHT22_PIN); // Assuming DHT22_PIN is the pin connected to the DHT22 sensor
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity:");
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.print(DHT.temperature);
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("Hum:");
lcd.print(DHT.humidity);
lcd.print("%");
}
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 loop() {
float depth = getDepth();
bool currentButtonState = digitalRead(BUTTON_PIN);
// Check for a change in button state
if (currentButtonState != lastButtonState && currentButtonState == LOW) {
displayWaterLevel = !displayWaterLevel; // Toggle the display flag
bahayaStatusDisplayed = false; // Reset Bahaya status flag
delay(50); // Debounce delay
}
lastButtonState = currentButtonState;
if (displayWaterLevel) {
if (depth > 370) {
digitalWrite(RED_LED_PIN, HIGH);
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("Bahaya");
tone(BUZZER_PIN, 800, 1000);
delay(1000);
bahayaStatusDisplayed = true; // Set Bahaya status displayed flag
} else if (depth >= 320 && depth < 370) {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
displayStatus(depth);
lcd.setCursor(0, 1);
lcd.print("Status :");
lcd.setCursor(9, 1);
lcd.print("Siaga");
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("Selamat");
delay(1000);
}
} else {
if (!bahayaStatusDisplayed && depth > 370) {
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
displayStatus(depth); // Display water level status as Bahaya while humidity info is shown
lcd.setCursor(0, 1);
lcd.print("Status :");
lcd.setCursor(9, 1);
lcd.print("Bahaya");
tone(BUZZER_PIN, 800, 1000);
delay(500);
} else {
displayHumidity(); // Display humidity info
delay(500); // Delay to stabilize the display
}
}
lcd.clear();
delay(100);
}