-
Notifications
You must be signed in to change notification settings - Fork 0
/
Joystick.ino
92 lines (87 loc) · 2.92 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
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
#include <Joystick.h>
Joystick_ Joystick;
void setup(){
// Initialize Button Pins
pinMode(3, INPUT_PULLUP);// SELECT / SHARE
pinMode(4, INPUT_PULLUP);// START / OPTIONS
pinMode(5, INPUT_PULLUP);// up
pinMode(6, INPUT_PULLUP);// left
pinMode(7, INPUT_PULLUP);// down
pinMode(8, INPUT_PULLUP);// right
pinMode(9, INPUT_PULLUP);// 1 / triangle
pinMode(10, INPUT_PULLUP);// 4 / rectangle
pinMode(11, INPUT_PULLUP);// 2 / circle
pinMode(12, INPUT_PULLUP);// 3 / cross
pinMode(13, INPUT_PULLUP);// R1
pinMode(14, INPUT_PULLUP);// R2
pinMode(15, INPUT_PULLUP);// L1
pinMode(16, INPUT_PULLUP);// L2
// Initialize Joystick Library
Joystick.begin();
}
int buttonToPinMap[16] = {
12,11,10,9,// right button panel
5,7,6,8,// left button panel
15,13,16,14,// upper button panel
3,4,// special button panel
1,0// joystick button panel
};
int lastButtonState[16] = {
0,0,0,0,// right button panel
0,0,0,0,// left button panel
0,0,0,0,// upper button panel
0,0,// special button
0,0// joystick button panel
};
void loop(){
// right button panel
for (int index = 0; index < 4; index++){
int currentButtonState = !digitalRead(buttonToPinMap[index]);
if (currentButtonState != lastButtonState[index]){
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
// left button panel
for (int index = 4; index < 8; index++){
int currentButtonState = !digitalRead(buttonToPinMap[index]);
if (currentButtonState != lastButtonState[index]){
Joystick.setButton(index+8, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
// upper button panel
for (int index = 8; index < 12; index++){
int currentButtonState = !digitalRead(buttonToPinMap[index]);
if (currentButtonState != lastButtonState[index]){
Joystick.setButton(index-4, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
// special button panel
for (int index = 12; index < 14; index++){
int currentButtonState = !digitalRead(buttonToPinMap[index]);
if (currentButtonState != lastButtonState[index]){
Joystick.setButton(index-4, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
// joystick button panel
for (int index = 14; index < 16; index++){
int currentButtonState = analogRead(buttonToPinMap[index]);
if (currentButtonState <= 512){
currentButtonState = 1;
} else {
currentButtonState = 0;
}
if (currentButtonState != lastButtonState[index]){
Joystick.setButton(index-4, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
//two joystick panels
Joystick.setRxAxis(1023-analogRead(2));
Joystick.setZAxis(1023-analogRead(3));
Joystick.setYAxis(1023-analogRead(4));
Joystick.setXAxis(1023-analogRead(5));
}