-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.js
94 lines (82 loc) · 2.46 KB
/
test2.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
var http = require('http');
var server = http.createServer();
var tessel = require('tessel');
var servolib = require('servo-pca9685');
var camera = require('camera-vc0706').use(tessel.port['A']);
var servo = servolib.use(tessel.port['D']);
var servo1 = 1; // We have a servo plugged in at position 1
var ambientlib = require('ambient-attx4');
var ambient = ambientlib.use(tessel.port['C']);
var request = require('request');
require('tesselate')({
modules: {
A: ['camera-vc0706', 'camera'],
C: ['ambient-attx4', 'ambient'],
D: ['servo-pca9685', 'servo']
},
development: true,
}, function(tessel, m){
var notificationLED = tessel.led[3];
notificationLED.high();
servo.configure(servo1, 0.05, 0.50, function () {
server.listen(1337, function () {
console.log('Server listening!');
// setTimeout(function() {
// app.post('/', function (req, res)) {
// res.json('payload={"channel": "#cereal", "username": "webhookbot", "text": "This is posted to #cereal and comes from a bot named webhookbot.", "icon_emoji": ":ghost:"}');
// }
// }, 3000);
});
});
server.on('request', function (req, res) {
// servo.move(servo1, 0);
var count = 10;
var turn = function(){
if (count == 0) {
count = 10;
return 0;
} else if (count % 2 == 0){
count -= 1;
servo.move(servo1, 1);
return setTimeout(turn, 1500);
} else if (count % 2 == 1){
count -= 1;
servo.move(servo1, 0);
return setTimeout(turn, 1500)
}
};
if (req.url === '/high') {
turn();
}
else if (req.url === '/med') {
console.log('got med req')
servo.move(servo1, 0.7);
setTimeout(function() { servo.move(servo1, 0) }, 2000);
}
else if (req.url === '/low') {
console.log('got low req')
servo.move(servo1, 0.4);
setTimeout(function() { servo.move(servo1, 0) }, 2000);
} else if (req.url === "/picture") {
console.log("taking picture");
camera.takePicture(function(err, image) {
if (err) {
console.log('error taking image', err);
} else {
notificationLED.low();
// Name the image
var name = 'picture-' + Math.floor(Date.now()*1000) + '.jpg';
// Save the image
console.log('Picture saving as', name, '...');
process.sendfile(name, image);
console.log('done.');
// Turn the camera off to end the script
camera.disable();
}
});
}
});
servo.on('error', function(err){
console.log(err);
})
})