Eric Saboia Eric Saboia - 1 month ago 8
Javascript Question

Delay led in Johnny-five

How do I a loop in javascript with API

johnny-five/arduino
. This looping is to delay
on/off
to my led. Example:

while(true){
while(time<1500){
'led off'
}
while(time<1500){
'led on'
}

}


My actual code is bellow. I remove the header
http and others
and the
listen
in the end.

arduino or board on:

arduino.on('ready', function(){
console.log("Arduino Pronto");

led0 = new five.Led(13);
led1 = new five.Led(12);
led2 = new five.Led(11);

this.digitalRead(10, function(value) {
console.log(value);
});
});


Server function:

function servidor(req, res){
var url = req.url;
if(url == '/'){
res.writeHead(200);
res.end(fs.readFileSync('view/index.html'));
}else if(url == '/led0'){
res.writeHead(302, {'Location': '/'});
res.end();
led0.toggle();
}else if(url == '/led1'){
res.writeHead(302, {'Location': '/'});
res.end();
led1.toggle();
}else if(url == '/led2'){
res.writeHead(302, {'Location': '/'});
res.end();
led2.toggle();
}else{
res.writeHead(200);
res.end("<h1>Erro 404</h1>");
}

};


Its possible?

Answer

You can delay your LEDs using setTimeout for a single delay or setInterval for a repeated delay:

setInterval(() => {
  led.toggle();
}, 1500);

This is an asynchronous delay. If you need a synchronous delay, you could use the sleep package or use execSync from the Node child_process module: execSync("sleep 1.5").

Comments