Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional LED examples #31

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions eg/led-rainbow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var five = require("johnny-five"),
Spark = require("../lib/spark"),
keypress = require("keypress"),
board;


// Create Johnny-Five board connected via Spark
board = new five.Board({
io: new Spark({
token: process.env.SPARK_TOKEN,
deviceId: process.env.SPARK_DEVICE_ID
})
});

board.on("ready", function() {
var rgb, rainbow, index;


// Initialize the RGB LED
rgb = new five.Led.RGB(["A5", "A6", "A7"]);
rainbow = ["FF000", "FF7F00", "00FF00", "FFFF00", "0000FF", "4B0082", "8F00FF"];
index = 0;

setInterval(function() {
if (index + 1 === rainbow.length) {
index = 0;
}
rgb.color(rainbow[index++]);
}, 500);

});
23 changes: 11 additions & 12 deletions eg/led-rgb.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var five = require("johnny-five"),
Spark = require("../lib/spark"),
keypress = require('keypress'),
keypress = require("keypress"),
board;


Expand All @@ -19,7 +19,7 @@ board.on("ready", function() {


// Initialize the RGB LED
var a = new five.Led.RGB({
var led = new five.Led.RGB({
pins: {
red: "A5",
green: "A6",
Expand All @@ -35,15 +35,15 @@ board.on("ready", function() {
// green: g,
// blue: b
// }
//var a = new five.Led.RGB(["A5","A6","A7"]);
//var led = new five.Led.RGB(['A5','A6','A7']);

// Turn it on and set the initial color
a.on();
a.color("#FF0000");
led.on();
led.color("#FF0000");

// Listen for user input to change the RGB color
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setEncoding("utf8");
process.stdin.setRawMode(true);

var keymap = {
Expand All @@ -53,17 +53,16 @@ board.on("ready", function() {
w: "#FFFFFF" // white
};

process.stdin.on('keypress', function (ch, key) {
if ( !key ) {
process.stdin.on("keypress", function(ch, key) {

if (!key) {
return;
}

if (keymap[key.name]) {
a.color(keymap[key.name]);
a.on();
led.color(keymap[key.name]);
} else {
a.off();
led.off();
}

});
Expand Down
104 changes: 104 additions & 0 deletions eg/led.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var five = require("johnny-five"),
Spark = require("../lib/spark"),
temporal = require("temporal"),
board;


// Create Johnny-Five board connected via Spark
board = new five.Board({
io: new Spark({
token: process.env.SPARK_TOKEN,
deviceId: process.env.SPARK_DEVICE_ID
})
});

board.on("ready", function() {
var led = new five.Led(process.argv[2] || "D0");

this.repl.inject({
led: led
});

temporal.queue([{
delay: 0,
task: function() {
// on()
//
// Turns the led on
led.on();
console.log("led on");
}
}, {
delay: 1000,
task: function() {
// off()
//
// Turns the led off
led.off();
console.log("led off");
}
}, {
delay: 3000,
task: function() {
// strobe()
//
// Strobe the led (on/off)
led.strobe();
console.log("led strobe");
}
}, {
delay: 3000,
task: function() {
// stop()
//
// Stop the pulse
led.stop();
console.log("led stop");

// If you want to make sure it's off
// in case it stopped it while on
led.off();
}
}, {
delay: 1000,
task: function() {
// fadeIn()
//
// Fade in the led
led.fadeIn();
console.log("led fadeIn");
}
}, {
delay: 3000,
task: function() {
// fadeOut()
//
// Fade out the led
led.fadeOut();
console.log("led fadeOut");
}
}, {
delay: 3000,
task: function() {
// brightness ()
//
// set analog brightness (0-255)
led.brightness(100);
console.log("led brightness");

// Exit gracefully
process.exit(0);
}
}
]);


});

// @markdown
// To make use of `Led` methods like `fade`, `pulse`, `animate`, you'll need to
// wire an LED to a PWM pin (A0, A1, A4, A5, A6, A7, D0 and D1).
// If you use a different pin, make sure to run the script with the correct pin number:
//
// `node eg/led.js [pinNumber]`
// @markdown