#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include <Stepper.h>
const int stepsPerRevolution = 32;
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
struct MyData {
byte X;
byte Y;
};
MyData data;
Stepper ayStepper(32, 8, 10, 9, 11);
int stepCount = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
pinMode(13, OUTPUT);
pinMode(A0, INPUT);
}
void loop(){
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
data.X = map(ax, -17000, 17000, 0, 255);
data.Y = map(ay, -17000, 17000, 0, 255);
Serial.print(" Axis: X = ");
Serial.print(data.X);
Serial.print(" Axis: Y = ");
Serial.print(data.Y);
if (data.Y < 110){
delay(500);
ayStepper.setSpeed(900);
ayStepper.step(300);
delay(100);
}
if (data.Y > 135){
delay(500);
ayStepper.setSpeed(900);
ayStepper.step(-300);
delay(100);
tone(2, 100, 500);
}
}