How a Johnny-Five program works
In this section, we'll take a look at the internals of a Johnny-Five program in more detail, so we can start building more complex applications.
Objects, functions, and events
Johnny-Five programs work using an event-based structure, and there are three concepts to keep in mind: objects, functions, and events.
Objects tend to programmatically represent our physical electronic components and are usually constructed using the new
keyword. A few examples of objects in Johnny-Five include the five
object, which represents the Johnny-Five library, the Board
object, which represents our microcontroller; and the LED object, which will programmatically represent our LED:
var led = new five.Led(11);
Functions are available for the objects we create and usually represent the actions that our robots can do. For instance, the LED object has an on()
and off()
function that turns the LED on and off:
led.on(); led.off(); led.blink(); led.stop();
Events are fired on objects...