#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define pins for components
const int redLedPin = 2; // Pin for the red LED
const int yellowLedPin = 3; // Pin for the yellow LED
const int buzzerPin = 5; // Pin for the buzzer
const int trigPin = 6; // Pin for ultrasonic trigger
const int echoPin = 7; // Pin for ultrasonic echo
const int pirPin = 8; // Pin for the PIR sensor
const int buttonPin = 9; // Pin for the push button
// Initialize the LCD object using the LiquidCrystal_I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variable to store the push button state
bool buttonState = false;
void setup() {
// Configure pin modes
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP); // Configure push button with internal pull-up
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Detection System");
lcd.setCursor(0, 1);
lcd.print("Earthquake & Tsunami");
delay(1000);
lcd.clear();
}
void loop() {
// Variables to store time and distance
long duration, distance;
// Read the button state
if (digitalRead(buttonPin) == LOW) { // Button is pressed
buttonState = !buttonState; // Toggle the system state
delay(200); // Button debounce
}
// If the system is off, turn off all components
if (!buttonState) {
lcd.clear();
lcd.print("System: Inactive");
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
noTone(buzzerPin);
delay(1000);
return; // Exit the loop to keep the system off
}
// Start measuring distance using the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Read the value from the PIR sensor
int pirValue = digitalRead(pirPin);
// Logic to determine the status and actions based on the sensors
if (pirValue == HIGH) {
lcd.setCursor(0, 0);
lcd.print("Status: Earthquake!");
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
tone(buzzerPin, 1000);
} else {
noTone(buzzerPin);
if (distance > 300) {
lcd.setCursor(0, 0);
lcd.print("Status: Danger");
digitalWrite(redLedPin, HIGH);
digitalWrite(yellowLedPin, LOW);
tone(buzzerPin, 1000);
} else if (distance >= 50 && distance <= 300) {
lcd.setCursor(0, 0);
lcd.print("Status: Warning");
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
noTone(buzzerPin);
} else {
lcd.setCursor(0, 0);
lcd.print("Status: Safe ");
digitalWrite(redLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
noTone(buzzerPin);
}
}
// Display distance information on the LCD
lcd.setCursor(0, 1);
lcd.print("Height: ");
lcd.print(distance);
lcd.print(" cm");
delay(1000);
lcd.clear();
}
// Function to produce buzzer sound with intervals
void slowBuzzer() {
for (int i = 0; i < 3; i++) {
tone(buzzerPin, 500);
delay(500);
noTone(buzzerPin);
delay(1000);
}
}