/

Johnny Five: How to Use a REPL for Embedded Development

Johnny Five: How to Use a REPL for Embedded Development

tags: [“johnny five”, “REPL”, “embedded development”]

This post is part of the Johnny Five series. See the first post here.

When working with Johnny Five, it’s important to understand the concept of a REPL (Read-Evaluate-Print-Loop). The REPL allows us to interactively write and execute code in the terminal.

Johnny Five REPL

To start using the REPL, create a repl.js file with the following code:

1
2
const { Board } = require("johnny-five");
const board = new Board();

In this example, we’ll be using an LCD circuit that we created in a previous lesson.

To run the program, use the command node repl.js.

Terminal with REPL

Once the program is running, you can start entering commands in the REPL.

Let’s begin by requiring the LCD class:

1
const { LCD } = require("johnny-five");

LCD Class in REPL

Next, initialize an lcd object using the LCD class:

1
const lcd = new LCD({ pins: [7, 8, 9, 10, 11, 12] });

LCD Object Initialization

Now, we can write to the LCD display. Let’s start with a simple message:

1
lcd.print("Hello!");

You’ll see the message displayed on the LCD:

LCD Hello Message

The lcd.print() command returns a reference to the LCD object, allowing us to chain commands together. For example:

1
lcd.clear().print("Hello!");

This clears the LCD screen before printing the message, ensuring that only the new content is displayed.

To write to the second row of the LCD, you can use the cursor() method. By default, the cursor is on the first row (row 0).

1
2
lcd.clear().print("Hello from");
lcd.cursor(1, 0).print("Johnny-Five!");

LCD Multi-line Text

By using the REPL in Johnny Five, you can interactively experiment with different commands and test your code directly, making the development process more efficient and convenient.