sony8943
Posts: 8
Joined: Mon Apr 16, 2018 12:30 pm

use sleep() or alternate timers in NodeJS

Tue Apr 23, 2019 5:34 am

Hello i want to insert delay or sleep time between the gpio pin but when i pres V1 appears this error on terminal.

how to put the proper command delay or sleep time


var Blynk = require('blynk-library');

var Gpio = require('onoff').Gpio;

var led1 = new Gpio(27, 'out');

var led2 = new Gpio(18, 'out');

var led3 = new Gpio(17, 'out');

var AUTH = 'e3bacfb60e0b4305854082b499cc2df9';

var blynk = new Blynk.Blynk(AUTH);

var v1 = new blynk.VirtualPin(1);

v1.on('write', function(param) {

if (param[0] == '1') {
led1.writeSync(1);
2000
led1.writeSync(0);
1000
led2.writeSync(1);
1000
led2.writeSync(0);
1000
led3.writeSync(1);
1000
led3.writeSync(0);
}else{
led1.writeSync(1);
2000
led1.writeSync(0);

}

console.log('V1:', param[0]);
});


and the error is apear when i pres V1

Give Blynk a Github star! => https://github.com/vshymanskyy/blynk-library-js

OnOff mode
Connecting to: blynk-cloud.com 443
SSL authorization...
Connected
Authorized
/home/pi/my-awesome-project/index2.js:22
delay (2000) ;
^

ReferenceError: delay is not defined
at Blynk.VirtualPin.<anonymous> (/home/pi/my-awesome-project/index2.js:22:5)
at emitOne (events.js:116:13)
at Blynk.VirtualPin.emit (events.js:211:7)
at Blynk.onReceive (/home/pi/my-awesome-project/node_modules/blynk-library/blynk.js:510:27)
at exports.SslClient.<anonymous> (/home/pi/my-awesome-project/node_modules/blynk-library/blynk.js:607:50)
at emitOne (events.js:116:13)
at exports.SslClient.emit (events.js:211:7)
at TLSSocket.<anonymous> (/home/pi/my-awesome-project/node_modules/blynk-library/blynk-node.js:212:16)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)

Heater
Posts: 15837
Joined: Tue Jul 17, 2012 3:02 pm

Re: use sleep() or alternate timers in NodeJS

Tue Apr 23, 2019 7:30 am

I'm afraid you cannot simply insert a delay between statements in Javascript like that.

Javascript uses an event driven programming model. Basically that means that functions get called when events happen. Those functions should do what they have to do as quickly as possible and then return. That means no endless loops inside functions, no delays etc. Such delays would hang up the entire program upsetting other things it has to do, like communicating with blinky.

For example:

You want to have something happen after one second. That means you need to create an event that will happen one second into the future which will call some function to do that something. This can be done with setInterval(). For example

Code: Select all

console.log("Now")
setTimeout(function () {
    console.log("1 second later")
}, 1000)
When you want many things to happen over some time steps you can use setInterval instead.

Have a look at this:

Code: Select all

function doThingsInTimeSteps(callBack) {
    let state = 0

    let t = setInterval(function () {
        switch(state) {
        case 0:
            console.log('State: ', state)
            led1.writeSync(1);
            state = 1
            break;
        case 1:
            console.log('State: ', state)
            led1.writeSync(0);
            state = 2
            break;
        case 2:
            console.log('State: ', state)
            led1.writeSync(1);
            state = 3
            break;
        case 3:
            console.log('State: ', state)
            led1.writeSync(0);
            state = 4
            break;
        case 4:
            console.log('State: ', state)
            led1.writeSync(1);
            state = 5
            break;
        case 5:
            console.log('State: ', state)
            led1.writeSync(0);
            clearInterval(t)
            callBack(false)
            break;
        default:
            console.log('Invalid state')
            callBack(true)
        }    
    }, 1000)
}

doThingsInTimeSteps(function(err) {
    if (err) {
        return console.log("dothingsInTimeSteps ERROR")
    }
    console.log("dothingsInTimeSteps OK")
});
Here setInterval is used to run a function every second. That function decides what to do from the value of the "state" variable by using a switch statement. Every second it does a different thing. Having done that thing it sets the state variable to indicate whatever it should do next time it is called (after another second) and then returns. When it has done all the 5 steps it cancels the timer with "clearInterval" and calls the callback function do indicate if any error happened.

Of course one could set the next value of the state variable to whatever one likes at each step, perhaps depending on some condition, in this way all kind of complex sequences can be built up easily. The sequence of states could be set back to zero, then the sequence would repeat all over again.
Memory in C++ is a leaky abstraction .

Return to “General discussion”