#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Servo.h>
Adafruit_MPU6050 mpu;
Servo myServo;
void setup() {
Serial.begin(115200);
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
while (1);
}
Serial.println("MPU6050 ready!");
// Attach servo to pin 11
myServo.attach(11);
}
void loop() {
sensors_event_t event;
// Get accelerometer data
mpu.getAccelerometerSensor()->getEvent(&event);
// Print acceleration values for debugging
Serial.print("X: ");
Serial.print(event.acceleration.x);
Serial.print(", Y: ");
Serial.print(event.acceleration.y);
Serial.print(", Z: ");
Serial.println(event.acceleration.z);
// Map acceleration value from g (gravity) to servo angle (0-180)
int servoPosition = map(event.acceleration.x * 100, -100, 100, 0, 180);
// Constrain servo position to valid range
servoPosition = constrain(servoPosition, 0, 180);
// Set servo position
myServo.write(servoPosition);
delay(100); // Small delay for stability
}