#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C 20X4
const int soilMoisturePin = 14; // Soil moisture sensor pin
const int vibrationPin = 13; // Vibration sensor pin
const int vibrationThreshold = 550;
const int buzzerPin = 2; // Buzzer pin
const int greenLEDPin = 17; // Green LED pin
const int yellowLEDPin = 16; // Yellow LED pin
const int redLEDPin = 4; // Red LED pin
const int t_p = 33; //
const 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(0,3);
lcd.print("INTEGRATED WITH IOT");
delay(3000);
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); // moisture measurement
int mappedMoisture = map(soilMoisture, 0, 4095, 0, 100); // Map moisture to 0-100 range
int vibrationValue = analogRead(vibrationPin); // vibration measurement
int mappedvibrationValue = map(vibrationValue, 0, 4095, 0, 1000); // Map vibration to 0-1000 range
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(mappedvibrationValue);
lcd.print(" g ");
Serial.print("Vibration: "); // Display vibration on Serial Monitor
Serial.print(mappedvibrationValue);
Serial.println(" g");
// DANGER condition (moisture more than and equal 30%, vibration measured more than or equal 550 g)
// OR (distance less than 100cm,vibration measured more than or equal 550 g)
if (mappedMoisture >= 30 && mappedvibrationValue >= vibrationThreshold ||
distance < 100 && mappedvibrationValue >= vibrationThreshold) {
Serial.println("Danger Condition Detected !!! ");
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Danger ");
for (int i = 0; i < 5; i++){ // red LED and buzzer triggered 5 times
digitalWrite(redLEDPin, HIGH);
tone(buzzerPin, 1000);
delay(200);
digitalWrite(redLEDPin, LOW);
noTone(buzzerPin);
delay(200);
}
// DANGER condition (moisture more than and equal 30%, vibration measured less than 550 g)
// OR (distance less than 100cm,vibration measured less than 550 g)
}else if (mappedMoisture >= 30 && mappedvibrationValue < vibrationThreshold ||
distance < 100 && mappedvibrationValue < vibrationThreshold){
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
lcd.setCursor(11, 0);
lcd.print("Danger ");
for (int i = 0; i < 1; i++){ // red LED and buzzer triggered 1 times
digitalWrite(redLEDPin, HIGH);
tone(buzzerPin, 1000);
delay(500);
digitalWrite(redLEDPin, LOW);
noTone(buzzerPin);
delay(500);
}
}
// ALERT condition (moisture between 20 & 30%, vibration measured more than or equal 550 g)
// OR (distance between 100cm & 200cm,vibration measured more than or equal 550 g)
if (mappedMoisture >= 20 && mappedMoisture < 30 && mappedvibrationValue >= vibrationThreshold ||
distance > 100 && distance < 200 && mappedvibrationValue >= vibrationThreshold) {
Serial.println("Alert Condition Detected ! ");
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Alert ");
for (int i = 0; i < 3; i++) { // yellow LED and buzzer triggered 3 times
digitalWrite(yellowLEDPin, HIGH);
tone(buzzerPin, 1000);
delay(500);
digitalWrite(yellowLEDPin, LOW);
noTone(buzzerPin);
delay(500);
}
// ALERT condition (moisture between 20 & 30%, vibration measured less than 550 g)
// OR (distance between 100cm & 200cm,vibration measured less than 550 g)
}else if (mappedMoisture >= 20 && mappedMoisture < 30 && mappedvibrationValue <= vibrationThreshold ||
distance > 100 && distance < 200 && mappedvibrationValue <= vibrationThreshold){
digitalWrite(greenLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Alert ");
for (int i = 0; i < 1; i++) { // yellow LED and buzzer triggered 1 times
digitalWrite(yellowLEDPin, HIGH);
tone(buzzerPin, 1000);
delay(500);
digitalWrite(yellowLEDPin, LOW);
noTone(buzzerPin);
delay(500);
}
}
// SAFE condition (moisture less than 20%, vibration measured more than or equal 550 g)
// OR (distance between 200cm & 300cm,vibration measured more than or equal 550 g)
if (mappedMoisture > 1 && mappedMoisture < 20 && mappedvibrationValue >= vibrationThreshold ||
distance > 200 && distance < 300 && mappedvibrationValue >= vibrationThreshold){
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Safe ");
for (int i = 0; i < 1; i++) { // green LED and buzzer triggered 1 times
digitalWrite(greenLEDPin, HIGH);
delay(500);
digitalWrite(greenLEDPin, LOW);
delay(500);
}
// SAFE condition (moisture less than 20%, vibration measured less than 550 g)
// OR (distance between 200cm & 300cm,vibration measured less than 550 g)
}else if (mappedMoisture > 1 && mappedMoisture < 20 && mappedvibrationValue <= vibrationThreshold ||
distance > 200 && distance < 300 && mappedvibrationValue <= vibrationThreshold){
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Safe ");
}
if (mappedMoisture <= 1 && distance >= 300){
digitalWrite(greenLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.setCursor(11, 0);
lcd.print("Safe ");
}
}