gpt4 book ai didi

javascript - 约翰尼五号延迟导致

转载 作者:行者123 更新时间:2023-12-03 05:48:05 25 4
gpt4 key购买 nike

如何使用 API johnny- Five/arduino 在 javascript 中循环。这个循环是为了延迟我的 LED 的开/关。示例:

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

}

我的实际代码如下。我删除了 header http 和其他 以及最后的 listen

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 延迟 LED 的亮起对于单个延迟或 setInterval对于重复延迟:

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

这是一个异步延迟。如果需要同步延迟,可以使用sleep包装或使用execSync来自节点child_process模块:execSync(" sleep 1.5")

关于javascript - 约翰尼五号延迟导致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40251458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com