JavaScript’s Role in Robotics
Before we started learning JavaScript at the Flatiron School, I came across a framework that powers and operates on robots with JavaScript called Johnny-Five. In engineering school I used the Arduino IDE to play with robots, but it was a little difficult to use (especially with minimal coding experience).
Two nights ago I skimmed the surface of Johnny-Five and I quickly found that it was much easier to use than the Arduino IDE. We’re only about a week into learning JavaScript and I can already use the framework with ease. Here’s an example comparing the Arduino IDE with Johnny-Five.
Blinking an LED (the ‘Hello World’ of robotics):

The examples in the Arduino IDE, in my opinion, are both expressive and intuitive. The comments explain exactly what is going on — void setup() is required to set up your environment, and everything in void loop() runs over and over again. You can set an LED to Pin 13 and turn it on using digitalWrite(13, HIGH) and off using digitalWrite(13, LOW). Between turning it on and off, we can wait a certain amount of time using delay(). Again, this is pretty self-explanatory, but let’s take a look at Johnny-Five’s way to blink an LED.

That looks awesome. It’s not only cleaner, but it incorporates fundamentals of Javascript. Using Johnny-Five’s library, we can instantiate a Board object. board.on(“ready”) looks a lot like $(document).ready, and that’s because they’re essentially the same thing! Using this function, we wait until the board is ready, and then execute the callback function. We also create a new led object on pin 13, and instead of using a combination of digitalWrite() and delay(), we can simply execute the blink method on our led object with a phase period of 500 milliseconds.
The blink() method is much easier to use than a sequence of digitalWrite() and delay(), but we still have access to these elementary functions in Johnny-Five. Just like any other framework, Johnny-Five creates an abstraction using these functions to make the code more readable.
Let’s take a look at another example. In this case, we’re going to light up an RGB led with a sequence of colors that resembles a rainbow.

This might look a little intimidating, so let’s break it down. When the board is ready, we create a new RGB led object using Johnny-Five’s library. This type of led requires three pins on the board, so we’re going to assign it to pins 6, 5, and 3. We set our index to (which is essentially a counter) 0 and then define an array of colors that we’re going to use. In the board object, we define a loop which runs every 1000 milliseconds, and define a callback function that lights up the led to the next color in our array, depending on the index.

Doing something like this with the Arduino IDE would be much more difficult, especially for someone with little experience with robotics. The ability to instantiate objects and use object-specific methods, as well as the usage of callback functions and event listeners, makes playing with robots a lot easier for those who are familiar with JavaScript. Even though our class at the Flatiron School is technically Web Development, we can use the languages/skills presented to us in other aspects of engineering, including robotics.