You are on page 1of 1

#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 myservo1;
Servo myservo2;
Servo myservo3;

int val;
int prevVal;

void setup()
{
Wire.begin();
Serial.begin(38400);

Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
myservo1.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}

void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

val = map(ay, -17000, 17000, 0, 179);


if (val != prevVal)
{
myservo1.write(val);
prevVal = val;
}

val = map(ax, -17000, 17000, 0, 179);


if (val != prevVal)
{
myservo2.write(val);
prevVal = val;
}

val = map(az, -17000, 17000, 0, 179);


if (val != prevVal)
{
myservo3.write(val);
prevVal = val;
}

delay(50);
}

You might also like