#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD with I2C address 0x27 and 16x2 display
Adafruit_MPU6050 m_p_u; // Create an MPU6050 object
// Define threshold values for different statuses
const float ACC_WARNING_VALUE = 4.0; // Acceleration value for warning
const float GYRO_WARNING_VALUE = 100.0; // Gyro value for warning
const float ACC_DANGER_VALUE = 0.5; // Normal value for acceleration
const float GYRO_DANGER_VALUE = 10.0; // Normal value for gyro
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
Serial.begin(115200); // Start serial communication at 115200 baud rate
while(!Serial)
delay(20); // Wait for serial connection to be established
if(!m_p_u.begin()){
while(1){
delay(20); // Halt if MPU6050 initialization fails
}
}
}
void loop() {
// Get sensor readings
sensors_event_t accEvent, gyroEvent, tempEvent;
m_p_u.getEvent(&accEvent, &gyroEvent, &tempEvent);
// Calculate the magnitude of the acceleration vector
float accelMag = sqrt(accEvent.acceleration.x * accEvent.acceleration.x +
accEvent.acceleration.y * accEvent.acceleration.y +
accEvent.acceleration.z * accEvent.acceleration.z);
// Calculate the magnitude of the gyroscope vector
float gyroMag = sqrt(gyroEvent.gyro.x * gyroEvent.gyro.x +
gyroEvent.gyro.y * gyroEvent.gyro.y +
gyroEvent.gyro.z * gyroEvent.gyro.z);
// Get status message for normal conditions
String statusMessageNormal = getStatusMessage(accelMag, gyroMag);
// Print the normal status message on the screen
print_on_screen(statusMessageNormal);
delay(2000); // Delay before the next reading
// Get and print warning status message
String statusMessageWarning = getStatusMessage(ACC_WARNING_VALUE, GYRO_WARNING_VALUE);
print_on_screen(statusMessageWarning);
delay(2000);
// Get and print danger status message
String statusMessageDanger = getStatusMessage(ACC_DANGER_VALUE, GYRO_DANGER_VALUE);
print_on_screen(statusMessageDanger);
delay(2000);
}
// Function to print message on the LCD screen
void print_on_screen(String statusMessage) {
lcd.clear(); // Clear the LCD display
int messageLength = statusMessage.length(); // Get the length of the message
if (messageLength <= 16) {
// If the message fits in one line, print it normally
lcd.setCursor(0, 0);
lcd.print(statusMessage);
} else {
// Scroll the message if it's longer than the LCD width
for (int i = 0; i <= messageLength - 16; i++) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(statusMessage.substring(i, i + 16));
delay(50); // Delay to create scrolling effect
}
}
}
// Function to determine the status message based on sensor values
String getStatusMessage(float accelMag, float gyroMag) {
// Check if the sensor readings match the warning values
if (fabs(accelMag - ACC_WARNING_VALUE) < 0.1 &&
fabs(gyroMag - GYRO_WARNING_VALUE) < 0.1) {
return "Warning signs\nContact your physician";
} else if (
fabs(accelMag - ACC_DANGER_VALUE) < 0.1 &&
fabs(gyroMag - GYRO_DANGER_VALUE) < 0.1) {
return "DANGER CONTACT THE NEAREST HOSPITAL";
} else {
return "Status is normal";
}
}