Johnny Five, how to use a REPL
This post is part of the Johnny Five series. See the first post here.
When you run a program using Johnny Five, you can see that in the terminal, we have access to a REPL, a term that means Read-Evaluate-Print-Loop.

In other words, we can write commands in here.
Let’s try by creating a repl.js file with this code:
const { Board } = require("johnny-five")
const board = new Board()
I am going to play with the LCD circuit made in the previous lesson.
Run the program with node repl.js:

Next, we’re going to write some commands in the REPL.
Start by requiring the LCD class:
const { LCD } = require("johnny-five")

Then initialize an lcd object from it:
const lcd = new LCD({ pins: [7, 8, 9, 10, 11, 12] })

Now write to the LCD display:
lcd.print("Hello!")
You’ll see a big message coming back:

Because the command returns a reference to the LCD object. This is to let us chain commands together, like this:
lcd.clear().print("Hello!")
If you don’t run clear(), any new thing you write is going to be appended to the one already there.
To write to the second row, you call cursor(1) (the default row is 0:
lcd.clear().print("Hello from")
lcd.cursor(1, 0).print("Johnny-Five!")

download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.