Skip to content

Commit a3c16a4

Browse files
committed
Added LED examples
1 parent 8c78784 commit a3c16a4

File tree

5 files changed

+245
-32
lines changed

5 files changed

+245
-32
lines changed

eg/led-functions.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var five = require("johnny-five"),
2+
Spark = require("../lib/spark"),
3+
temporal = require("temporal"),
4+
board;
5+
6+
7+
// Create Johnny-Five board connected via Spark
8+
board = new five.Board({
9+
io: new Spark({
10+
token: process.env.SPARK_TOKEN,
11+
deviceId: process.env.SPARK_DEVICE_ID
12+
})
13+
});
14+
15+
board.on("ready", function() {
16+
var led = new five.Led(process.argv[2] || "D0");
17+
18+
this.repl.inject({
19+
led: led
20+
});
21+
22+
temporal.queue([{
23+
delay: 0,
24+
task: function() {
25+
// on()
26+
//
27+
// Turns the led on
28+
led.on();
29+
console.log("led on");
30+
}
31+
}, {
32+
delay: 1000,
33+
task: function() {
34+
// off()
35+
//
36+
// Turns the led off
37+
led.off();
38+
console.log("led off");
39+
}
40+
}, {
41+
delay: 3000,
42+
task: function() {
43+
// strobe()
44+
//
45+
// Strobe the led (on/off)
46+
led.strobe(1000);
47+
console.log("led strobe");
48+
}
49+
}, {
50+
delay: 5000,
51+
task: function() {
52+
// stop()
53+
//
54+
// Stop the pulse
55+
led.stop();
56+
console.log("led stop");
57+
58+
// If you want to make sure it's off
59+
// in case it stopped it while on
60+
led.off();
61+
}
62+
}, {
63+
delay: 1000,
64+
task: function() {
65+
// fadeIn()
66+
//
67+
// Fade in the led
68+
led.fadeIn(1000);
69+
console.log("led fadeIn");
70+
}
71+
}, {
72+
delay: 3000,
73+
task: function() {
74+
// fadeOut()
75+
//
76+
// Fade out the led
77+
led.fadeOut(1000);
78+
console.log("led fadeOut");
79+
}
80+
}, {
81+
delay: 5000,
82+
task: function() {
83+
// brightness ()
84+
//
85+
// set analog brightness (0-255)
86+
led.brightness(100);
87+
console.log("led brightness");
88+
89+
// Exit gracefully
90+
process.exit(0);
91+
}
92+
}
93+
]);
94+
95+
96+
});
97+
98+
// @markdown
99+
// To make use of `Led` methods like `fade`, `pulse`, `animate`, you'll need to
100+
// wire an LED to a PWM pin (A0, A1, A4, A5, A6, A7, D0 and D1).
101+
// If you use a different pin, make sure to run the script with the correct pin number:
102+
//
103+
// `node eg/led.js [pinNumber]`
104+
// @markdown

eg/led-rgb-keypress.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var five = require("johnny-five"),
2+
Spark = require("../lib/spark"),
3+
keypress = require("keypress"),
4+
board;
5+
6+
7+
// Create Johnny-Five board connected via Spark
8+
board = new five.Board({
9+
io: new Spark({
10+
token: process.env.SPARK_TOKEN,
11+
deviceId: process.env.SPARK_DEVICE_ID
12+
})
13+
});
14+
15+
// The board's pins will not be accessible until
16+
// the board has reported that it is ready
17+
board.on("ready", function() {
18+
console.log("CONNECTED");
19+
20+
21+
// Initialize the RGB LED
22+
var led = new five.Led.RGB({
23+
pins: {
24+
red: "A5",
25+
green: "A6",
26+
blue: "A7"
27+
}
28+
});
29+
30+
// RGB LED alternate constructor
31+
// This will normalize an array of pins in [r, g, b]
32+
// order to an object (like above) that's shaped like:
33+
// {
34+
// red: r,
35+
// green: g,
36+
// blue: b
37+
// }
38+
//var led = new five.Led.RGB(["A5","A6","A7"]);
39+
40+
// Turn it on and set the initial color
41+
led.color("#FF0000");
42+
43+
// Listen for user input to change the RGB color
44+
process.stdin.resume();
45+
process.stdin.setEncoding("utf8");
46+
process.stdin.setRawMode(true);
47+
48+
var keymap = {
49+
r: "#FF0000", // red
50+
g: "#00FF00", // green
51+
b: "#0000FF", // blue
52+
w: "#FFFFFF" // white
53+
};
54+
55+
process.stdin.on("keypress", function (ch, key) {
56+
57+
if ( !key ) {
58+
return;
59+
}
60+
61+
if (keymap[key.name]) {
62+
led.color(keymap[key.name]);
63+
console.log(" ", led.color());
64+
} else {
65+
led.off();
66+
}
67+
68+
});
69+
70+
});
71+
72+
board.on("error", function(error) {
73+
console.log(error);
74+
});

