Created
June 6, 2014 22:18
-
-
Save Resseguie/88b2d8331c99c2a2380a to your computer and use it in GitHub Desktop.
led demo sequence example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var five = require("johnny-five"), | |
board = new five.Board(), | |
led; | |
var demoSequence = [{ | |
method: "pulse", | |
args: [1000], | |
duration: 5000 | |
}, { | |
method: "strobe", | |
args: [500], | |
duration: 3000 | |
}, { | |
method: "fadeIn", | |
args: [ | |
2000, | |
function(){ console.log("fadeIn complete!")} | |
], | |
duration: 2500 | |
}, { | |
method: "fadeOut", | |
args: [ | |
5000, | |
function(){ console.log("fadeOut complete!")} | |
], | |
duration: 5500 | |
}, { | |
method: "brightness", | |
args: [10], | |
duration: 3000 | |
}, { | |
method: "off" | |
}]; | |
// Execute a method in the demo sequence | |
function execute(step){ | |
var method = demoSequence[step].method; | |
var args = demoSequence[step].args; | |
var duration = demoSequence[step].duration || 3000; | |
// Just print out what we're executing | |
console.log( "led." + method + "("+ ( args ? args.join() : "" ) + ")" ); | |
// Make the actual call to the LED | |
five.Led.prototype[method].apply( led, args ); | |
// Increment the step | |
step++; | |
if(step == demoSequence.length){ | |
// We're done! | |
process.exit(0); | |
} | |
// Recursively call the next step after specified duration | |
board.wait(duration,function(){ | |
execute(step); | |
}); | |
} | |
board.on("ready", function() { | |
// Defaults to pin 11 (must be PWM) | |
led = new five.Led(process.argv[2] || 11); | |
// Kick off the first step | |
execute(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is mostly a nit-pick: this isn't actually a recursive call since
execute
is not actually the function that makes the call to itself, the callback to board.wait owns that. You should change this to something like:"Schedule the next demo to be called from a later execution turn"