SlideShare una empresa de Scribd logo
1 de 3
Descargar para leer sin conexión
#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

Más contenido relacionado

La actualidad más candente

Basic Static Route Configuration
Basic Static Route ConfigurationBasic Static Route Configuration
Basic Static Route Configurationcstony_dreamer
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話tatsunori ishikawa
 
20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会Hiroki Mizuno
 
Swift girls 5th_teaching_meeting20160829
Swift girls 5th_teaching_meeting20160829Swift girls 5th_teaching_meeting20160829
Swift girls 5th_teaching_meeting20160829雅方 鄭
 
Vhdl practical exam guide
Vhdl practical exam guideVhdl practical exam guide
Vhdl practical exam guideEslam Mohammed
 

La actualidad más candente (7)

Basic Static Route Configuration
Basic Static Route ConfigurationBasic Static Route Configuration
Basic Static Route Configuration
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話
 
20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会20110424 action scriptを使わないflash勉強会
20110424 action scriptを使わないflash勉強会
 
Swift girls 5th_teaching_meeting20160829
Swift girls 5th_teaching_meeting20160829Swift girls 5th_teaching_meeting20160829
Swift girls 5th_teaching_meeting20160829
 
Vhdl practical exam guide
Vhdl practical exam guideVhdl practical exam guide
Vhdl practical exam guide
 
Michael kontopoulo1s
Michael kontopoulo1sMichael kontopoulo1s
Michael kontopoulo1s
 
Metal detecting robot sketch
Metal detecting robot sketchMetal detecting robot sketch
Metal detecting robot sketch
 

Similar a Gyro mouse

Laboratory Report Sample
Laboratory Report SampleLaboratory Report Sample
Laboratory Report SampleMarkus Flicke
 
Loopback.vhd
Loopback.vhdLoopback.vhd
Loopback.vhdsachindb9
 
망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15종인 전
 
create_workbook_store.sql-- create_store.sql-- Introcution,.docx
create_workbook_store.sql-- create_store.sql-- Introcution,.docxcreate_workbook_store.sql-- create_store.sql-- Introcution,.docx
create_workbook_store.sql-- create_store.sql-- Introcution,.docxfaithxdunce63732
 
code-nodemcu.docx
code-nodemcu.docxcode-nodemcu.docx
code-nodemcu.docxLTinTun
 
Sybase to oracle_conversion
Sybase to oracle_conversionSybase to oracle_conversion
Sybase to oracle_conversionSam Varadarajan
 
Ramco C Question Paper 2003
Ramco  C  Question  Paper 2003Ramco  C  Question  Paper 2003
Ramco C Question Paper 2003ncct
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScriptJens Siebert
 
What will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfWhat will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfSIGMATAX1
 
zen and the art of SQL optimization
zen and the art of SQL optimizationzen and the art of SQL optimization
zen and the art of SQL optimizationKaren Morton
 

Similar a Gyro mouse (20)

Movemaster robot
Movemaster robotMovemaster robot
Movemaster robot
 
Laboratory Report Sample
Laboratory Report SampleLaboratory Report Sample
Laboratory Report Sample
 
Loopback.vhd
Loopback.vhdLoopback.vhd
Loopback.vhd
 
망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15
 
create_workbook_store.sql-- create_store.sql-- Introcution,.docx
create_workbook_store.sql-- create_store.sql-- Introcution,.docxcreate_workbook_store.sql-- create_store.sql-- Introcution,.docx
create_workbook_store.sql-- create_store.sql-- Introcution,.docx
 
Q 1
Q 1Q 1
Q 1
 
SOM
SOMSOM
SOM
 
code-nodemcu.docx
code-nodemcu.docxcode-nodemcu.docx
code-nodemcu.docx
 
Gps c
Gps cGps c
Gps c
 
Dcn code and output
Dcn code and outputDcn code and output
Dcn code and output
 
FINISHED_CODE
FINISHED_CODEFINISHED_CODE
FINISHED_CODE
 
Sybase to oracle_conversion
Sybase to oracle_conversionSybase to oracle_conversion
Sybase to oracle_conversion
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Mona cheatsheet
Mona cheatsheetMona cheatsheet
Mona cheatsheet
 
Diseqc
DiseqcDiseqc
Diseqc
 
Ramco C Question Paper 2003
Ramco  C  Question  Paper 2003Ramco  C  Question  Paper 2003
Ramco C Question Paper 2003
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
 
What will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdfWhat will be quantization step size in numbers and in voltage for th.pdf
What will be quantization step size in numbers and in voltage for th.pdf
 
zen and the art of SQL optimization
zen and the art of SQL optimizationzen and the art of SQL optimization
zen and the art of SQL optimization
 

Último

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 

Último (20)

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 

Gyro mouse

  • 1. #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
  • 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(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