/
Joystick.h
177 lines (159 loc) · 4.94 KB
/
Joystick.h
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
#ifndef Joystick_H
#define Joystick_H
#include <Arduino.h>
class Joystick
{
public:
int xPin;
int yPin;
int button;
bool debugIt = false;
String newX;
String newY;
String newZ;
String bothReleased = "";
int analogX = 0;
int analogY = 0;
Joystick (int xAnalogPin, int yAnalogPin, int buttonPin ) {
xPin = xAnalogPin;
yPin = yAnalogPin;
button = buttonPin;
timeout = 1;
pinMode (button,INPUT);
digitalWrite (button,1); // Set the pull-up resistor
}
void update() {
int count = 0;
lastX = xDirection;
lastY = yDirection;
lastZ = zButton;
lastReleased = released;
newX = "";
newY = "";
newZ = "";
bothReleased = "";
if ((millis() > timeout) && (timeout != 0)) { // Don't call too often
timeout = millis() + 100;
// Read the nunchuk values
analogX = analogRead (xPin);
analogY = analogRead (yPin);
zButton = !digitalRead (button);
xDirection = 0; // Initialized to release
if (analogX < 200) {
xDirection = 1; // Left
} else if (analogX > 800) {
xDirection = 2; // Right
}
if (debugIt) {
Serial.print ( "[x,y]: " );
Serial.print ( "[" );
Serial.print ( xPin );
Serial.print ( "," );
Serial.print ( yPin );
Serial.print ( "] [" );
Serial.print ( analogX );
Serial.print ( "," );
Serial.print ( analogY );
Serial.println ( "]" );
}
yDirection = 0; // Initialized to release
if (analogY < 200) {
yDirection = 2; // Down
} else if (analogY > 800) {
yDirection = 1; // Up
}
newX = "";
if (lastX != xDirection) {
xReleased = false;
if (xDirection == 0) {
newX = "RELEASED";
xReleased = true;
} else if (xDirection == 1) {
newX = "LEFT";
} else if (xDirection == 2) {
newX = "RIGHT";
}
if (debugIt) {
Serial.print ( "X Direction changed to \"" );
Serial.print ( newX );
Serial.println ( "\"" );
}
}
newY = "";
if (lastY != yDirection) {
yReleased = false;
if (yDirection == 0) {
yReleased = true;
newY = "RELEASED";
} else if (yDirection == 1) {
newY = "DOWN";
} else if (yDirection == 2) {
newY = "UP";
}
if (debugIt) {
Serial.print ( "Y Direction changed to \"" );
Serial.print ( newY );
Serial.println ( "\"" );
}
}
newZ = "";
if (lastZ != zButton) {
if (zButton == 1) {
newZ = "PRESSED";
} else {
newZ = "RELEASED";
}
if (debugIt) {
Serial.print ( "Z button changed to \"" );
Serial.print ( newZ );
Serial.println ( "\"" );
}
}
released = false;
if (xReleased && yReleased) {
released = true;
}
bothReleased = "";
if (released != lastReleased) {
if (released) {
bothReleased = "YES";
} else {
bothReleased = "NO";
}
if (debugIt) {
if (released) {
Serial.println ( "X & Y released" );
} else {
Serial.println ( "X | Y not released" );
}
}
}
}
}
void showData() {
Serial.print ( "[x,y,z]: [" );
Serial.print ( analogX );
Serial.print ( "," );
Serial.print ( analogY );
Serial.print ( "," );
Serial.print ( zButton );
Serial.print ( "], [xDirection, yDirection]: [" );
Serial.print ( xDirection );
Serial.print ("," );
Serial.print ( yDirection );
Serial.println ( "]" );
}
private:
unsigned long timeout = 0;
int lastZ = 0; // 0 = Released, 1 = Pressed
int zButton = 0; // 0 = Released, 1 = Pressed
int xDirection = 0; // 0 = Released, 1 = Left, 2 = Right
int lastX = 0; // 0 = Released, 1 = Left, 2 = Right
int yDirection = 0; // 0 = Released, 1 = Left, 2 = Right
int lastY = 0; // 0 = Released, 1 = Up, 2 = Right
int lastReleased = 0; // 0 = Not Released, 1 = Released
int released = 0;
bool xReleased = true;
bool yReleased = true;
};
#endif