Learning JavaScript Robotics - Sample Chapter
Learning JavaScript Robotics - Sample Chapter
P U B L I S H I N G
pl
C o m m u n i t y
Learn about the Animation API which allows
you to program complex movements using
timing and key frames
Bring other devices into your Johnny-Five
projects, such as USB devices
Connect your Johnny-Five projects to external
APIs and create your own Internet of Things!
$ 29.99 US
19.99 UK
Sa
m
Kassandra Perch
Learning JavaScript
Robotics
ee
D i s t i l l e d
Learning JavaScript
Robotics
Design, build, and program your own remarkable robots
with JavaScript and open source hardware
E x p e r i e n c e
Kassandra Perch
frontend developer and moved to server-side with the advent of Node.js and was
especially enthralled by the advance of the NodeBots community. She travels the
world speaking at conferences about NodeBots and the fantastic community around
them. When she's not building bots, she's knitting, sewing, sculpting, or playing
video games with her cats.
Preface
Hello! Welcome to Learning JavaScript Robotics. In this book, you'll learn how to write
code for Arduino and other robotics platforms in JavaScript using Johnny-Five. We'll
cover the basics of Johnny-Five, input and output devices, and even movement devices
such as servos and motors. Finally, we'll cover how to connect your bots to the Internet
and move your Johnny-Five code between different platforms.
Preface
Chapter 9, Connecting NodeBots to the World, and Where to Go Next, covers how to
connect your NodeBots to the Internet and use Johnny-Five code with platforms
other than Arduino.
Chapter 1
[1]
We're going to use a microcontroller for our projects. What microcontroller should
you use? Luckily, our use of Johnny-Five means that we can choose from a large array
of different microcontrollers and still write the same code as you'll see in this book!
What exactly is Johnny-Five, and how does it make our lives easier?
[2]
Chapter 1
Installing Node.JS
If you don't have Node.JS already installed, you can download an installer for your
platform from nodejs.org. This installer will also install NPM or Node Package
Manager, which will be used to manage the rest of the software that we'll be using.
Run the installer on your machine, which may require a restart. After this, open up
your terminal application and run the following command:
node -version
The output from this command should be 4.x.x, where x are integers.
[3]
You should see a spinner, followed by some output. Unless you see an ERR NOT
OK message at the end of your output, you're good to go with Johnny-Five.
On a Mac machine, you may need to install XCode
developer command-line tools.
Once you've downloaded the software, open it. Then, we'll make sure that your
serial connection works.
If you are using a board other than an Arduino, this step is not
necessary. However, there may be other steps. These will be
outlined with the wrapper plugin for your board.
Plug the USB cable into both the board and the computer. A few LEDs should
light up on this boardthis is normal. Then, go to the Tools menu in the Arduino
IDE and hover over the ports submenu. You should see a list of ports that looks
somewhat like the following screenshot:
[4]
Chapter 1
You should see at least one entry in this list that fits the following format: /dev/
cu.usbmodem*****. It may or may not have Arduino Uno next to it. If you see this,
go ahead and click on it, because this is the port you will want to use for the Firmata
installation. If you have this, it means your board can communicate with your
computer, and you're ready to install Firmata.
To install Firmata on your board, go to File | Examples | Firmata | StandardFirmata,
as shown in the following screenshot:
[5]
Once you've opened the sketch, you should get an IDE window that looks like the
following screenshot:
Once this sketch is up, click on the Upload button (it looks like an arrow pointing
to the right) to upload Firmata to your board. Once the uploading is done, you can
close the Arduino IDE, and you will be ready to start working with JavaScript.
A developer named Suz Hinton (@noopkat) is working on a node
program called AVRGirl that will remove this step in the near future.
Take a look at www.github.com/noopkat/avrgirl to learn more!
[6]
Chapter 1
We'll go over more of what this script does in Chapter 2, Working with Johnny-Five,
but the basic overview is this: we require this script in the Johnny-Five module and
use it to create a new board object. When this board is ready, we will create an LED
object at pin 13 (this pin is wired to the onboard LED on an Arduino Uno board).
We then program this LED to blink every half second.
[7]
You should see an LED blink on your Arduino Uno. The following figure shows
where the LED is on the board:
If all is well and the LED is blinking, congratulations! You're ready to start building
robots and applications with Arduino and Johnny-Five!
If there is a problem, many troubleshooting issues can be solved
by checking the Johnny-Five website (www.johnny-five.io).
Summary
In this chapter, we learned about JS robotics and understood what a NodeBot is.
We went through the hardware components that we will be using in the book, and
we also learned how to set up the development environment. Finally, we got to
know how to get the on-board LED to blink. In the next chapter, we'll dive deep into
what makes Johnny-Five so powerful, and we will start writing and building some
more complex projects.
[8]
www.PacktPub.com
Stay Connected: