2016-10-25 7 views
0

API johnny-five/arduinoを使用してJavaScriptでどのようにループしますか?このループは、on/offを私の手に遅らせることです。例:遅延がJohnny-fiveで導かれました

実際のコードは次のとおりです。私は最後にhttp and otherslistenというヘッダーを削除します。

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

サーバー機能:上

arduinoのか、ボード

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

}; 

その可能?

答えて

1

あなたは繰り返し遅延のために、単一の遅延またはsetIntervalためsetTimeoutを使用してLEDを遅らせることができます。

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

これは、非同期遅延です。同期遅延が必要な場合は、sleepパッケージを使用するか、ノードchild_processモジュールのexecSyncexecSync("sleep 1.5"))を使用します。

+0

センセーショナル、ありがとうございます! –

関連する問題