Note: this page refers to a product that is retired.

EsploraKart

Use the Esplora as a controller to play a kart racing game.

Esplora Kart

This example demonstrates how to turn your Esplora into a customized computer game pad. It uses the analog joystick and the four switches as inputs to a video game. The Esplora will appear to the computer as a standard keyboard, the computer will see joystick movement and button presses as keystrokes.

The configuration chosen in this example is suitable for SuperTuxKart, an open-source racing game, available for Windows, OSX, and Linux. It can be downloaded from http://supertuxkart.sourceforge.net/ .

Hardware Required

  • Arduino Esplora

Circuit

Only your Arduino Esplora is needed for this example. Connect the Esplora to your computer with a USB cable.

Esplora as SuperTuxKart game controller
Esplora as SuperTuxKart game controller

Code

The default keys to play with the SuperTuxKart game are:

commandkey
ThrottleKEY_UP_ARROW
BrakeKEY_DOWN_ARROW
Turn rightKEY_RIGHT_ARROW
Turn LeftKEY_LEFT_ARROW
FireSPACE_BAR
BendV
NitroN
Look backB

You need to map these keyboard keystrokes to the available inputs on your Esplora board. To handle the eight different buttons, you'll use arrays, ordered lists of variables with a fixed size. Each array has an index (counting from 0) to keep track of the position you're reading in the array, and each position can contain a number.

This code uses three different arrays: one for the buttons you'll read, a second to hold the current state of the buttons, and a third to hold the keystrokes associated with each button.

The Esplora has the ability to appear to a connected computer as a USB keyboard, so when you press the buttons and move the joystick, it's just like pressing keys on your keyboard.

The for() loop checks the state of all the buttons and the joystick. If the button's state has changed, then the corresponding keypress or release is sent to the computer. See the keyboard library reference for more information on using the Esplora as a keyboard or mouse.

1/*
2
3 Esplora Kart
4
5 This sketch turns the Esplora into a PC game pad.
6
7 It uses the both the analog joystick and the four switches.
8
9 By moving the joystick in a direction or by pressing a switch,
10
11 the PC will "see" that a key is pressed. If the PC is running
12
13 a game that has keyboard input, the Esplora can control it.
14
15 The default configuration is suitable for SuperTuxKart, an
16
17 open-source racing game. It can be downloaded from
18
19 http://supertuxkart.sourceforge.net/ .
20
21 Created on 22 november 2012
22
23 By Enrico Gueli <enrico.gueli@gmail.com>
24
25*/
26
27#include <Esplora.h>
28
29/*
30
31 You're going to handle eight different buttons. You'll use arrays,
32
33 which are ordered lists of variables with a fixed size. Each array
34
35 has an index (counting from 0) to keep track of the position
36
37 you're reading in the array, and each position can contain a number.
38
39 This code uses three different arrays: one for the buttons you'll read;
40
41 a second to hold the current states of those buttons; and a third to hold
42
43 the keystrokes associated with each button.
44
45 */
46
47/*
48
49 This array holds the last sensed state of each of the buttons
50
51 you're reading.
52
53 Later in the code, you'll read the button states, and compare them
54
55 to the previous states that are stored in this array. If the two
56
57 states are different, it means that the button was either
58
59 pressed or released.
60
61 */
62
63bool buttonStates[8];
64
65/*
66
67 This array holds the names of the buttons being read.
68
69 Later in the sketch, you'll use these names with
70
71 the method Esplora.readButton(x), where x
72
73 is one of these buttons.
74
75 */
76
77const byte buttons[] = {
78
79 JOYSTICK_DOWN,
80
81 JOYSTICK_LEFT,
82
83 JOYSTICK_UP,
84
85 JOYSTICK_RIGHT,
86
87 SWITCH_RIGHT, // fire
88
89 SWITCH_LEFT, // bend
90
91 SWITCH_UP, // nitro
92
93 SWITCH_DOWN, // look back
94};
95
96/*
97
98 This array tells what keystroke to send to the PC when a
99
100 button is pressed.
101
102 If you look at this array and the above one, you can see that
103
104 the "cursor down" keystroke is sent when the joystick is moved
105
106 down, the "cursor up" keystroke when the joystick is moved up
107
108 and so on.
109
110*/
111
112const char keystrokes[] = {
113
114 KEY_DOWN_ARROW,
115
116 KEY_LEFT_ARROW,
117
118 KEY_UP_ARROW,
119
120 KEY_RIGHT_ARROW,
121
122 ' ',
123
124 'V',
125
126 'N',
127
128 'B'
129};
130
131/*
132
133 This is code is run only at startup, to initialize the
134
135 virtual USB keyboard.
136
137*/
138void setup() {
139
140 Keyboard.begin();
141}
142
143/*
144
145 After setup() is finished, this code is run continuously.
146
147 Here we continuously check if something happened with the
148
149 buttons.
150
151*/
152void loop() {
153
154 // Iterate through all the buttons:
155
156 for (byte thisButton = 0; thisButton < 8; thisButton++) {
157
158 bool lastState = buttonStates[thisButton];
159
160 bool newState = Esplora.readButton(buttons[thisButton]);
161
162 if (lastState != newState) { // Something changed!
163
164 /*
165
166 The Keyboard library allows you to "press" and "release" the
167
168 keys as two distinct actions. These actions can be
169
170 linked to the buttons we're handling.
171
172 */
173
174 if (newState == PRESSED) {
175
176 Keyboard.press(keystrokes[thisButton]);
177
178 } else if (newState == RELEASED) {
179
180 Keyboard.release(keystrokes[thisButton]);
181
182 }
183
184 }
185
186 // Store the new button state, so you can sense a difference later:
187
188 buttonStates[thisButton] = newState;
189
190 }
191
192 /*
193
194 Wait a little bit (50ms) between a check and another.
195
196 When a mechanical switch is pressed or released, the
197
198 contacts may bounce very rapidly. If the check is done too
199
200 fast, these bounces may be confused as multiple presses and
201
202 may lead to unexpected behaviour.
203
204 */
205
206 delay(50);
207}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.