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.
To start using the REPL, create a repl.js
file with the following code:
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
.
Once the program is running, you can start entering commands in the REPL.
Let’s begin by requiring the LCD class:
const { LCD } = require("johnny-five");
Next, initialize an lcd
object using the LCD class:
const lcd = new LCD({ pins: [7, 8, 9, 10, 11, 12] });
Now, we can write to the LCD display. Let’s start with a simple message:
lcd.print("Hello!");
You’ll see the message displayed on the LCD:
The lcd.print()
command returns a reference to the LCD object, allowing us to chain commands together. For example:
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).
lcd.clear().print("Hello from");
lcd.cursor(1, 0).print("Johnny-Five!");
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.