-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample11.js
71 lines (56 loc) · 1.79 KB
/
example11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var http = require("http").createServer(handler);
var firmata = require("firmata");
var fs = require("fs");
var io = require("socket.io").listen(http);
console.log("Connecting to Arduino");
var board = new firmata.Board("/dev/ttyACM0", function(){
console.log("Activation of pin 2");
board.pinMode(2, board.MODES.OUTPUT);
console.log("Activation of pin 3");
board.pinMode(3, board.MODES.PWM);
console.log("Enabling analog Pin 0");
board.pinMode(0, board.MODES.ANALOG);
});
function handler(req, res) {
fs.readFile(__dirname + "/example11.html",
function(err, data) {
if (err) {
res.writeHead(500, {"Content-Type": "text/plain"});
return res.end("You didn't connect html file!");
}
res.writeHead(200);
res.end(data);
});
}
http.listen(8080);
var desiredValue = 0;
console.log("Running the system");
board.on("ready", function(){
console.log("Board is ready!");
board.analogRead(0, function(value){
desiredValue = value;
});
io.sockets.on("connection", function(socket){
socket.on("sendPWM", function(pwm){
board.analogWrite(3,pwm);
console.log("PWM value:" + pwm);
});
socket.on("left", function(value) {
board.digitalWrite(2,value);
});
socket.on("right", function(value) {
board.digitalWrite(2,value);
});
socket.on("stop", function(value) {
board.analogWrite(3,value);
});
socket.emit("messageToClient", "Server connected, board ready.");
setInterval(sendValues, 40, socket); // na 40ms we send message to client
});
function sendValues (socket) {
socket.emit("clientReadValues",
{
"desiredValue": desiredValue
});
};
});