Skip to content

Commit

Permalink
Added LED examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Resseguie committed Sep 18, 2014
1 parent 8c78784 commit a3c16a4
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 32 deletions.
104 changes: 104 additions & 0 deletions eg/led-functions.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(1000);
console.log("led strobe");
}
}, {
delay: 5000,
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(1000);
console.log("led fadeIn");
}
}, {
delay: 3000,
task: function() {
// fadeOut()
//
// Fade out the led
led.fadeOut(1000);
console.log("led fadeOut");
}
}, {
delay: 5000,
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
74 changes: 74 additions & 0 deletions eg/led-rgb-keypress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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
})
});

// The board's pins will not be accessible until
// the board has reported that it is ready
board.on("ready", function() {
console.log("CONNECTED");


// Initialize the RGB LED
var led = new five.Led.RGB({
pins: {
red: "A5",
green: "A6",
blue: "A7"
}
});

// RGB LED alternate constructor
// This will normalize an array of pins in [r, g, b]
// order to an object (like above) that's shaped like:
// {
// red: r,
// green: g,
// blue: b
// }
//var led = new five.Led.RGB(["A5","A6","A7"]);

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

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

var keymap = {
r: "#FF0000", // red
g: "#00FF00", // green
b: "#0000FF", // blue
w: "#FFFFFF" // white
};

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

if ( !key ) {
return;
}

if (keymap[key.name]) {
led.color(keymap[key.name]);
console.log(" ", led.color());
} else {
led.off();
}

});

});

board.on("error", function(error) {
console.log(error);
});
30 changes: 30 additions & 0 deletions eg/led-rgb-rainbow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var five = require("johnny-five"),
Spark = require("../lib/spark"),
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++]);
}, 1000);

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


Expand All @@ -19,7 +18,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,38 +34,11 @@ 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");

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

var keymap = {
r: "#FF0000", // red
g: "#00FF00", // green
b: "#0000FF", // blue
w: "#FFFFFF" // white
};

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

if ( !key ) {
return;
}

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

});
led.color("#FF0000");
led.strobe(1000);

});

Expand Down
33 changes: 33 additions & 0 deletions eg/led.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var five = require("johnny-five"),
Spark = require("../lib/spark"),
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
})
});

// The board's pins will not be accessible until
// the board has reported that it is ready
board.on("ready", function() {
console.log("CONNECTED");


// Initialize the LED
var led = new five.Led("A5");

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

led.blink(1000);

});

board.on("error", function(error) {
console.log(error);
});

0 comments on commit a3c16a4

Please sign in to comment.