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.
 
  
    
    
Code
The default keys to play with the SuperTuxKart game are:
| command | key | 
|---|---|
| Throttle | KEY_UP_ARROW | 
| Brake | KEY_DOWN_ARROW | 
| Turn right | KEY_RIGHT_ARROW | 
| Turn Left | KEY_LEFT_ARROW | 
| Fire | SPACE_BAR | 
| Bend | V | 
| Nitro | N | 
| Look back | B | 
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 Kart4
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 running12
13  a game that has keyboard input, the Esplora can control it.14
15  The default configuration is suitable for SuperTuxKart, an16
17  open-source racing game. It can be downloaded from18
19  http://supertuxkart.sourceforge.net/ .20
21  Created on 22 november 201222
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 array34
35  has an index (counting from 0) to keep track of the position36
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 hold42
43  the keystrokes associated with each button.44
45 */46
47/*48
49  This array holds the last sensed state of each of the buttons50
51  you're reading.52
53  Later in the code, you'll read the button states, and compare them54
55  to the previous states that are stored in this array. If the two56
57  states are different, it means that the button was either58
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 with70
71  the method Esplora.readButton(x), where x72
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, // fire88
89  SWITCH_LEFT, // bend90
91  SWITCH_UP, // nitro92
93  SWITCH_DOWN, // look back94};95
96/*97
98  This array tells what keystroke to send to the PC when a99
100  button is pressed.101
102  If you look at this array and the above one, you can see that103
104  the "cursor down" keystroke is sent when the joystick is moved105
106  down, the "cursor up" keystroke when the joystick is moved up107
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 the134
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 the148
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" the167
168        keys as two distinct actions. These actions can be169
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, the197
198    contacts may bounce very rapidly. If the check is done too199
200    fast, these bounces may be confused as multiple presses and201
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.