#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <SoftwareSerial.h>
// Initialize MPU6050
Adafruit_MPU6050 mpu;
// Initialize LCD (I2C address 0x27, 16 columns, 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define flex sensor pins
const int FLEX_SENSOR_PIN1 = A0;
const int FLEX_SENSOR_PIN2 = A1;
const int FLEX_SENSOR_PIN3 = A2;
const int FLEX_SENSOR_PIN4 = A3;
// MP3 Module connections
const int RX_PIN = 10;
const int TX_PIN = 11;
SoftwareSerial
mp3Module(RX_PIN, TX_PIN);
void setup(void) {
// Initialize Serial for debugging
Serial.begin(115200);
while (!Serial) delay(10); // Pause for serial connection
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) delay(10);
}
Serial.println("MPU6050 Found!");
// MPU6050 settings
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MPU6050 Ready");
delay(1000);
// Initialize MP3 module
mp3Module.begin(9600);
setMP3Volume(30);
}
void setMP3Volume(uint8_t volume) {
uint8_t command[] = {0x7E, 0xFF, 0x06, 0x06, 0x00, 0x00, volume, 0xEF};
mp3Module.write(command, sizeof(command));
}
void playMP3(uint8_t track) {
// Send command to play specific track on the MP3 module
uint8_t command[] = {0x7E, 0xFF, 0x06, 0x03, 0x00, 0x00, track, 0xEF};
mp3Module.write(command, sizeof(command)); // Send the entire command array
}
void loop() {
// Get sensor events from MPU6050
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Read the flex sensor values
int flex_value1 = analogRead(FLEX_SENSOR_PIN1);
int flex_value2 = analogRead(FLEX_SENSOR_PIN2);
int flex_value3 = analogRead(FLEX_SENSOR_PIN3);
int flex_value4 = analogRead(FLEX_SENSOR_PIN4);
// Clear LCD
lcd.clear();
lcd.setCursor(0, 0);
// Check for flex sensor activations
if (flex_value1 > 512 && flex_value2 > 512) { // Flex 1 and Flex 2 bent simultaneously
lcd.print("I am feeling well");
} else if (flex_value1 > 512) { // Flex 1 bent
lcd.print("I need water");
} else if (flex_value2 > 512) { // Flex 2 bent
lcd.print(" washroom");
playMP3(1); // Play corresponding track
} else if (flex_value3 > 512) { // Flex 3 bent
lcd.print("TAKE SHOWER");
playMP3(2); // Play corresponding track
} else if (flex_value4 > 512) { // Flex 4 bent
lcd.print("I need meal");
playMP3(3); // Play corresponding track
} else if (a.acceleration.x >9) { // X-axis condition
lcd.print("Hello");
}
else if (a.acceleration.x >6 & a.acceleration.x <9) { // X-axis condition
lcd.print("HOW ARE YOU");
}
else {
// Display MPU6050 data if no flex sensors are bent
lcd.print("Ax:");
lcd.print(a.acceleration.x, 1);
lcd.setCursor(8, 0);
lcd.print("Ay:");
lcd.print(a.acceleration.y, 1);
lcd.setCursor(0, 1);
lcd.print("Gz:");
lcd.print(g.gyro.z, 1);
lcd.setCursor(8, 1);
lcd.print("flex_value2");
}
// Wait for 500ms before the next update
delay(500);
}
Comments
Please log in or sign up to comment.