/*
Title: SeisSense: A.C.S.I. ( Advancing Community Safety with Innovation)
Developer: Mr. Edmon Mancao
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h> // Mahalaga ito to compute the magnitude of earthquake measurement
const int MPU = 0x68; // Adress ito para sa MPU6050 module
const int buzzerPin = 9;
const int ledPin = 13; // Naka-connect and LED sa nasabing pin
LiquidCrystal_I2C lcd(0x27, 16, 2); // Ito ang dalawang model ng LCD nasa likod ng chip ito ba ay 0x27 o 0x3F
bool alarmActive = false;
void setup() {
Wire.begin();
// Para magising o i-activate ang MPU6050
Wire.beginTransmission(MPU);
Wire.write(0x6B); // Ito ay para sa power management register
Wire.write(0); // Set to zero (wakes up the MPU6050)
Wire.endTransmission(true);
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT); // Intialize the Buzzer pin
pinMode(ledPin, OUTPUT); // Initialize the LED pin
// Initialize LCD
lcd.begin(16, 2); // Initialize the LCD
lcd.backlight(); // Turn on backlight
lcd.print("System Ready!");
delay(2000);
lcd.clear();
}
void loop() {
// Read accelerometer data from MPU6050
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Request 6 registers (accelerometer data)
// Combine high and low bytes to get the raw accelerometer values
int AcX = Wire.read() << 8 | Wire.read();
int AcY = Wire.read() << 8 | Wire.read();
int AcZ = Wire.read() << 8 | Wire.read();
// Convert to g's (1g = 9.81m/s^2)
float ax = AcX / 16384.0;
float ay = AcY / 16384.0;
float az = AcZ / 16384.0;
// Compute earthquake magnitude
float magnitude = sqrt(ax * ax + ay * ay + az * az);
// Print accelerometer values and magnitude for debugging
Serial.print("AX: "); Serial.print(ax);
Serial.print(" AY: "); Serial.print(ay);
Serial.print(" AZ: "); Serial.println(az);
Serial.print("Magnitude: "); Serial.println(magnitude);
// Check for significant movement
if (magnitude > 2.0) {
if (!alarmActive) {
alarmActive = true;
sendAlert(ax, ay, az, magnitude);
}
generateSineWaveAlarm();
} else {
if (alarmActive) {
noTone(buzzerPin); // Stop the buzzer if no significant activity
digitalWrite(ledPin, LOW); // Turn off the LED
lcd.clear();
lcd.print("No Activity");
alarmActive = false;
}
}
delay(100);
}
void sendAlert(float x, float y, float z, float mag) {
Serial.println("Earthquake detected!");
Serial.print("X: "); Serial.print(x);
Serial.print(" Y: "); Serial.print(y);
Serial.print(" Z: "); Serial.println(z);
Serial.print("Magnitude: "); Serial.println(mag);
lcd.clear();
lcd.print("Earthquake!");
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Magnitude: ");
lcd.print(mag);
delay(3000); // Display the magnitude for 3 seconds
lcd.clear();
lcd.print("Evacuate Now!");
Serial.println("Activating buzzer and LED...");
digitalWrite(ledPin, HIGH); // Turn on the LED
}
void generateSineWaveAlarm() {
static int i = 0;
float frequency = 1000 + 500 * sin(i * 0.1); // Ma-generate ang frequency sa pagitan ng 500Hz and 1500Hz
tone(buzzerPin, frequency);
delay(50); // Short delay to create the effect of a continuous tone
i++;
}