ad space

Arduino Johnny-Five wait() vs loop()

 Johnny-Five is a popular JavaScript library for controlling Arduino hardware with Node.js. It provides two functions, wait() and loop(), for managing timing in your programs. In this Johnny-Five javascript tutorial we want to highlight the main difference between these two timing functions. 

In the Johnny-Five library for Node.js, wait() and loop() are two different functions that can be used for scheduling tasks or controlling the timing of operations in a JavaScript program interacting with Arduino hardware.

  1. wait(): The wait() function in Johnny-Five is used to introduce a delay or pause in the execution of the program for a specified amount of time, measured in milliseconds (ms). It allows you to pause the program and wait for a certain duration before continuing with the next line of code. It is a one-time delay and the program will resume execution after the specified time has passed.

Here's an example of using wait() in Johnny-Five:

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // Execute code after 1 second (1000ms) delay
  this.wait(1000, function() {
    console.log("Waited for 1 second.");
  });
});

The following shows execution of the above johnny-five wait() function in visual studio code IDE.

wait johnny five

  1. loop(): The loop() function in Johnny-Five is used to repeatedly execute a function or a block of code with a specified interval between each execution. It allows you to create a loop that continues to execute the specified code until stopped. It is similar to the setInterval() function in JavaScript.

Here's an example of using loop() in Johnny-Five:

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
  // Execute code every 1 second (1000ms)
  this.loop(1000, function() {
    console.log("Looping every 1 second.");
  });
});

The following shows execution of the above johnny-five loop() function in visual studio code IDE.

loop johnny-five

In summary, wait() is used for a one-time delay or pause in the program, while loop() is used for repeatedly executing code at a specified interval. Both functions are useful for managing timing and controlling the flow of your Johnny-Five programs.

 Learn javascript programming language for Arduino hardware programming with following tutorials.

[1]  Programming Arduino with Johnny-Five in Proteus

[2]  Getting Started with Johnny-Five in Proteus

Post a Comment

Previous Post Next Post