-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathButton_Box.ino
203 lines (184 loc) · 4.9 KB
/
Button_Box.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* -------------------------------------------------------------------------
* Arduino Button Box
* -------------------------------------------------------------------------
* Copyright Kevin Peat 2014
* This sketch is licensed public domain
* -------------------------------------------------------------------------
* Original code provided by Dan Nixon on his blog
* Multiplexing Switches on Arduino without additional ICs
* http://www.dan-nixon.com/2012/04/multiplexing-switches-on-arduino.html
* -------------------------------------------------------------------------
* Requires an Arduino Leonardo or other similar device that is able to
* emulate a USB HID Keyboard
* -------------------------------------------------------------------------
* Hardware to be used with iRacing and other racing sims. The simple
* hardware design means that only one button can be pressed at a time
* which is not a problem for this type of hardware. Also there is no need
* for key modifiers so only simple key presses can be sent.
* -------------------------------------------------------------------------
*/
/*
* -------------------------------------------------------------------------
* Arduino Leonardo key codes
* -------------------------------------------------------------------------
0 = 0x30
1 = 0x31
2 = 0x32
3 = 0x33
4 = 0x34
5 = 0x35
6 = 0x36
7 = 0x37
8 = 0x38
9 = 0x39
a = 0x61
b = 0x62
c = 0x63
d = 0x64
e = 0x65
f = 0x66
g = 0x67
h = 0x68
i = 0x69
j = 0x6A
k = 0x6B
l = 0x6C
m = 0x6D
n = 0x6E
o = 0x6F
p = 0x70
q = 0x71
r = 0x72
s = 0x73
t = 0x74
u = 0x75
v = 0x76
v = 0x76
w = 0x77
x = 0x78
y = 0x79
z = 0x7A
' = 0x27
, = 0x2C
- = 0x2D
. = 0x2E
/ = 0x2F
; = 0x3B
= = 0x3D
[ = 0x5B
] = 0x5D
#~ = 0x7E
# = 0xE0
F1 = 0xC2
F2 = 0xC3
F3 = 0xC4
F4 = 0xC5
F5 = 0xC6
F6 = 0xC7
F7 = 0xC8
F8 = 0xC9
F9 = 0xCA
F10 = 0xCB
F11 = 0xCC
F12 = 0xCD
BACKSPACE = 0xB2
DELETE = 0xD4
ESC = 0xB1
HOME = 0xD2
INSERT = 0xD1
RETURN = 0xB0
SPACE = 0x20
TAB = 0xB3
LEFT_ARROW = 0xD8
RIGHT_ARROW = 0xD7
UP_ARROW = 0xDA
DOWN_ARROW = 0xD9
* -------------------------------------------------------------------------
*/
// Array of pins for the columns
int cPins[] = {2, 3, 4, 5, 6};
// Number of pins in the column array
int cPinsNo = 5;
// Array of pins for the rows
int rPins[] = {8, 9, 10, 11};
// Number of pins in the row array
int rPinsNo = 4;
// Array for the last known switch states [cPinsNo][rPinsNo]
int colPrev[5][4] = {0};
// Key codes to be used for each button
// (see table above for codes to use)
// (columns and rows are transposed on device)
uint8_t buttonCodes[5][4] = {
{0xC2, 0xC8, 0x2D, 0x35},
{0xC4, 0xC9, 0x3D, 0x36},
{0xC5, 0x39, 0xD8, 0x38},
{0xC6, 0xDA, 0xD9, 0x31},
{0xC7, 0x20, 0xD7, 0x32}
};
void setup()
{
//Start Serial
Serial.begin(9600);
Serial.println("Multiplexed Buttons Test");
//Set the Column Pin Mode
Serial.println("Setting Column Pins...");
for(int cPin = 0; cPin < cPinsNo; cPin++)
{
pinMode(cPins[cPin], OUTPUT);
digitalWrite(cPins[cPin], HIGH);
}
//Set the Row Pin Mode
Serial.println("Setting Row Pins...");
for(int rPin = 0; rPin < rPinsNo; rPin++)
{
pinMode(rPins[rPin], INPUT);
digitalWrite(rPins[rPin], HIGH);
}
Serial.println("Ready!");
}
void loop()
{
// Loop through the columns
for(int cPin = 0; cPin < cPinsNo; cPin++)
{
digitalWrite(cPins[cPin], LOW);
// Loop through the rows
for(int rPin = 0; rPin < rPinsNo; rPin++)
{
//Check if each switch is pressed
if(digitalRead(rPins[rPin]) == LOW)
{
// Check to see if the state has changed since last time
if(colPrev[cPin][rPin] == 0)
{
// Do action here, switch is on
Serial.print(cPins[cPin]);
Serial.print(", ");
Serial.print(rPins[rPin]);
Serial.println(" ON");
Keyboard.press(buttonCodes[cPin][rPin]);
delay(150);
Keyboard.release(buttonCodes[cPin][rPin]);
// Update last known state of this switch
colPrev[cPin][rPin] = 1;
}
}
else {
// Check to see if the state has changed since last time
if(colPrev[cPin][rPin] == 1)
{
// Do action here, switch is off
Serial.print(cPins[cPin]);
Serial.print(", ");
Serial.print(rPins[rPin]);
Serial.println(" OFF");
// Update last known state of this switch
colPrev[cPin][rPin] = 0;
}
}
}
digitalWrite(cPins[cPin], HIGH);
}
}