#include <LiquidCrystal.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 m_p_u;
LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
// Set the I2C address and LCD dimensions (16 columns, 2 rows)
float rotationX = 0.0; // Declare rotationX in the global scope
float accelX;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2); // Initialize the LCD
lcd.clear(); // Clear the LCD screen
while (!Serial)
delay(200);
if (!m_p_u.begin()) {
lcd.print("MPU6050 not found");
while (1) {
// If the MPU6050 sensor is not found, loop indefinitely
}
}
lcd.println("Need Water");
}
void loop() {
sensors_event_t acc, gcc, temp;
m_p_u.getEvent(&acc, &gcc, &temp);
// Read sensor values and calculate rotation
rotationX = (gcc.gyro.x) * 180 / PI; // Update the global rotationX value
accelX = acc.acceleration.x;
// Print rotation values to the serial monitor
Serial.print("Rotation on x axis: ");
Serial.println(rotationX);
// Update the LCD based on the rotation values
lcd.clear(); // Clear the LCD screen before printing new data
if (accelX <= 5) {
if (rotationX >= 60.0 && rotationX <= 100.0) {
lcd.setCursor(0, 0);
lcd.print("NEED WATER");
Serial.println("NEED WATER");
} else if (rotationX >= 101.0 && rotationX <= 180.0) {
lcd.setCursor(0, 0);
lcd.print("NEED FOOD");
Serial.println("NEED FOOD");
}
float rotationZ = (gcc.gyro.z) * 180 / PI; // Calculate rotation on z-axis
if (rotationZ >= 100.0 && rotationZ <= 130.0) {
lcd.setCursor(0, 0);
lcd.print("GO TO WASH ROOM");
Serial.println("GO TO WASH ROOM");
} else if (rotationZ >= 30.0 && rotationZ <= 90.0) {
lcd.setCursor(0, 0);
lcd.print("CALL ALERT");
Serial.println("CALL ALERT");
}
} else if(accelX>5) { // Added proper else statement with curly braces
Serial.println("Acceleration on X axis:");
Serial.println(accelX);
lcd.setCursor(0, 1);
lcd.print("FALL DETECTED ");
lcd.setCursor(0, 0);
lcd.print("EMERGENCY ");
}
delay(1000); // Optional delay for stability
lcd.setCursor(0, 0);
delay(1000); // This speeds up the simulation
}