- •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 7. Writing a Game for the Motion-Sensing Game Controller • 124
we read its width. In the next line we do the same to determine the block’s height. Because all blocks have the same width and height, we have to do this only once. After that we determine the ball’s current position.
In line 38, we use the each method to loop over all blocks in the HTML document. each expects a callback function that gets called for each block. Note that the function doesn’t get any arguments, because you can find the current block in $(this).
In the loop function, we check whether the current block is visible, because if it’s not, we don’t have to check it for a collision. We use our helper functions inXRange and inYRange to see whether the current block has been hit by the ball. In that case we make it invisible, and depending on the way the ball has hit the block, we invert the ball’s velocities.
Finally, we have to make sure that the gameLoop function is called every 30 milliseconds to make the game run smoothly:
BrowserGame/Arduinoid/js/arduinoid.js $(function() {
initGame(); setInterval(gameLoop, 30);
});
We use yet another variant of jQuery’s $ function. This time we pass it an anonymous function that gets called as soon as the HTML page has been loaded completely. In this function, we initialize the game and make sure the gameLoop function is called every 30 milliseconds.
The game is complete, so play a few rounds and relax! You deserve it!
What If It Doesn’t Work?
If you cannot make this chapter’s code run, you should download the code from the book’s website and try to run it. Make sure you’re using the right serial port in the arduinoid.js and game_controller.js files in the code/BrowserGame/Arduinoid/js/ directory.
Exercises
•Create your own computer mouse using the ADXL335 accelerometer. It should work in free air, and it should emit the current acceleration around the x- and y-axes. It should also have a left button and a right button. Write a Chrome app (or perhaps code in a programming language of your choice?) to control a mouse pointer on the screen.
report erratum • discuss
Exercises • 125
Creating Games and Game Controllers with the Arduino
If you’re thinking about a proper case for the game controller you’ve built in the preceding chapter, you might have a look at the Lego/Arduino controller.a Its innards differ from the controller we’ve built, but its case is really cool.
You can use the Arduino to build more than your own cool game controllers. You can also use it to build some cool games. With the right extension shields, you can even turn an Arduino into a powerful gaming console. The most powerful extension shields are probably the Gameduinob and its successor, Gameduino 2.c
If you don’t need color graphics and stereo sound, you can find even cheaper solutions, such as the Video Game Shieldd or the Hackvision.e They generate a monochrome video signal, and you can learn how to do it yourself in Chapter 8, Generating Video Signals with an Arduino, on page 127.
While looking for a solution that doesn’t need an external monitor, someone built a Super Mario Bros. clone with minimal hardware requirements.f It’s a perfect example of the unbelievable creativity that the Arduino sets free.
a.http://strangemeadowlarkprojects.blogspot.de/2014/05/a-legoarduino-game-controller.html
b.http://excamera.com/sphinx/gameduino/
c.http://excamera.com/sphinx/gameduino2/index.html#gameduino2
d.http://www.wayneandlayne.com/projects/video-game-shield/
e.http://nootropicdesign.com/store/index.php?main_page=index&cPath=2
f.http://blog.makezine.com/archive/2010/03/super-mario-brothers-with-an-arduin.html
report erratum • discuss