forked from devonshire/arduino-joystick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoystick.ino
156 lines (136 loc) · 3.3 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
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
/*
* -------------------------------------------------------------------------
* Digital Joystick Code
* -------------------------------------------------------------------------
* Copyright Kevin Peat 2013
* This code module is licensed public domain
* -------------------------------------------------------------------------
* Uses modified HID.cpp and USBAPI.h provided by "That Guy"
* at http://www.imaginaryindustries.com/blog/?p=80
* -------------------------------------------------------------------------
*/
// Pin definitions
const int BUT1_PIN = 6;
const int BUT2_PIN = 7;
const int LEFT_PIN = 8;
const int UP_PIN = 9;
const int RIGHT_PIN = 10;
const int DOWN_PIN = 11;
const int LED_PIN = 13;
// Previous states
boolean button1State = false;
boolean button2State = false;
boolean stickUp = false;
boolean stickDown = false;
boolean stickLeft = false;
boolean stickRight = false;
// Hold joystick state
JoyState_t joySt;
void setup()
{
// Pin definitions
pinMode(BUT1_PIN, INPUT);
pinMode(BUT2_PIN, INPUT);
pinMode(LEFT_PIN, INPUT);
pinMode(UP_PIN, INPUT);
pinMode(RIGHT_PIN, INPUT);
pinMode(DOWN_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
// Use pull-up resistors
digitalWrite(BUT1_PIN, HIGH);
digitalWrite(BUT2_PIN, HIGH);
digitalWrite(LEFT_PIN, HIGH);
digitalWrite(UP_PIN, HIGH);
digitalWrite(RIGHT_PIN, HIGH);
digitalWrite(DOWN_PIN, HIGH);
// Set joystick initial state
joySt.xAxis = 127;
joySt.yAxis = 127;
joySt.zAxis = 127;
joySt.xRotAxis = 0;
joySt.yRotAxis = 0;
joySt.zRotAxis = 0;
joySt.throttle = 0;
joySt.rudder = 0;
joySt.hatSw1 = 0;
joySt.hatSw2 = 0;
joySt.buttons = 0;
}
void loop()
{
// Button 1 pressed
if (!button1State && digitalRead(BUT1_PIN) == LOW)
{
button1State = true;
joySt.buttons = joySt.buttons | 1;
}
// Button 1 released
if (button1State && digitalRead(BUT1_PIN) == HIGH)
{
button1State = false;
joySt.buttons = joySt.buttons & 254;
}
// Button 2 pressed
if (!button2State && digitalRead(BUT2_PIN) == LOW)
{
button2State = true;
joySt.buttons = joySt.buttons | 2;
}
// Button 2 released
if (button2State && digitalRead(BUT2_PIN) == HIGH)
{
button2State = false;
joySt.buttons = joySt.buttons & 253;
}
// Joystick pressed up
if (digitalRead(UP_PIN) == LOW)
{
stickUp = true;
joySt.yAxis = 255;
}
// Joystick was up but no longer
if (stickUp && digitalRead(UP_PIN) == HIGH)
{
stickUp = false;
joySt.yAxis = 127;
}
// Joystick pressed right
if (digitalRead(RIGHT_PIN) == LOW)
{
stickRight = true;
joySt.xAxis = 255;
}
// Joystick was right but no longer
if (stickRight && digitalRead(RIGHT_PIN) == HIGH)
{
stickRight = false;
joySt.xAxis = 127;
}
// Joystick pressed down
if (digitalRead(DOWN_PIN) == LOW)
{
stickDown = true;
joySt.yAxis = 0;
}
// Joystick was down but no longer
if (stickDown && digitalRead(DOWN_PIN) == HIGH)
{
stickDown = false;
joySt.yAxis = 127;
}
// Joystick pressed left
if (digitalRead(LEFT_PIN) == LOW)
{
stickLeft = true;
joySt.xAxis = 0;
}
// Joystick was left but no longer
if (stickLeft && digitalRead(LEFT_PIN) == HIGH)
{
stickLeft = false;
joySt.xAxis = 127;
}
// Send joystick state
Joystick.setState(&joySt);
}