-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoystick.ino
51 lines (43 loc) · 1.18 KB
/
joystick.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
/**
* Update the joystick values
*/
bool updateJoystick() {
int newJoyX = round(analogRead(PIN_JOY_X) / 4 - 128);
int newJoyY = round(analogRead(PIN_JOY_Y) / 4 - 128);
#ifdef INVERT_JOYSTICK_X
newJoyX = newJoyX * -1;
#endif
#ifdef INVERT_JOYSTICK_Y
newJoyY = newJoyY * -1;
#endif
// Compensate for the dead zone;
if ((newJoyX < JOYSTICK_DEAD_ZONE) && (-JOYSTICK_DEAD_ZONE < newJoyX)) {
newJoyX = 0;
}
if ((newJoyY < JOYSTICK_DEAD_ZONE) && (-JOYSTICK_DEAD_ZONE < newJoyY)) {
newJoyY = 0;
}
// Account for the dead zone
if (newJoyX < 0) {
newJoyX = map(newJoyX, -JOYSTICK_DEAD_ZONE, -127, -1, -255);
} else if (newJoyX > 0) {
newJoyX = map(newJoyX, JOYSTICK_DEAD_ZONE, 127, 1, 255);
}
if (newJoyY < 0) {
newJoyY = map(newJoyY, -JOYSTICK_DEAD_ZONE, -127, -1, -255);
} else if (newJoyY > 0) {
newJoyY = map(newJoyY, JOYSTICK_DEAD_ZONE, 127, 1, 255);
}
newJoyX = constrain(map(newJoyX, -255, 255, 0, 255), 0, 255);
newJoyY = constrain(map(newJoyY, -255, 255, 0, 255), 0, 255);
// Any Changes?
if (
(newJoyX != joyX) ||
(newJoyY != joyY)
) {
joyX = newJoyX;
joyY = newJoyY;
return true;
}
return false;
}