-
Notifications
You must be signed in to change notification settings - Fork 1
/
pi-client.js
45 lines (40 loc) · 1.19 KB
/
pi-client.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
var express = require("express");
var app = express();
var server = require("http").Server(app);
var io = require("socket.io-client")("http://eye-of-sauron.herokuapp.com", {reconnect: true});
var piblaster = require("pi-blaster.js");
var position = 0.15;
var key = "SERVO_KEY";
io.on("connect", function() {
piblaster.setPwm(17, position);
console.log("Servo Key: " + key);
console.log("Resetting servo position");
io.on("PiServo", function(data) {
if(data.servoName === key) {
move(data.direction, data.speed);
}
});
});
server.listen((process.env.PORT || 2345), function() {
console.log("App started!");
});
function move(direction, speed) {
if(typeof(speed) === "undefined") {
speed = 0.01
}
speed = parseFloat(speed);
if(speed > 0.01) {
speed = 0.01
}
if(direction == "right" && position > 0.06) {
console.log("Going right!");
position =+ parseFloat((position -= speed).toFixed(5));
} else if(direction == "left" && position < 0.23) {
console.log("Going left!");
position =+ parseFloat((position += speed).toFixed(5));
} else if(direction == "centre") {
console.log("Centreing!");
position = 0.15;
}
piblaster.setPwm(17, position);
}