#include <Wire.h>
#define MPU6050_ACCEL_XOUT_H 0x3B // R
#define MPU6050_WHO_AM_I 0x75 // R
#define MPU6050_PWR_MGMT_1 0x6B // R/W
#define MPU6050_I2C_ADDRESS 0x68
//---------ボタンの設定-------------------------------------------------------
const int buttonPin1 = 11;//11番ピンにボタンの配線を付ける
//------MPU6050から読み込む変数(元からの流用)------------------------------
typedef union accel_t_gyro_union{
struct{
uint8_t x_accel_h;
uint8_t x_accel_l;
uint8_t y_accel_h;
uint8_t y_accel_l;
uint8_t z_accel_h;
uint8_t z_accel_l;
uint8_t t_h;
uint8_t t_l;
uint8_t x_gyro_h;
uint8_t x_gyro_l;
uint8_t y_gyro_h;
uint8_t y_gyro_l;
uint8_t z_gyro_h;
uint8_t z_gyro_l;
}
reg;
struct{
int16_t x_accel;
int16_t y_accel;
int16_t z_accel;
int16_t temperature;
int16_t x_gyro;
int16_t y_gyro;
int16_t z_gyro;
}
value;
};
// MPU control/status vars(元からの流用)----------------------------------------
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
int error;
accel_t_gyro_union accel_t_gyro;
float gyro_x,gyro_y,gyro_z;
float accel_X_cal[4]={1,1,1};
float accel_Y_cal[4]={1,1,1};
float accel_Z_cal[4]={1,1,1};
//------------------MPU6050からジャイロの値を読み込む----------------------
1
void Read_deg(){
error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro));uint8_t swap;
#define SWAP(x,y) swap = x; x = y; y = swap
SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l);
SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l);
SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l);
SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l);
SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l);
SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l);
SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l);
//-----読み取ったジャイロの値を換算する(元からの流用)--------------------
gyro_x = accel_t_gyro.value.x_gyro / 131.0; //FS_SEL_0 131 LSB / (°/s)
gyro_y = accel_t_gyro.value.y_gyro / 131.0;
gyro_z = accel_t_gyro.value.z_gyro / 131.0;
//----ジャイロの値を表示--------------------------------------------------
Serial.print("Degree_sp ");
Serial.print("X':");
Serial.print(gyro_x, 2);
Serial.print("t");
Serial.print(",Y':");
Serial.print(gyro_y, 2);
Serial.print(" ,Z':");
Serial.print(gyro_z, 2);
Serial.println("t");
Serial.println("");
}
void setup(){
Wire.begin();
Mouse.begin();
int error;
uint8_t c;
//INPUT_PULLUPを使い、aruduinoからボタンに直接配線できるようにする
pinMode(buttonPin1, INPUT_PULLUP);
Serial.begin(19200);
error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1);
error = MPU6050_read (MPU6050_PWR_MGMT_1, &c, 1);
MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0);
Serial.println("I'm ready!");
delay(1000);
}
void loop(){
//------------ボタンがクリックされたらマウスをクリックとマウス移動させる---------------
if(digitalRead(buttonPin1)!=HIGH){
Mouse.press(MOUSE_LEFT);
Read_deg();
//ジャイロ値によるマウスの移動  ↓ここの-1は持った際の自重(重力)を考慮している 
Mouse.move(gyro_y*-.2,gyro_x*-.2-1,0);
}
//--------------クリックがない場合はマウスの移動のみ-------------------------
else{
Mouse.release(MOUSE_LEFT);
Read_deg();
Mouse.move(gyro_y*-.2,gyro_x*-.2-1,0);
2
}
//------ディレイを一応2ms入れてみる------------------------------------------
delay(2);
}
//-------- MPU6050_read(元からの流用)-------------------------------------
int MPU6050_read(int start, uint8_t *buffer, int size){
int i, n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start);
if (n != 1)
return (-10);
n = Wire.endTransmission(false); // hold the I2C-bus
if (n != 0)
return (n);
// Third parameter is true: relase I2C-bus after data is read.
Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
i = 0;
while(Wire.available() && i<size){
buffer[i++]=Wire.read();
}
if ( i != size)
return (-11);
return (0); // return : no error
}
//---------------- MPU6050_write(元からの流用)-------------------------
int MPU6050_write(int start, const uint8_t *pData, int size){
int n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start); // write the start address
if (n != 1)
return (-20);
n = Wire.write(pData, size); // write data bytes
if (n != size)
return (-21);
error = Wire.endTransmission(true); // release the I2C-bus
if (error != 0)
return (error);
return (0); // return : no error
}
// MPU6050_write_reg
int MPU6050_write_reg(int reg, uint8_t data){
int error;
error = MPU6050_write(reg, &data, 1);
return (error);
}
3

