In this example consider the function setTimeout. setTimeout sets a timer which executes a function or specified piece of code
once after the timer expires. In our case, we will put pin 13 to LOW after 1000ms:
board.digitalWrite(13, board.HIGH); // write high on pin 13 timer1 = setTimeout(function () {board.digitalWrite(13, board.LOW);}, 1000);Clearing the timer:
clearTimeout(timer1);var http = require("http").createServer(handler); // on req - handler - function to handle eventsvar io = require("socket.io").listen(http); // socket libraryvar fs = require("fs"); // variable for file system for providing htmlvar firmata = require("firmata");console.log("Starting the code");var board = new firmata.Board("/dev/ttyACM0", function(){ console.log("Connecting to Arduino"); console.log("Activation of Pin 13"); board.pinMode(13, board.MODES.OUTPUT); // pin13 as out});// we should define handler function which is an argument in the first line// to specify how events are handled// handler is similar function as in example01.js -> http.createServer(function(req, res){...function handler(req, res) { fs.readFile(__dirname + "/assignment07.html", function (err, data) { if (err) { res.writeHead(500, {"Content-Type": "text/plain"}); return res.end("Error loading html page."); } res.writeHead(200); res.end(data); })}var timer1;http.listen(8080); // server will listen on port 8080io.sockets.on("connection", function(socket) { socket.on("commandToArduino", function(commandNo){ if (commandNo == "1") { board.digitalWrite(13, board.HIGH); // write high on pin 13 } if (commandNo == "0") { board.digitalWrite(13, board.LOW); // write low on pin 13 } }); socket.on("blinkOn", function(){ board.digitalWrite(13, board.HIGH); // write high on pin 13 timer1 = setTimeout(function () {board.digitalWrite(13, board.LOW);}, 1000); }); socket.on("blinkOff", function(){ board.digitalWrite(13, board.LOW); clearTimeout(timer1); });});<!DOCTYPE html><meta charset = utf8><html><head> <title>Example with buttons</title></head><body><button id="buttonOn" style="width: 80px;" onClick="on()">On</button><button id="buttonOff" style="width: 80px;" onClick="off()">Off</button><br><button id="buttonBlinkOn" style="width: 80px;" onClick="blinkOn()">B On</button><button id="buttonBlinkOff" style="width: 80px;" onClick="blinkOff()">B Off</button><br><script type="text/javascript" src="/socket.io/socket.io.js"></script><script type="text/javascript">var socket = io.connect("192.168.254.133:8080"); // connect via socketfunction on () { socket.emit("commandToArduino", 1);}function off () { socket.emit("commandToArduino", 0);}function blinkOn () { socket.emit("blinkOn");}function blinkOff () { socket.emit("blinkOff");} </script> </body> </html>The gui should look like this (we use style on buttons: style="width: 80px;"):