#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// Initialize the LCD with the I2C address, 16 columns, and 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2); // Update 0x27 with your LCD's I2C address
Adafruit_MPU6050 mpu;
int flexSensorPin = 25; // Analog pin to read the flex sensor output
int emgSensorPin = 33; // Pin to read the EMG sensor output
int flexValue; // Variable to store the flex sensor value
int angle; // Variable to store the angle from the flex sensor
int emgValue; // Variable to store the EMG sensor value
int displayState = 0; // Variable to cycle through different displays
unsigned long lastDisplayChange = 0; // To track time for cycling
unsigned long displayInterval = 3000; // Change display every 3 seconds
void setup(void) {
Serial.begin(115200);
// Initialize the LCD
lcd.init();
lcd.backlight();
delay(100);
// Display initial message
lcd.setCursor(0, 0);
lcd.print("Initializing...");
// Initialize MPU6050 sensor
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
lcd.setCursor(0, 1);
lcd.print("MPU6050 Error");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
delay(1000); // Pause to see the initialization message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ESP32 MPU6050");
delay(1000);
}
void loop() {
// Get current time
unsigned long currentMillis = millis();
// Check if it's time to change the display
if (currentMillis - lastDisplayChange > displayInterval) {
displayState = (displayState + 1) % 4; // Cycle through 0, 1, 2, and 3
lastDisplayChange = currentMillis;
}
// Get new sensor events with the readings
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Read the analog value from the flex sensor
flexValue = analogRead(flexSensorPin);
angle = map(flexValue, 0, 2073, 0, 180); // Map flex sensor value to angle (0-180 degrees)
// Read the analog value from the EMG sensor
emgValue = analogRead(emgSensorPin); // Assuming the EMG sensor outputs analog values
// Clear LCD and display based on the current display state
lcd.clear();
if (displayState == 0) {
// Display accelerometer data
lcd.setCursor(0, 0);
lcd.print("Accel X,Y,Z:");
lcd.setCursor(0, 1);
lcd.print(a.acceleration.x, 1);
lcd.print(",");
lcd.print(a.acceleration.y, 1);
lcd.print(",");
lcd.print(a.acceleration.z, 1);
} else if (displayState == 1) {
// Display gyroscope data
lcd.setCursor(0, 0);
lcd.print("Gyro X,Y,Z:");
lcd.setCursor(0, 1);
lcd.print(g.gyro.x, 1);
lcd.print(",");
lcd.print(g.gyro.y, 1);
lcd.print(",");
lcd.print(g.gyro.z, 1);
} else if (displayState == 2) {
// Display flex sensor data
lcd.setCursor(0, 0);
lcd.print("Flex Angle:");
lcd.setCursor(0, 1);
lcd.print(angle);
} else if (displayState == 3) {
// Display EMG sensor data
lcd.setCursor(0, 0);
lcd.print("EMG Value:");
lcd.setCursor(0, 1);
lcd.print(emgValue);
}
// Print the flex and EMG values to the serial monitor
Serial.print("Raw Flex Value: ");
Serial.println(flexValue);
Serial.print("Angle: ");
Serial.println(angle);
Serial.print("Raw EMG Value: ");
Serial.println(emgValue);
delay(500); // Slow down the updates for readability
}