Gyro mouse

  • 1.
    #include <Wire.h> #define MPU6050_ACCEL_XOUT_H0x3B // R #define MPU6050_WHO_AM_I 0x75 // R #define MPU6050_PWR_MGMT_1 0x6B // R/W #define MPU6050_I2C_ADDRESS 0x68 //---------ボタンの設定------------------------------------------------------- const int buttonPin1 = 11;//11番ピンにボタンの配線を付ける //------MPU6050から読み込む変数(元からの流用)------------------------------ typedef union accel_t_gyro_union{ struct{ uint8_t x_accel_h; uint8_t x_accel_l; uint8_t y_accel_h; uint8_t y_accel_l; uint8_t z_accel_h; uint8_t z_accel_l; uint8_t t_h; uint8_t t_l; uint8_t x_gyro_h; uint8_t x_gyro_l; uint8_t y_gyro_h; uint8_t y_gyro_l; uint8_t z_gyro_h; uint8_t z_gyro_l; } reg; struct{ int16_t x_accel; int16_t y_accel; int16_t z_accel; int16_t temperature; int16_t x_gyro; int16_t y_gyro; int16_t z_gyro; } value; }; // MPU control/status vars(元からの流用)---------------------------------------- bool dmpReady = false; // set true if DMP init was successful uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) uint16_t packetSize; // expected DMP packet size (default is 42 bytes) uint16_t fifoCount; // count of all bytes currently in FIFO uint8_t fifoBuffer[64]; // FIFO storage buffer int error; accel_t_gyro_union accel_t_gyro; float gyro_x,gyro_y,gyro_z; float accel_X_cal[4]={1,1,1}; float accel_Y_cal[4]={1,1,1}; float accel_Z_cal[4]={1,1,1}; //------------------MPU6050からジャイロの値を読み込む---------------------- 1
  • 2.
    void Read_deg(){ error =MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro));uint8_t swap; #define SWAP(x,y) swap = x; x = y; y = swap SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l); SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l); SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l); SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l); SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l); SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l); SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l); //-----読み取ったジャイロの値を換算する(元からの流用)-------------------- gyro_x = accel_t_gyro.value.x_gyro / 131.0; //FS_SEL_0 131 LSB / (°/s) gyro_y = accel_t_gyro.value.y_gyro / 131.0; gyro_z = accel_t_gyro.value.z_gyro / 131.0; //----ジャイロの値を表示-------------------------------------------------- Serial.print("Degree_sp "); Serial.print("X':"); Serial.print(gyro_x, 2); Serial.print("t"); Serial.print(",Y':"); Serial.print(gyro_y, 2); Serial.print(" ,Z':"); Serial.print(gyro_z, 2); Serial.println("t"); Serial.println(""); } void setup(){ Wire.begin(); Mouse.begin(); int error; uint8_t c; //INPUT_PULLUPを使い、aruduinoからボタンに直接配線できるようにする pinMode(buttonPin1, INPUT_PULLUP); Serial.begin(19200); error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1); error = MPU6050_read (MPU6050_PWR_MGMT_1, &c, 1); MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0); Serial.println("I'm ready!"); delay(1000); } void loop(){ //------------ボタンがクリックされたらマウスをクリックとマウス移動させる--------------- if(digitalRead(buttonPin1)!=HIGH){ Mouse.press(MOUSE_LEFT); Read_deg(); //ジャイロ値によるマウスの移動  ↓ここの-1は持った際の自重(重力)を考慮している  Mouse.move(gyro_y*-.2,gyro_x*-.2-1,0); } //--------------クリックがない場合はマウスの移動のみ------------------------- else{ Mouse.release(MOUSE_LEFT); Read_deg(); Mouse.move(gyro_y*-.2,gyro_x*-.2-1,0); 2
  • 3.
    } //------ディレイを一応2ms入れてみる------------------------------------------ delay(2); } //-------- MPU6050_read(元からの流用)------------------------------------- int MPU6050_read(intstart, uint8_t *buffer, int size){ int i, n, error; Wire.beginTransmission(MPU6050_I2C_ADDRESS); n = Wire.write(start); if (n != 1) return (-10); n = Wire.endTransmission(false); // hold the I2C-bus if (n != 0) return (n); // Third parameter is true: relase I2C-bus after data is read. Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true); i = 0; while(Wire.available() && i<size){ buffer[i++]=Wire.read(); } if ( i != size) return (-11); return (0); // return : no error } //---------------- MPU6050_write(元からの流用)------------------------- int MPU6050_write(int start, const uint8_t *pData, int size){ int n, error; Wire.beginTransmission(MPU6050_I2C_ADDRESS); n = Wire.write(start); // write the start address if (n != 1) return (-20); n = Wire.write(pData, size); // write data bytes if (n != size) return (-21); error = Wire.endTransmission(true); // release the I2C-bus if (error != 0) return (error); return (0); // return : no error } // MPU6050_write_reg int MPU6050_write_reg(int reg, uint8_t data){ int error; error = MPU6050_write(reg, &data, 1); return (error); } 3