-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.js
More file actions
87 lines (69 loc) · 1.54 KB
/
Copy pathbot.js
File metadata and controls
87 lines (69 loc) · 1.54 KB
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
var five = require("johnny-five");
var Particle = require("particle-io");
var keydownup = require("keyupdown");
// Store process.stdin to a local variable
var stdin = process.stdin;
// Init the keydown and keyup events
keydownup(stdin);
var board = new five.Board({
io: new Particle({
token: '7ae40abd0f7c272888ca95b6740667febd1a29c8',
deviceName: 'cat_mountain'
})
});
board.on("ready", function() {
console.log('ready');
var rightWheel = new five.Motor({
pins: { pwm: "D0", dir: "D4" },
invertPWM: true
});
var leftWheel = new five.Motor({
pins: { pwm: "D1", dir: "D5" },
invertPWM: true
});
var speed = 255;
function reverse() {
leftWheel.rev(speed);
rightWheel.rev(speed);
}
function forward() {
leftWheel.fwd(speed);
rightWheel.fwd(speed);
}
function stop() {
leftWheel.stop();
rightWheel.stop();
}
function left() {
leftWheel.rev(speed);
rightWheel.fwd(speed);
}
function right() {
leftWheel.fwd(speed);
rightWheel.rev(speed);
}
function exit() {
leftWheel.rev(0);
rightWheel.rev(0);
setTimeout(process.exit, 1000);
}
var keyMap = {
'up': forward,
'down': reverse,
'left': left,
'right': right,
'space': stop,
'q': exit,
'c': console.clear
};
stdin.setRawMode(true);
stdin.resume();
stdin.on("keypress", function(chunk, key) {
if (!key || !keyMap[key.name]) return;
keyMap[key.name]();
});
// Stop when no keys are being pressed
stdin.on("keyup", function() {
stop();
});
});