Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Hands-On Robotics with JavaScript

You're reading from   Hands-On Robotics with JavaScript Build robotic projects using Johnny-Five and control hardware with JavaScript and Raspberry Pi

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789342055
Length 214 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Kassandra Perch Kassandra Perch
Author Profile Icon Kassandra Perch
Kassandra Perch
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Setting Up Your Development Environment FREE CHAPTER 2. Creating Your First Johnny-Five Project 3. Building Interactive Projects with RGB LED 4. Bringing in Input with Buttons 5. Using a Light Sensor to Create a Night-Light 6. Using Motors to Move Your Project 7. Using Servos for Measured Movement 8. The Animation Library 9. Getting the Information You Need 10. Using MQTT to Talk to Things on the Internet 11. Building a NodeBots Swarm 12. Assessments 13. Other Books You May Enjoy

The Johnny-Five motor object


The motor object in Johnny-Five allows us to easily control our motors without having to worry about communicating with the hat via the Pi. Let's code a test setup with the REPL before coding our project, to make sure everything is working.

Create a new project folder and, inside it, run the following:

npm init -y

And, create a file in the folder named motor-test.js. Start by requiring in Johnny-Five and Raspi-IO, instantiating your board object, and creating a board.on('ready') handler, as we usually do:

constRaspi=require('raspi-io')
constfive=require('johnny-five')
constboard=newfive.Board({
  io:newRaspi()
})

board.on('ready', () => {

})

 

Now, we're ready to set up our motor object, keeping in mind that we'll need to configure for our hat.

Constructors for our hat

If you are using the Adafruit hat, your constructor is as follows:

let motor = new five.Motor(five.Motor.SHIELD_CONFIGS.ADAFRUIT_V2.M1)

And if you're using the L293D hat, your constructor is as follows...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime