#include <Wire.h>
#include <MPU6050.h>
#include <LiquidCrystal.h>
MPU6050 mpu;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("MPU6050 Demo");
delay(1000);
lcd.clear();
Wire.begin();
mpu.initialize();
}
void loop() {
// Read raw accelerometer data
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Calculate angle deviation in X direction
float angleX = atan2(ay, az) * 180 / M_PI;
// Determine the direction of train wheel turning
String direction;
if (angleX > 0) {
direction = "Left";
} else {
direction = "Right";
}
// Display the result on LCD screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Angle deviation:");
lcd.setCursor(0, 1);
lcd.print(angleX, 2);
lcd.print(" degrees");
lcd.setCursor(11, 1);
lcd.print(direction);
delay(100);
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
imu1:INT
imu1:AD0
imu1:XCL
imu1:XDA
imu1:SDA
imu1:SCL
imu1:GND
imu1:VCC
lcd1:VSS
lcd1:VDD
lcd1:V0
lcd1:RS
lcd1:RW
lcd1:E
lcd1:D0
lcd1:D1
lcd1:D2
lcd1:D3
lcd1:D4
lcd1:D5
lcd1:D6
lcd1:D7
lcd1:A
lcd1:K
pot1:GND
pot1:SIG
pot1:VCC