#include<Wire.h>
#include<MPU6050.h>
const int MPU_addr = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
float ax = 0, ay = 0, az = 0, gx = 0, gy = 0, gz = 0;
int angleChange = 0;
bool simulateFall = true; // Set to true for fall simulation, false for real MPU6050 readings
void setup() {
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
pinMode(9, OUTPUT); // Buzzer
}
void loop() {
if (simulateFall) {
simulateSensorData();
} else {
mpu_read();
}
// Process accelerometer data (replace these with your calibration values)
ax = (AcX - 2050) / 16384.00;
ay = (AcY - 77) / 16384.00;
az = (AcZ - 1947) / 16384.00;
// Process gyroscope data (replace these with your calibration values)
gx = (GyX + 270) / 131.07;
gy = (GyY - 351) / 131.07;
gz = (GyZ + 136) / 131.07;
// Calculate amplitude vector and angle change
float Raw_AM = pow(pow(ax, 2) + pow(ay, 2) + pow(az, 2), 0.5);
int AM = Raw_AM * 10;
angleChange = pow(pow(gx, 2) + pow(gy, 2) + pow(gz, 2), 0.5);
Serial.println(AM);
Serial.println(angleChange);
// Fall detection logic
if (AM >= 10) {
digitalWrite(9, HIGH); // Activate buzzer
Serial.println("FALL DETECTED");
delay(5000); // Buzzer on for 5 seconds
} else {
digitalWrite(9, LOW); // Deactivate buzzer
}
delay(1000); // Delay for readability
}
void mpu_read() {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
}
void simulateSensorData() {
// Simulate normal movement
AcX = random(1800, 2200); // Simulated X-axis acceleration
AcY = random(-100, 100); // Simulated Y-axis acceleration
AcZ = random(1800, 2200); // Simulated Z-axis acceleration
GyX = random(-300, 300); // Simulated X-axis angular velocity
GyY = random(-300, 300); // Simulated Y-axis angular velocity
GyZ = random(-300, 300); // Simulated Z-axis angular velocity
// Introduce a simulated fall every 10 loops
static int counter = 0;
if (++counter == 10) {
AcX = 5000; // Simulated high acceleration during fall
AcY = 5000; // Simulated high acceleration during fall
AcZ = 5000; // Simulated high acceleration during fall
GyX = 10000; // Simulated high angular velocity during fall
GyY = 10000; // Simulated high angular velocity during fall
GyZ = 10000; // Simulated high angular velocity during fall
counter = 0; // Reset counter
}
}