#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address to 0x27 for a 20x4 display
int soilMoisturePin = 14; // Soil moisture sensor pin
int vibrationPin = 13; // Vibration sensor pin
int vibrationThreshold = 2000;
int buzzerPin = 2; // Buzzer pin
int greenLEDPin = 17; // Green LED pin
int yellowLEDPin = 16; // Yellow LED pin
int redLEDPin = 4; // Red LED pin
int t_p = 33; //
int e_p = 32; //
void setup() {
Serial.begin(115200);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight of the LCD
pinMode(soilMoisturePin, INPUT);
pinMode(vibrationPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(t_p, OUTPUT);
pinMode(e_p, INPUT);
lcd.setCursor(3,0);
lcd.print("DEVELOPMENT OF");
lcd.setCursor(3,1);
lcd.print("LANDSLIDE EARLY");
lcd.setCursor(3,2);
lcd.print("WARNING SYSTEM");
lcd.setCursor(6,3);
lcd.print("WITH IOT");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to the first position of the first line
lcd.print("Condition: ");
lcd.setCursor(0, 1); // Set cursor to the first position of the second line
lcd.print("Moisture : ");
lcd.setCursor(0, 2); // Set cursor to the first position of the third line
lcd.print("Distance : ");
lcd.setCursor(0, 3); // Set cursor to the first position of the forth line
lcd.print("Vibration: ");
}
void loop() {
long duration; // Ultrasonic measurement
float distance;
digitalWrite(t_p, LOW);
delayMicroseconds(2);
digitalWrite(t_p, HIGH);
delayMicroseconds(10);
digitalWrite(t_p, LOW);
duration = pulseIn(e_p, HIGH);
distance = duration * 0.034 / 2;
int soilMoisture = analogRead(soilMoisturePin); // soil moisture measurement
int mappedMoisture = map(soilMoisture, 0, 4095, 0, 100); // Map soil moisture to 0-100 range
int vibrationValue = analogRead(vibrationPin); // vibration measurement
bool newVibrationStatus = (vibrationValue >= vibrationThreshold);
lcd.setCursor(0, 1); // Display mositure on LCD
lcd.print("Moisture : ");
lcd.print(mappedMoisture);
lcd.print(" % ");
Serial.print("Moisture : ");// Display moisture on Serial Monitor
Serial.print(mappedMoisture);
Serial.println(" %");
lcd.setCursor(0, 2); // Display distance on LCD
lcd.print("Distance : ");
lcd.print((int)distance);
lcd.print(" cm ");
Serial.print("Distance: ");// Display distance on Serial Monitor
Serial.print((int)distance);
Serial.println(" cm");
lcd.setCursor(0, 3); // Display vibration on LCD
lcd.print("Vibration: ");
lcd.print(vibrationValue);
lcd.print(" g ");
Serial.print("Vibration: "); // Display vibration on Serial Monitor
Serial.print(vibrationValue);
Serial.println(" g");
// Danger condition which is moisture is more than or equal to 70% and distance is less than or equal to 100cm
if (mappedMoisture >= 70 && vibrationValue >= vibrationThreshold ||
distance <= 100 && vibrationValue >= vibrationThreshold) {
Serial.println("Danger Condition Detected !!! ");
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Danger ");
for (int i = 0; i < 1; i++){
digitalWrite(redLEDPin, HIGH);
tone(buzzerPin, 1000);
delay(500);
digitalWrite(redLEDPin, LOW);
noTone(buzzerPin);
delay(500);
}
// Danger condition which is moisture is more than or equal to 70% and distance is less than or equal to 100cm
}else if (mappedMoisture >= 70 && vibrationValue <= vibrationThreshold ||
distance <= 100 && vibrationValue <= vibrationThreshold){
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
lcd.setCursor(11, 0);
lcd.print("Danger ");
}
// Alert condition which is moisture is between 30% and 70% and distance is between 100cm and 200cm
if (mappedMoisture > 30 && mappedMoisture < 70 && vibrationValue >= vibrationThreshold ||
distance > 100 && distance < 200 && vibrationValue >= vibrationThreshold) {
Serial.println("Alert Condition Detected ! ");
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Alert ");
for (int i = 0; i < 1; i++) {
digitalWrite(yellowLEDPin, HIGH);
tone(buzzerPin, 1000);
delay(500);
digitalWrite(yellowLEDPin, LOW);
noTone(buzzerPin);
delay(500);
}
// Alert condition which is moisture is between 30% and 70% and distance is between 100cm and 200cm
}else if (mappedMoisture > 30 && mappedMoisture < 70 && vibrationValue <= vibrationThreshold ||
distance > 100 && distance < 200 && vibrationValue <= vibrationThreshold){
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Alert ");
}
// Safe condition which is moisture is between 5% and 30% and distance is between 200cm and 300cm
if (mappedMoisture > 5 && mappedMoisture < 30 && vibrationValue >= vibrationThreshold ||
distance > 200 && distance < 300 && vibrationValue >= vibrationThreshold){
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Safe ");
for (int i = 0; i < 1; i++) {
digitalWrite(greenLEDPin, HIGH);
delay(500);
digitalWrite(greenLEDPin, LOW);
delay(500);
}
// Safe condition which is moisture is between 5% and 30% and distance is between 200cm and 300cm
}else if (mappedMoisture > 5 && mappedMoisture < 30 && vibrationValue <= vibrationThreshold ||
distance > 200 && distance < 300 && vibrationValue <= vibrationThreshold){
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Safe ");
}
if (mappedMoisture <= 5 && distance >= 300){
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Safe ");
}
}