diff --git a/eg/led-rainbow.js b/eg/led-rainbow.js new file mode 100644 index 0000000..300835b --- /dev/null +++ b/eg/led-rainbow.js @@ -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); + +}); diff --git a/eg/led-rgb.js b/eg/led-rgb.js index c0892fb..b2534d8 100644 --- a/eg/led-rgb.js +++ b/eg/led-rgb.js @@ -1,6 +1,6 @@ var five = require("johnny-five"), Spark = require("../lib/spark"), - keypress = require('keypress'), + keypress = require("keypress"), board; @@ -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", @@ -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 = { @@ -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(); } }); diff --git a/eg/led.js b/eg/led.js new file mode 100644 index 0000000..09ac5d5 --- /dev/null +++ b/eg/led.js @@ -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