#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT.h>
#define DHTPIN 7
#define DHTTYPE DHT22
const int buzzerPin = 5;
const float threshold = 0.5;
const int pirPin = 8;
int pirState = LOW;
int servoPin = 9;
Servo myservo;
Adafruit_MPU6050 mpu;
LiquidCrystal_I2C lcd(0x27, 20, 4);
DHT dht(DHTPIN, DHTTYPE);
sensors_event_t event;
float ax_prev, ay_prev;
bool earthquakeDetected = false;
float temperature, humidity, dewPoint, heatIndex;
const unsigned long earthquakeAlertDuration = 5000;
unsigned long startTime; // Declare startTime at a higher scope
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
while (!mpu.begin()) {
delay(1000);
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
myservo.attach(servoPin);
myservo.write(90);
dht.begin();
pinMode(buzzerPin, OUTPUT);
pinMode(pirPin, INPUT);
ax_prev = 0.0;
ay_prev = 0.0;
}
void loop() {
mpu.getAccelerometerSensor()->getEvent(&event);
pirState = digitalRead(pirPin);
temperature = dht.readTemperature();
humidity = dht.readHumidity();
// Calculate dew point using a separate function (you need to implement this)
dewPoint = calculateDewPoint(temperature, humidity);
heatIndex = dht.computeHeatIndex(temperature, humidity, false);
if (!earthquakeDetected && (
abs(event.acceleration.x - ax_prev) > threshold ||
abs(event.acceleration.y - ay_prev) > threshold )) {
earthquakeDetected = true;
digitalWrite(buzzerPin, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("*** EARTHQUAKE! ***");
lcd.setCursor(0, 1);
lcd.print("Seek safe shelter!");
startTime = millis();
while (millis() - startTime < earthquakeAlertDuration) {
}
}
if (pirState == HIGH) {
myservo.write(0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("** Animal on road! **");
lcd.setCursor(0, 1);
lcd.print("Roads closed!");
delay(5000);
} else if (pirState == LOW && !earthquakeDetected) {
myservo.write(90);
delay(5000);
}
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.setCursor(6, 1);
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(1, 3);
lcd.print("Humid:");
lcd.setCursor(7, 3);
lcd.print(humidity);
lcd.print("%");
ax_prev = event.acceleration.x;
ay_prev = event.acceleration.y;
if (earthquakeDetected && (millis() - startTime >= earthquakeAlertDuration)) {
earthquakeDetected = false;
digitalWrite(buzzerPin, LOW);
}
delay(500);
}
float calculateDewPoint(float temperature, float humidity) {
return 0;
}