EasyManuals Logo

Arduino uno User Manual

Arduino uno
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #130 background imageLoading...
Page #130 background image
To control the ball with the paddle at the bottom of the screen, you can tilt
the controller around the x-axis to move the paddle horizontally. The game
runs in a web browser, and it communicates with the Arduino via a serial
port. It reads the game controllers state several times per second to determine
the controllers current x-axis position.
Although this isnt a book about game programming, it wont hurt to take a
look at the games innards, especially because game programming with
JavaScript is really pure fun! Also, JavaScript is very popular. Its available
on nearly every computer, because all modern web browsers come with
JavaScript interpreters.
Well implement the game as a Google Chrome app, so make sure youve read
Appendix 4, Controlling the Arduino with a Browser, on page 267. The Chrome
app implements the games logic, and it talks to our game controller via serial
port. It reads the current controller state and turns it into movements of our
paddle on the screen.
Writing a GameController Class
With the
SerialDevice
class from Writing a SerialDevice Class, on page 274, its
easy to create a
GameController
class that provides even more convenient access
to our motion-sensing Arduino. Heres its constructor function:
BrowserGame/GameController/js/game_controller.js
var GameController = function(path, threshold) {
this.arduino = new SerialDevice(path);
this.threshold = threshold || 325;
this.moveLeft = false;
this.moveRight = false;
this.buttonPressed = false;
this.boundOnReadLine = this.onReadLine.bind(this);
this.arduino.onReadLine.addListener(this.boundOnReadLine);
this.arduino.connect();
}
This function defines several properties. First, it creates a property named
arduino
and initializes it with a new
SerialDevice
object. The next property defines
a threshold for the game controllers x-axis. To check whether a user has
tilted the game controller to the left or to the right, we need to know the con-
trollers resting point. Instead of looking for the exact resting point, well add
some tolerance, and thats the value well store in
threshold
.
The following three properties are all Boolean flags representing the controllers
current state. If
moveLeft
is true, the user has moved the controller to the left.
Chapter 7. Writing a Game for the Motion-Sensing Game Controller 112
report erratum discuss
www.it-ebooks.info

Table of Contents

Other manuals for Arduino uno

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino uno and is the answer not in the manual?

Arduino uno Specifications

General IconGeneral
Form factorArduino
CertificationRoHS, FC, CE
Processor model-
Processor frequency- MHz
Microcontroller modelATmega328
Microcontroller frequency16 MHz
DC input voltage7-12 V
Operating voltage5 V
DC current per I/O pin40 mA
Flash memory0.032 MB
Maximum internal memory- GB
SRAM (Static Random Access Memory)2 KB
EEPROM (Electrically Erasable Programmable Read-Only Memory)1 KB
Wi-FiNo
Number of analog I/O pins6
Number of digital I/O pins14
Weight and Dimensions IconWeight and Dimensions
Board dimensions53.4 x 68.6 mm

Related product manuals