#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>
#include <AccelStepper.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
volatile uint8_t data[14]; //6050センサからのデータ格納用配列
volatile int16_t ax = 0; //6050出力データ(生値)
Servo myservo;
int const Servo_PIN = 6;
int ANGLE;
int const DIR = 0;
int const STEP = 1;
AccelStepper stepper(1,STEP,DIR);
void setup() {
// put your setup code here, to run once:
Wire.begin();
lcd.init();
lcd.backlight();
lcd.clear();
Wire.beginTransmission(0x68);//送信処理を開始する(0x68がセンサーのアドレス)
Wire.write(0x6b); //レジスタ「0x6b」(動作状変数)を指定
Wire.write(0x00); //0x00を指定(ON)
Wire.endTransmission(); //送信を終了する
myservo.attach(Servo_PIN,500,2400);
pinMode(STEP,OUTPUT);
pinMode(DIR,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
MPU_DATAGET();
lcd.setCursor(0, 0);
lcd.print("ax:");
lcd.setCursor(4, 0);
lcd.print(ax);
ANGLE = map(ax, -32768,32768,0,180);
myservo.write(ANGLE);
stepper.setSpeed(ANGLE);
delay(100);
}
void MPU_DATAGET() {
Wire.beginTransmission(0x68); //送信処理を開始する
Wire.write(0x3b); //(取得値の先頭を指定)
Wire.endTransmission(); //送信を終了する
Wire.requestFrom(0x68, 14); //データを要求する(0x3bから14バイトが6軸の値)
uint8_t i = 0;
while (Wire.available()) {
data[i++] = Wire.read();//データを読み込む
}
ax = (data[0] << 8) | data[1];//LowとHighを連結して、値を取得する
}