-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
53 lines (45 loc) · 1.19 KB
/
index.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
var five = require('johnny-five');
var player = require('./monstersound/index');
var HeadMovements = require('./commands/head-movements');
var board = new five.Board();
var HEAD = {UP: 90, DOWN: 120};
var JAW = {OPEN: 60, CLOSED: 170};
var DISTANCE_TRESHOLD = 20;
var distance = 1;
var skullMoving = false;
var isObjectClose = function(objectDistance, previousDistance) {
var isClose = objectDistance <= DISTANCE_TRESHOLD;
return isClose;
};
board.on('ready', function() {
var proximity = new five.Proximity({
controller: 'HCSR04',
pin: 11
});
var eye = new five.Led(6);
var head = new five.Servo(10);
var jaw = new five.Servo(9);
this.repl.inject({
head: head,
jaw: jaw,
proximity: proximity
});
head.to(HEAD.DOWN);
jaw.to(JAW.CLOSED);
proximity.on('data', function() {
console.log(this.cm);
if (isObjectClose(this.cm, distance) && !skullMoving) {
console.log('go lauging');
distance = this.cm;
skullMoving = true;
eye.on();
player.play();
HeadMovements.laugh(head, jaw, function() {
console.log('Done lauging bitch');
eye.off();
player.stop();
skullMoving = false;
});
}
});
});