- •Acknowledgments
- •Preface
- •Who Should Read This Book
- •What’s in This Book
- •Arduino Uno and the Arduino Platform
- •Code Examples and Conventions
- •Online Resources
- •The Parts You Need
- •Starter Packs
- •Complete Parts List
- •1. Welcome to the Arduino
- •What You Need
- •What Exactly Is an Arduino?
- •Exploring the Arduino Board
- •Installing the Arduino IDE
- •Meeting the Arduino IDE
- •Hello, World!
- •Compiling and Uploading Programs
- •What If It Doesn’t Work?
- •Exercises
- •2. Creating Bigger Projects with the Arduino
- •What You Need
- •Managing Projects and Sketches
- •Changing Preferences
- •Using Serial Ports
- •What If It Doesn’t Work?
- •Exercises
- •3. Building Binary Dice
- •What You Need
- •Working with Breadboards
- •Using an LED on a Breadboard
- •First Version of a Binary Die
- •Working with Buttons
- •Adding Your Own Button
- •Building a Dice Game
- •What If It Doesn’t Work?
- •Exercises
- •4. Building a Morse Code Generator Library
- •What You Need
- •Learning the Basics of Morse Code
- •Building a Morse Code Generator
- •Fleshing Out the Morse Code Generator’s Interface
- •Outputting Morse Code Symbols
- •Installing and Using the Telegraph Class
- •Publishing Your Own Library
- •What If It Doesn’t Work?
- •Exercises
- •5. Sensing the World Around Us
- •What You Need
- •Measuring Distances with an Ultrasonic Sensor
- •Increasing Precision Using Floating-Point Numbers
- •Increasing Precision Using a Temperature Sensor
- •Creating Your Own Dashboard
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Wiring Up the Accelerometer
- •Bringing Your Accelerometer to Life
- •Finding and Polishing Edge Values
- •Building Your Own Game Controller
- •More Projects
- •What If It Doesn’t Work?
- •Exercises
- •7. Writing a Game for the Motion-Sensing Game Controller
- •Writing a GameController Class
- •Creating the Game
- •What If It Doesn’t Work?
- •Exercises
- •8. Generating Video Signals with an Arduino
- •What You Need
- •How Analog Video Works
- •Building a Digital-to-Analog Converter (DAC)
- •Connecting the Arduino to Your TV Set
- •Using the TVout Library
- •Building a TV Thermometer
- •Working with Graphics in TVout
- •What If It Doesn’t Work?
- •Exercises
- •9. Tinkering with the Wii Nunchuk
- •What You Need
- •Wiring a Wii Nunchuk
- •Talking to a Nunchuk
- •Building a Nunchuk Class
- •Using Our Nunchuk Class
- •Creating Your Own Video Game Console
- •Creating Your Own Video Game
- •What If It Doesn’t Work?
- •Exercises
- •10. Networking with Arduino
- •What You Need
- •Using Your PC to Transfer Sensor Data to the Internet
- •Registering an Application with Twitter
- •Tweeting Messages with Processing
- •Communicating Over Networks Using an Ethernet Shield
- •Using DHCP and DNS
- •What If It Doesn’t Work?
- •Exercises
- •11. Creating a Burglar Alarm with Email Notification
- •What You Need
- •Emailing from the Command Line
- •Emailing Directly from an Arduino
- •Detecting Motion Using a Passive Infrared Sensor
- •Bringing It All Together
- •What If It Doesn’t Work?
- •Exercises
- •What You Need
- •Understanding Infrared Remote Controls
- •Grabbing Remote Control Codes
- •Cloning a Remote
- •Controlling Infrared Devices Remotely with Your Browser
- •Building an Infrared Proxy
- •What If It Doesn’t Work?
- •Exercises
- •13. Controlling Motors with Arduino
- •What You Need
- •Introducing Motors
- •First Steps with a Servo Motor
- •Building a Blaminatr
- •What If It Doesn’t Work?
- •Exercises
- •Current, Voltage, and Resistance
- •Electrical Circuits
- •Learning How to Use a Wire Cutter
- •Learning How to Solder
- •Learning How to Desolder
- •The Arduino Programming Language
- •Bit Operations
- •Learning More About Serial Communication
- •Serial Communication Using Various Languages
- •What Are Google Chrome Apps?
- •Creating a Minimal Chrome App
- •Starting the Chrome App
- •Exploring the Chrome Serial API
- •Writing a SerialDevice Class
- •Index
Chapter 6. Building a Motion-Sensing Game Controller • 106
It starts with a small delay to give the Arduino some time to switch between analog pins; otherwise, you might get bad data. Then it reads the acceleration for the axis we have passed and stores it at the current buffer position belonging to the axis. It increases the buffer position and sets it back to zero when the end of the buffer has been reached. Finally, we return the average value of the data we have gathered so far for the current axis.
That’s the whole trick, and the data structure we’ve just built is named circular buffer.2 To see its effect, leave the sensor untouched on your desk and run the program with different buffer sizes. If you don’t touch the sensor, you wouldn’t expect the program’s output to change. But if you set BUFFER_SIZE to 1, you will quickly see small changes. They will disappear as soon as the buffer is big enough.
The acceleration data we measure now is accurate enough that we can finally build a game controller that won’t annoy users with unexpected movements.
Building Your Own Game Controller
To build a full-blown game controller, we only need to add a button to our breadboard. Figure 19, Game controller with accelerometer and pushbutton, on page 107 shows you how to do it. (Please double-check the pin labels on your breakout board!)
That’s how it looks inside a typical modern game controller. We won’t build a fancy housing for the controller, but we still should think about ergonomics for a moment. Our current breadboard solution is rather fragile (see the following figure), and you cannot really wave around the board when it’s connected to the Arduino. Sooner or later you’ll disconnect some wires, and the controller will stop working.
To solve this problem, you could try to attach the breadboard to the Arduino using some rubber bands. That works, but it doesn’t look very pretty, and it’s still hard to handle.
A much better solution is to use an Arduino Proto Shield—a pluggable breadboard that lets you quickly build circuit prototypes. The breadboard is surrounded by the Arduino’s pins, so you no longer need long wires. Shields are a great way to enhance an Arduino’s capabilities, and you can get shields for many different purposes, such as adding Ethernet, sound, displays, and so on. The figure on page 108 shows a bare Proto Shield and a shield containing our motion sensor.
2.http://en.wikipedia.org/wiki/Circular_buffer
report erratum • discuss
Building Your Own Game Controller • 107
Figure 19—Game controller with accelerometer and pushbutton
Now that the hardware is complete, we need a final version of the game controller software. It supports the button we’ve added, and it performs the antijittering we created in Finding and Polishing Edge Values, on page 103:
MotionSensor/Controller/Controller.ino
#include <Bounce2.h>
const unsigned int BUTTON_PIN = 7; const unsigned int X_AXIS_PIN = A2; const unsigned int Y_AXIS_PIN = A1; const unsigned int Z_AXIS_PIN = A0; const unsigned int NUM_AXES = 3; const unsigned int PINS[NUM_AXES] = {
X_AXIS_PIN, Y_AXIS_PIN, Z_AXIS_PIN
};
report erratum • discuss
Chapter 6. Building a Motion-Sensing Game Controller • 108
Figure 20—An empty Proto Shield (left), and one holding our game controller (right)
const unsigned int BUFFER_SIZE = 16; const unsigned int BAUD_RATE = 38400; int buffer[NUM_AXES][BUFFER_SIZE]; int buffer_pos[NUM_AXES] = { 0 }; boolean button_pressed = false;
Bounce button;
void setup() { Serial.begin(BAUD_RATE); pinMode(BUTTON_PIN, INPUT); button.attach(BUTTON_PIN); button.interval(20);
}
int get_axis(const int axis) { delay(1);
buffer[axis][buffer_pos[axis]] = analogRead(PINS[axis]); buffer_pos[axis] = (buffer_pos[axis] + 1) % BUFFER_SIZE;
long sum = 0;
for (unsigned int i = 0; i < BUFFER_SIZE; i++) sum += buffer[axis][i];
return round(sum / BUFFER_SIZE);
}
int get_x() { return get_axis(0); } int get_y() { return get_axis(1); } int get_z() { return get_axis(2); }
report erratum • discuss
More Projects • 109
void loop() { Serial.print(get_x()); Serial.print(" "); Serial.print(get_y()); Serial.print(" "); Serial.print(get_z()); Serial.print(" ");
if (button.update()) {
button_pressed = button.read() == HIGH;
}
Serial.println(button_pressed == HIGH ? "1" : "0"); delay(10);
}
As in Building a Dice Game, on page 55, we use the Bounce class to debounce the button. The rest of the code is pretty much standard, and the only thing worth mentioning is that we use a 38,400 baud rate to transfer the controller data sufficiently fast.
Compile and upload the code, open the serial terminal, and play around with the controller. Move it, press the button sometimes, and it should output something like the following:
324 365 396 0
325 364 397 0
325 364 397 1
325 364 397 0
325 365 397 0
325 365 397 1
326 364 397 0
A homemade game controller is nice, and you can use it for many projects. For example, you could use it to control devices such as a robot, a marble maze, or something similar. Its original purpose is to control games, so we’ll build one in the next chapter.
More Projects
If you keep your eyes open, you’ll quickly find many more applications for accelerometers than you might imagine. Here’s a small collection of both commercial and free products:
report erratum • discuss