Rick Waldron, Anna Gerber, David Resseguie, Emily Rose, Susan Hinton, Sara Gorecki, Bryan Hughes, Andrew Fisher, Julian David Duque, Pawel Szymczykowski, Donovan Buck, Jonathan Beri, Kassandra Perch, Raquel Vlez, Lyza Danger Gardner
Make: JavaScript Robotics
by Backstop Media and Rick Waldron
Copyright 2015 Backstop Media, LLC. All rights reserved.
Printed in Canada.
Published by Maker Media, Inc. , 1160 Battery Street East, Suite 125, San Francisco, California 94111.
Maker Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our distributors corporate/institutional sales department: 800-998-9938 or .
- Editor: Brian Jepson
- Production Editor: Melanie Yarbrough
- Copyeditor: Tracy Brown Hamilton
- Proofreader: Jasmine Kwityn
- Indexer: Meghan Jones, WordCo Indexing
- Interior Designer: David Futato
- Cover Designer: Brian Jepson
- Cover Photographer: Pawel Szymczykowski
- Illustrator: Rebecca Demarest
- April 2015: First Edition
Revision History for the First Edition
- 2015-04-03: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781457186950 for release details.
The Make logo and Maker Media logo are registered trademarks of Maker Media, Inc. Make: JavaScript Robotics and related trade dress are trademarks of Maker Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Maker Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
978-1-4571-8695-0
[TI]
Preface
By Rick Waldron
I love programming and I also love making things. When I discovered Chris Williams node-serialport
module (for Node.js), I remember thinking, Now I can program the things that I make. So I did! I contributed an Arduino sketch that printed a photoresistor sensor value to the open serial port and a small JavaScript handler program that listened for data and printed a graph to the terminal. Months later, I started contributing to Cam Pedersens duino project, until Julian Gaultier approached me with his JavaScript implementation of the Firmata protocol. From there, we set out to build a collection of high-level component classes with one goal: to make it easy and fun to control hardware with JavaScript. This book will show you what we built and how to use it to program the things that you make.
While the physical challenges of engineering a hardware project remain the same as they would for a project programmed in any other language, this book is going to show you how to think about that hardware in terms of objects that maintain state and provide control behaviors in the form of intuitively designed interfaces.
So, how would you verbally describe adding an LED to a project and then turning it on? You might say, Connect the LED to ground and pin 9, then turn it on. Using the Johnny-Five framework, that would be written as:
// Connect the LED to ground and pin 9
var
led
=
new
five
.
Led
(
9
);
// then turn it on
led
.
on
();
What about connecting a servo and then setting its horn to a specific angle in degrees? Attach the servo to pin 10 and position its horn to 110. Heres what that looks like:
// Attach the servo to pin 10
var
servo
=
new
five
.
Servo
(
10
);
// position its horn to 110
servo
.
to
(
110
);
These examples both illustrate an output, but what about input? Consider how Arduino sketches work: they generally rely on a program loop and often introduce some form of delay when reading and processing input. This means that your Arduino sketch is blocked whenever it is waiting for input. When writing programs in JavaScript, the process is never blocked; instead, your handlers wait for data to arrive and process it asynchronously:
var
sensor
=
new
five
.
Sensor
(
"A0"
);
sensor
.
on
(
"data"
,
function
()
{
console
.
log
(
this
.
value
);
});
These are trivial examples, but they illustrate the patterns that you will see repeated consistently throughout this book. Each project will show you how to construct it in the physical sense, then construct it in the abstract programming sense, and the latter will align with the former.
With these concepts, you will build and program:
- Walkers, typers, swimmers, and rovers (Chapters )
- A dancing hexapod ()
- Voice-activated relay control ()
- An indoor sundial ()
- Holiday, mood, or anytime lighting (Chapters )
- A security and notification system ()
- Sonar-based artificial intelligence ()
- A delta bot ()
- Musical shoes ()
For me, the most exciting part about this book is the authors themselves. This group is an excellent representation of NodeBots community members that have stood out since the very beginning. They are more than just writers or engineers: they are teachers, communicators, leaders, and (in my opinion) heroes. It would be an understatement to say that I couldnt have done it without them.
Enough talk, more rock. These projects do not have to be done in any specific order, so take a look at the Table of Contents, find a project that sounds like fun, and start building!
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
This element signifies a tip, suggestion, or general note.
This element indicates a warning or caution.
The part numbers in each chapter use the following abbreviations:
- MS: Maker Shed
- AZ: Amazon
- AF: Adafruit
- SF: SparkFun
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from Make: books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.