导致延迟 Johnny-five

Delay led in Johnny-five

如何使用 API johnny-five/arduino 在 javascript 中循环。这个循环是为了延迟 on/off 到我的 led。示例:

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

}

我的实际代码如下。我最后删除了 header http and otherslisten

arduino 或开发板:

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);
  });
});

服务器函数:

  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>");
  }

};

可能吗?

您可以使用 setTimeout for a single delay or setInterval 重复延迟 LED:

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

这是一个异步延迟。如果需要同步延迟,可以使用 sleep package or use execSync from the Node child_process 模块:execSync("sleep 1.5").