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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Javascript Robotics

You're reading from   Learning Javascript Robotics Design, build, and program your own remarkable robots with JavaScript and open source hardware

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781785883347
Length 160 pages
Edition 1st Edition
Languages
Tools
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 (11) Chapters Close

Preface 1. Getting Started with JS Robotics FREE CHAPTER 2. Working with Johnny-Five 3. Using Digital and PWM Output Pins 4. Using Specialized Output Devices 5. Using Input Devices and Sensors 6. Moving Your Bot 7. Advanced Movement with the Animation Library 8. Advanced Components – SPI, I2C, and Other Devices 9. Connecting NodeBots to the World, and Where to Go Next Index

Using the Read-Eval-Print-Loop (REPL)

The Read, Eval, Print Loop, or REPL, is a concept relative to many scripting languages, but it is new to libraries, and definitely new to robotics. Think about how you alter the state in a typical Arduino program: you modify the source code, re-load it onto the board, and wait for it to run.

However, due to the way Johnny-Five works, we can modify the state of our robot code while the code is running. This is because we use Firmata—the board is just a thin client that reacts to instructions from our node program, so if we let our Node programs send different instructions, we can change how our robot works in real time.

The way to do this in a Johnny-Five program is by injecting components into the REPL, which allows us to use them.

Making components available to the REPL

We're going to modify our script from the previous section in order to manipulate our LED. To do this, we're going to use the this.repl.inject() function. The this keyword, when used within our board.on('ready') handler, is a reference to the global context, so we can access the REPL for the program we are working on with this.repl. The inject method accepts an object; the keys of this object will represent the names you can access from the REPL, and the values will be the components you wish to access.

So, we're going to pass the following object to the inject method. It will allow us to access our LED component by the myLed name:

{
  myLed: led
}

Our new program looks like this:

var five = require("johnny-five");

var board = new five.Board();

board.on("ready", function() {
  var led = new five.Led(11);

  this.repl.inject({
    myLed: led
  });

  led.blink(500);
});

Go ahead and save this in LED-repl.js. Now we have the code that we had before—the LED at pin 11 will blink—but we have programmatic access to this LED via the REPL. Now, let's run it and have some fun.

Using the REPL

First, with the LED still wired to pin 11, attach your board to your computer. Then, in your command line, in the same folder as your .js file, run the following:

> node LED-repl.js

You should see a boot-up sequence, followed by a prompt—it'll look like the following screenshot. Your LED wired to pin 11 should also start blinking.

Using the REPL

The terminal setup for the Johnny-Five REPL prompt

This (as well as the Repl Initialized line) means that you can start working with the REPL. Try typing myLed and hit Enter. What you'll see is an object representing your LED:

Using the REPL

The output of your myLed object in the REPL

You can see the names of several functions and attributes of the LED object. Next, we'll use the REPL to stop the blinking of the LED. Type myLed.stop() into the REPL and hit Enter. As the .stop() function returns the LED object, the output will look like the following screenshot:

Using the REPL

The output from myLed.stop();

This function should return quickly, and the LED will stop blinking.

Note

Please note that the LED will not necessarily turn off; it may just stay on.

One of the cool things about Johnny-Five's object functions is that they are chainable—if you want the LED to remain off once you stop its blinking, you can use myLed.stop().off():

Using the REPL

Using chainable function calls in the REPL

There are a bunch of LED functions available to you in the REPL:

  • .on() and .off()
  • .blink()
  • .pulse()
  • .toggle()
  • .strobe()
  • .fadeIn() and .fadeOut()

Try them all to see what happens with your myLed object!

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 $19.99/month. Cancel anytime
Banner background image