-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGyro_Testing.ino
More file actions
123 lines (104 loc) · 3.71 KB
/
Copy pathGyro_Testing.ino
File metadata and controls
123 lines (104 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <Wire.h> //library allows communication with I2C / TWI devices
#include <math.h> //library includes mathematical functions
#include <Mouse.h>
const int MPU=0x68; //I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; //16-bit integers
int AcXcal,AcYcal,AcZcal,GyXcal,GyYcal,GyZcal,tcal; //calibration variables
double t,tx,tf,pitch,roll;
void setup()
{
Wire.begin(); //initiate wire library and I2C
Wire.beginTransmission(MPU); //begin transmission to I2C slave device
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true); //ends transmission to I2C slavef device
Serial.begin(9600); //serial communication at 9600 bauds
Mouse.begin();
}
void loop()
{
Wire.beginTransmission(MPU); //begin transmission to I2C slave device
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false); //restarts transmission to I2C slave device
Wire.requestFrom(MPU,14,true); //request 14 registers in total
//Acceleration data correction
AcXcal = -950;
AcYcal = -300;
AcZcal = 0;
//Temperature correction
tcal = -1600;
//Gyro correction
GyXcal = 480;
GyYcal = 170;
GyZcal = 210;
//read accelerometer data
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) 0x40 (ACCEL_ZOUT_L)
//read temperature data
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) 0x42 (TEMP_OUT_L)
//read gyroscope data
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) 0x48 (GYRO_ZOUT_L)
//temperature calculation
tx = Tmp + tcal;
t = tx/340 + 36.53; //equation for temperature in degrees C from datasheet
tf = (t * 9/5) + 32; //fahrenheit
//get pitch/roll
getAngle(AcX,AcY,AcZ);
//printing values to serial port
Serial.print("Angle: ");
Serial.print("Pitch = "); Serial.print(pitch + 6);
Serial.print(" Roll = "); Serial.println(roll);
//if mouse is tilted to the back-left, perform left down press
if(roll > 16 && pitch < -13){
//Dragging Feature
Serial.println("Diagonal Click");
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
Serial.println("Diagonal Press");
delay(750);
}else{
Mouse.release(MOUSE_LEFT);
Serial.println("Diagonal Release");
delay(750);
}
}
else if(roll > 18){
// if the mouse is not pressed, press it:
Mouse.press(MOUSE_LEFT);
Serial.println("Clicking Left");
delay(300);
Mouse.release(MOUSE_LEFT);
}
else if(roll < -13){
Mouse.press(MOUSE_RIGHT);
Serial.println("Clicking Right");
delay(300);
Mouse.release(MOUSE_RIGHT);
}
else if(pitch +6 > 20) {
Mouse.move(0, 0, 1);
Serial.println("Scrolling Up");
delay(100);
}
else if(pitch +6 < -30) {
Mouse.move(0, 0, -1);
Serial.println("Scrolling Down");
delay(100);
}
delay(10);
}
//function to convert accelerometer values into pitch and roll
void getAngle(int Ax,int Ay,int Az)
{
double x = Ax;
double y = Ay;
double z = Az;
pitch = atan(x/sqrt((y*y) + (z*z))); //pitch calculation
roll = atan(y/sqrt((x*x) + (z*z))); //roll calculation
//converting radians into degrees
pitch = pitch * (180.0/3.14);
roll = roll * (180.0/3.14) ;
}