#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
Adafruit_MPU6050 mpu;
boolean fall = false; //stores if a fall has occurred
void setup(void) {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE,0);
display.setCursor(0, 0);
display.println("Accelerometer:");
display.display();
// Initialize the MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(1000);
}
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE,0);
display.setCursor(0, 16);
display.print("X: "); display.print(a.acceleration.x);
display.setCursor(0, 32);
display.print("Y: "); display.print(a.acceleration.y);
display.setCursor(0, 48);
display.print("Z: "); display.print(a.acceleration.z);
display.setCursor(64, 16);
display.print("X°: "); display.print(g.gyro.x);
display.setCursor(64, 32);
display.print("Y°: "); display.print(g.gyro.y);
display.setCursor(64, 48);
display.print("Z°: "); display.print(g.gyro.z);
if (!fall) {
display.display();
} else {
// Display "FALL DETECTED" when a fall is detected
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE,0);
display.setCursor(0, 20);
display.println("FALL DETECTED");
tone(15, 262, 500); // Plays 262Hz tone for 5.00 seconds
display.display();
}
// Simulated fall detection logic
if (a.acceleration.z < 0.6) {
fall = true;
}
delay(10);
}