-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystickHandler.h
86 lines (80 loc) · 2.07 KB
/
joystickHandler.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
// Pins
const int VRx = A2;
const int VRy = A3;
const int SW = 2;
// Joystick Variables
int xPosition = 0;
int yPosition = 0;
int mapX = 0;
int mapY = 0;
// Misc Variables
int interval = 200;
int lastTime;
int pinSelected = 0;
int pageNumber = 0;
const int maxPageNumber = 2;
void initJoystick() {
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP);
}
void detectJoystick() {
int currentTime = millis();
if ((currentTime - lastTime) >= interval) {
lastTime = currentTime;
xPosition = analogRead(VRx);
yPosition = analogRead(VRy);
mapX = map(xPosition, 0, 1023, -512, 512);
mapY = map(yPosition, 0, 1023, -512, 512);
// Check what page to switch the three songs.
if (mapY >= 500) {
++pageNumber;
Serial.print(pageNumber);
Serial.print(F("- Page Increasing!"));
Serial.println();
}
else if (mapY <= -500) {
--pageNumber;
Serial.print(pageNumber);
Serial.print(F("- Page Decreasing!"));
Serial.println();
}
if (pageNumber > maxPageNumber) {
pageNumber = 0;
Serial.print(pageNumber);
Serial.print(F("- Page Resetting!"));
Serial.println();
}
else if (pageNumber < 0) {
pageNumber = maxPageNumber;
Serial.print(pageNumber);
Serial.print(F("- Page Resetting!"));
Serial.println();
}
// Checks what pin is being hovered over.
if (mapX <= -500) {
++pinSelected;
Serial.print(pinSelected + (pageNumber * 3));
Serial.print(F("- Pin Increasing!"));
Serial.println();
}
else if (mapX >= 500) {
--pinSelected;
Serial.print(pinSelected + (pageNumber * 3));
Serial.print(F("- Pin Decreasing!"));
Serial.println();
}
if (pinSelected > 2) {
pinSelected = 0;
Serial.print(pinSelected + (pageNumber * 3));
Serial.print(F("- Pin Resetting!"));
Serial.println();
}
else if (pinSelected < 0) {
pinSelected = 2;
Serial.print(pinSelected + (pageNumber * 3));
Serial.print(F("- Pin Resetting!"));
Serial.println();
}
}
}