-
Notifications
You must be signed in to change notification settings - Fork 3
/
artbot.js
96 lines (81 loc) · 1.72 KB
/
artbot.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var five = require('johnny-five');
var board, lServo, rServo,
moveSpeed = 0.1;
var moves = {
fwd: function (timeout) {
// console.log("fwd")
return turn(true, true, timeout);
},
bkwd: function (timeout) {
// console.log("bkwd")
return turn(false, false, timeout);
},
left: function (timeout) {
// console.log("left")
return turn(true, false, timeout);
},
right: function (timeout) {
// console.log("right")
return turn(false, true, timeout);
},
stop: function () {
// console.log("stop")
lServo.stop();
rServo.stop();
}
}
function turn (leftOn, rightOn, timeout) {
if (leftOn) {
lServo.ccw(moveSpeed);
} else {
lServo.cw(moveSpeed);
}
if (rightOn) {
rServo.cw(moveSpeed);
} else {
rServo.ccw(moveSpeed);
}
if (timeout) {
setTimeout(moves.stop, timeout)
}
}
module.exports = function ArtBot (io) {
board = new five.Board();
board.on('ready', function () {
var led = new five.Led(13);
led.off();
lServo = new five.Servo({
pin: 11,
type: 'continuous'
});
rServo = new five.Servo({
pin: 10,
type: 'continuous'
});
marker = new five.Servo({
pin: 9
});
moves.stop();
marker.center();
this.repl.inject({
marker: marker,
led: led
});
io.on('connection', function (socket) {
socket.on('move', function (data) {
// console.log(data.direction)
moves[data.direction](600);
});
socket.on('draw', function (drawOn) {
// console.log('draw? ', drawOn);
if (drawOn) {
led.on();
marker.min();
} else {
led.off();
marker.to(30);
}
})
});
})
}