-
Notifications
You must be signed in to change notification settings - Fork 2
/
04_switch_with_twitter.js
55 lines (50 loc) · 1.29 KB
/
04_switch_with_twitter.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
var j5 = require('johnny-five'),
Twitter = require('ntwitter'),
arduino = new j5.Board();
var texts = ['|ω・)チカッ',
'ピコーン',
'/^o^\フッジッサーン'];
var client = new Twitter({
consumer_key : 'API key',
consumer_secret : 'API secret',
access_token_key : 'Access token',
access_token_secret: 'Access token secret'
});
arduino.on('ready', function(){
var i = 0;
var led = new j5.Led({
// the pin to apply a voltage.
pin: 13
});
var button = new j5.Button({
// input pin
pin: 2,
// minimum time to emit 'hold' event.
holdtime: 500
});
button.on('up', function(){
led.toggle();
if (led.isOn) {
tweet();
}
});
/**
* utility functions
*/
function tweet(){
client.updateStatus(generateText(), function(err, res){
if (err) {
console.log(err);
return;
}
});
updateIndex();
}
function generateText(){
// add Datetime to avoid error code 187, 'Status is a duplicate'
return (texts[i] + '\n' + (new Date()).toString());
}
function updateIndex(){
i = (i + 1) % texts.length;
}
});