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
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTEr. Ganesh Ram Suwal
 
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
 

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
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LIST
 
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
 

Último

Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
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
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 

Último (20)

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
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
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
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 

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