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 #131 background imageLoading...
Page #131 background image
Eventually, we add our own
onReadLine
listener to the
SerialDevice
object and use
our usual
bind
trick.
The
onReadLine
listener interprets the data we get from the Arduino:
BrowserGame/GameController/js/game_controller.js
GameController.prototype.onReadLine = function(line) {
const TOLERANCE = 5;
var attr = line.trim().split(' ');
if (attr.length == 4) {
this.moveRight = false;
this.moveLeft = false;
var x = parseInt(attr[0]);
if (x <= this.threshold - TOLERANCE) {
this.moveLeft = true;
} else if (x >= this.threshold + TOLERANCE) {
this.moveRight = true;
}
this.buttonPressed = (attr[3] == '1');
}
var message = 'moveLeft(' + this.moveLeft + '), ' +
'moveRight (' + this.moveRight + '), ' +
'buttonPressed(' + this.buttonPressed + ')';
console.log(message);
document.getElementById('output').innerText = message;
}
The method splits the line it receives at each blank character. Then it makes
sure that the line contains exactly four attributes. If yes, it checks whether
the current X position is to the left or to the right of the controllers tipping
point. Note that we use the threshold value here to make the movement
detection smoother.
Finally, the method checks whether the controllers button is currently
pressed. Also, it writes the controllers current state to the console.
By the way, if youd like to control the game using a Nunchuk later on (see
Chapter 9, Tinkering with the Wii Nunchuk, on page 145), you only have to
adjust the
GameController
class.
In Figure 22, The game controller communicates with a Chrome app, on page
114, you can see the output of a sample Chrome app that outputs the game
controllers state to the JavaScript console.
We can now conveniently combine Chrome apps with our motion-sensing
controller. In the next section, youll learn how to create a more advanced
application using these techniques.
report erratum discuss
Writing a GameController Class 113
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