-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrack_mouse_esp.ino
201 lines (163 loc) · 5.52 KB
/
Crack_mouse_esp.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <BleCombo.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
const int l = 33;
const int r = 33;
const int b2 = 13;
const int b1 = 15;
const int fn = 12;
const int b3 = 27;
const int b4 = 14;
int mouseClickFlag = 0;
long lastDebounceTime1 = 0; // the last time the output pin was toggled
long lastDebounceTime2 = 0;
long lastDebounceTime3 = 0;
long lastDebounceTime4 = 0;
long lastDebounceTimel = 0;
long lastDebounceTimer = 0;
long lastDebounceTimefn = 0;
long debounceDelay = 500; // the debounce time; increase if the output flickers
//Set the old button state to be LOW/false; which means not pressed
boolean oldPinState1 = LOW;
boolean oldPinState2 = LOW;
boolean oldPinState3 = LOW;
boolean oldPinState4 = LOW;
boolean oldPinStatel = LOW;
boolean oldPinStater = LOW;
boolean oldPinStatefn = LOW;
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;
void setup() {
Serial.begin(115200);
Serial.println(" Crack_mouse Starting work!");
Keyboard.begin();
Mouse.begin();
Wire.begin();
mpu.initialize();
if (!mpu.testConnection()) { while (1); }
pinMode(l, INPUT_PULLDOWN);
pinMode(r, INPUT_PULLDOWN);
pinMode(b2, INPUT_PULLDOWN);
pinMode(b1, INPUT_PULLDOWN);
pinMode(fn, INPUT_PULLDOWN);
pinMode(b3, INPUT_PULLDOWN);
pinMode(b4, INPUT_PULLDOWN);
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
vx = (gx+100)/150; // change 300 from 0 to 355
vy = -(gz-100)/150; // same here about "-100" from -355 to 0
Serial.print("gx = ");
Serial.print(gx);
Serial.print(" | gz = ");
Serial.print(gz);
Serial.print(" | X = ");
Serial.print(vx);
Serial.print(" | Y = ");
Serial.println(vy);
Mouse.move(vx, vy);
delay(20);
if(Keyboard.isConnected()) {
if (digitalRead(b2) == HIGH) {
//If the button pin is pressed (since it is pulled down, it is pressed when it is high
if ( (millis() - lastDebounceTime2) > debounceDelay) {
if (oldPinState2 == LOW) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('a');
lastDebounceTime2 = millis(); //set the current time;
Keyboard.release(KEY_LEFT_CTRL);
}
oldPinState2 = HIGH;
}
}
else {
oldPinState2 = LOW;
}
if (digitalRead(b1) == HIGH) {
//If the button pin is pressed (since it is pulled down, it is pressed when it is high
if ( (millis() - lastDebounceTime1) > debounceDelay) {
if (oldPinState1 == LOW) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('c');
lastDebounceTime1 = millis(); //set the current time;
Keyboard.release(KEY_LEFT_CTRL);
}
oldPinState1 = HIGH;
}
}
else {
oldPinState1 = LOW;
}
if (digitalRead(b3) == HIGH) {
//If the button pin is pressed (since it is pulled down, it is pressed when it is high
if ( (millis() - lastDebounceTime3) > debounceDelay) {
if (oldPinState3 == LOW) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('s');
lastDebounceTime3 = millis(); //set the current time;
Keyboard.release(KEY_LEFT_CTRL);
}
oldPinState3 = HIGH;
}
}
else {
oldPinState3 = LOW;
}
if (digitalRead(b4) == HIGH) {
//If the button pin is pressed (since it is pulled down, it is pressed when it is high
if ( (millis() - lastDebounceTime4) > debounceDelay) {
if (oldPinState4 == LOW) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('z');
lastDebounceTime4 = millis(); //set the current time;
Keyboard.release(KEY_LEFT_CTRL);
}
oldPinState4 = HIGH;
}
}
else {
oldPinState4 = LOW;
}
if ((digitalRead(fn) == HIGH) && (digitalRead(b1)== HIGH)) {
if ( (millis() - lastDebounceTimefn) > debounceDelay) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('v');
//lastDebounceTimefn = millis();
lastDebounceTimefn = millis();
}
Keyboard.release(KEY_LEFT_CTRL);
}
if ((digitalRead(fn) == HIGH) && (digitalRead(b4)== HIGH)) {
if ( (millis() - lastDebounceTimefn) > debounceDelay) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.write('y');
lastDebounceTimefn = millis();
}
Keyboard.release(KEY_LEFT_CTRL);
}
if ((digitalRead(fn) == HIGH) && (digitalRead(b3)== HIGH)) {
//If the button pin is pressed (since it is pulled down, it is pressed when it is high
if ( (millis() - lastDebounceTimefn) > debounceDelay) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_RIGHT_SHIFT);
Keyboard.write('s');
lastDebounceTimefn = millis();
}
Keyboard.releaseAll();
}
if (digitalRead(l) == HIGH) { // if the joystick button is pressed
Mouse.press(MOUSE_LEFT); // click the left button down
}
else if (digitalRead(l) == LOW ){ // if the joystick button is not pressed
Mouse.release(MOUSE_LEFT); // release the left button
}
if (digitalRead(r) == HIGH) { // if the joystick button is pressed
Mouse.press(MOUSE_RIGHT); // click the left button down
}
else if (digitalRead(l) == LOW ){ // if the joystick button is not pressed
Mouse.release(MOUSE_RIGHT); // release the left button
}
}
}