#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>
#include <RTClib.h>
MPU6050 gyro;
Servo servoMotor;
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
Wire.begin();
gyro.initialize();
servoMotor.attach(9); // Adjust the pin for your servo motor
// Check sensor connections
Serial.println(gyro.testConnection() ? "Gyroscope connection successful" : "Failed to connect to the gyroscope");
delay(1000);
}
void loop() {
int16_t ax, ay, az, gx, gy, gz;
// Read data from the gyroscope
gyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Control the servo based on gyroscope data
int servoPosition = map(gx, -32768, 32767, 0, 180); // Adjust the range as needed
servoMotor.write(servoPosition);
// Display data on Serial Monitor
Serial.println("Gyroscope (deg/s):");
Serial.print("X="); Serial.print(gx); Serial.print("\t");
Serial.print("Y="); Serial.print(gy); Serial.print("\t");
Serial.print("Z="); Serial.println(gz);
Serial.println("Accelerometer (g):");
Serial.print("X="); Serial.print(ax); Serial.print("\t");
Serial.print("Y="); Serial.print(ay); Serial.print("\t");
Serial.print("Z="); Serial.println(az);
delay(1000); // Delay to avoid too rapid data readings
}