-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU_6050_mouse.ino
141 lines (119 loc) · 3.13 KB
/
MPU_6050_mouse.ino
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy,clik=0,clik1=0,clik2=0;/*, vxx,vy;*/
const int buttonPin = 5; // the number of the pushbutton pin
const int button1Pin=6;
const int button2Pin=7;
//const int ledPin = 13; // the number of the LED pin
int buttonState = 0;
int button1State=0;
int button2State=0;
unsigned long recoilTime = 0; // the last time the output pin was toggled
unsigned long recoilPulse = 100; // the debounce time; increase if the output flickers
void setup() {
//delay(5000);
//flush
Serial.begin(38400);
Wire.begin();
mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
pinMode(buttonPin, INPUT);
pinMode(button1Pin,INPUT);
pinMode(button2Pin,INPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
digitalWrite(8,HIGH);
digitalWrite(10,LOW);
vx=0;
vy=0;
mpu.initialize();
while (!mpu.testConnection()) {
Serial.print(2);
}
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
/*vxx=vx-gx-310;
vyy=vy-gz+26;*/
vx =-gx-310; // "-310" because the x axis of gyroscope give values about +310 while it's not moving. Change this value if you get something different using the TEST code, chacking if there are values far from zero.
vy =-gz+26;// same here about "+26"
/* if(vxx<100&&vxx>-100)
{ vx=0;
}
if(vyy<100&&vyy>-100)
{vy=0;}*/
Serial.print(vx/800);
Serial.print(",");
Serial.print(vy/500);
Serial.print(",");
/*
Serial.print(ax);
Serial.print(" ");
Serial.print(ay);
Serial.print(" ");
Serial.print(az);
Serial.print(" ");
Serial.print(gx);
Serial.print(" ");
Serial.print(gy);
Serial.print(" ");
Serial.print(gz);
Serial.print(" ");
Serial.print(vx);
Serial.print(" ");
Serial.println(vy);
*/
buttonState = digitalRead(buttonPin);
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
// if (millis()-cliktime>debound||(clik==0&&buttonState==LOW))
//{
// cliktime=millis();
// LEFT CLICK
if (clik==0&&buttonState == LOW) {
clik=1;
// turn LED on:
Serial.print("1,");
digitalWrite(9,HIGH);
recoilTime=millis();
}
else if (clik==1&&buttonState==HIGH){
clik=0;
// turn LED off:
Serial.print("0,");
}
else {Serial.print("2,");}
if ((millis()-recoilTime) > recoilPulse){
digitalWrite(9,LOW);
}
// RIGHT CLICK
if (clik1==0&&button1State == LOW) {
clik1=1;
// turn LED on:
Serial.print("1,");
}
else if (clik1==1&&button1State==HIGH){
clik1=0;
// turn LED off:
Serial.print("0,");
}
else {Serial.print("2,");}
// Reload button
if (clik2==0&&button2State ==LOW) {
clik2=1;
// turn LED on:
Serial.println("1");
}
else if (clik2==1&&button2State==HIGH){
clik2=0;
Serial.println("0");
}
else {Serial.println("2");}
}
//delay(0);