eg/led-rgb-rainbow.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var five = require("johnny-five"),
2+
Spark = require("../lib/spark"),
3+
board;
4+
5+
6+
// Create Johnny-Five board connected via Spark
7+
board = new five.Board({
8+
io: new Spark({
9+
token: process.env.SPARK_TOKEN,
10+
deviceId: process.env.SPARK_DEVICE_ID
11+
})
12+
});
13+
14+
board.on("ready", function() {
15+
var rgb, rainbow, index;
16+
17+
18+
// Initialize the RGB LED
19+
rgb = new five.Led.RGB(["A5", "A6", "A7"]);
20+
rainbow = ["FF000", "FF7F00", "00FF00", "FFFF00", "0000FF", "4B0082", "8F00FF"];
21+
index = 0;
22+
23+
setInterval(function() {
24+
if (index + 1 === rainbow.length) {
25+
index = 0;
26+
}
27+
rgb.color(rainbow[index++]);
28+
}, 1000);
29+
30+
});

eg/led-rgb.js

+4-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var five = require("johnny-five"),
22
Spark = require("../lib/spark"),
3-
keypress = require('keypress'),
43
board;
54

65

@@ -19,7 +18,7 @@ board.on("ready", function() {
1918

2019

2120
// Initialize the RGB LED
22-
var a = new five.Led.RGB({
21+
var led = new five.Led.RGB({
2322
pins: {
2423
red: "A5",
2524
green: "A6",
@@ -35,38 +34,11 @@ board.on("ready", function() {
3534
// green: g,
3635
// blue: b
3736
// }
38-
//var a = new five.Led.RGB(["A5","A6","A7"]);
37+
//var led = new five.Led.RGB(["A5","A6","A7"]);
3938

4039
// Turn it on and set the initial color
41-
a.on();
42-
a.color("#FF0000");
43-
44-
// Listen for user input to change the RGB color
45-
process.stdin.resume();
46-
process.stdin.setEncoding('utf8');
47-
process.stdin.setRawMode(true);
48-
49-
var keymap = {
50-
r: "#FF0000", // red
51-
g: "#00FF00", // green
52-
b: "#0000FF", // blue
53-
w: "#FFFFFF" // white
54-
};
55-
56-
process.stdin.on('keypress', function (ch, key) {
57-
58-
if ( !key ) {
59-
return;
60-
}
61-
62-
if (keymap[key.name]) {
63-
a.color(keymap[key.name]);
64-
a.on();
65-
} else {
66-
a.off();
67-
}
68-
69-
});
40+
led.color("#FF0000");
41+
led.strobe(1000);
7042

7143
});
7244

eg/led.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var five = require("johnny-five"),
2+
Spark = require("../lib/spark"),
3+
board;
4+
5+
6+
// Create Johnny-Five board connected via Spark
7+
board = new five.Board({
8+
io: new Spark({
9+
token: process.env.SPARK_TOKEN,
10+
deviceId: process.env.SPARK_DEVICE_ID
11+
})
12+
});
13+
14+
// The board's pins will not be accessible until
15+
// the board has reported that it is ready
16+
board.on("ready", function() {
17+
console.log("CONNECTED");
18+
19+
20+
// Initialize the LED
21+
var led = new five.Led("A5");
22+
23+
board.repl.inject({
24+
led: led
25+
});
26+
27+
led.blink(1000);
28+
29+
});
30+
31+
board.on("error", function(error) {
32+
console.log(error);
33+
});

0 commit comments

Comments
 (0)