I found a code from Astronomica Messier that allows to activate a servo from a Gyroscope MPU 6050 change in direction. I followed the wiring and uploading correctly. The thing is that the MPU is initializing but the connection is failing. Could you help me please to figure out whats happening here? I'm using a GY-86, an Arduino Nano and a Micro Servo 9g. Here's the code. Thank you.
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
Servo servo1;
int val1;
int prevVal1;
void setup()
{
Wire.begin();
Serial.begin(38400);
Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" :
"Connection failed");
servo1.attach(9);
}
void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
val1 = map(ax, -17000, 17000, 120, 10);
if (val1 != prevVal1)
{
servo1.write(val1);
prevVal1 = val1;
}
delay(50);
}