Objects usually represent physical electronic components. Some examples of an object in Johnny-Five would be the five object, which represents the Johnny-Five Library, the board object, which represents the microcontroller, and the LED object, which represents the physical LED.
Functions represent the actions your objects (and therefore the robot) can do. Examples of a function would be .off(), .on(), etc.
Events are fired on objects and represent observable points in a program. An example is when a board object fires a ready event when it’s ready to receive instructions. In Johnny-Five, and with robots in general, event-based programming is the best type of programming, as opposed to loop-based or interrupt-based programming. This means that instead of saying “every [A] seconds, I want to check for [B] and start [C]”, programming using the format “When [X] happens, I want [Y] to happen”. This type of programming makes it easier to write robotic programs – especially for beginners!
Basic Format for Most Johnny-Five Scripts
- Create a board object
- Create a listener and handler for ready event
- (Within the ready event handler) create component objects and call functions through them