Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created November 9, 2013 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rwaldron/7388783 to your computer and use it in GitHub Desktop.
Save rwaldron/7388783 to your computer and use it in GitHub Desktop.
var j5 = require("johnny-five"),
board = new j5.Board();
board.on("ready", function () {
var ping, button, lights, ginches, lowerLimit, offset;
ping = new j5.Ping({
pin: 8,
freq: 250
});
button = new j5.Button({
pin: 7,
holdtime: 500,
invert: true
});
[
{ color: "red", pin: 3 },
{ color: "yellow", pin: 2 },
{ color: "green", pin: 4 }
].forEach(function(light) {
lights[light.color] = new j5.Led(light.pin);
});
// Create a reference to all of the initialized Leds
lights.all = new Leds();
ginches = 20;
button.on("hold", function () {
offset = ginches;
console.log("offset = " + offset);
});
lowerLimit = 2;
offset = 5;
ping.on("change", function (err, value) {
ginches = this.inches;
// Each condition turns on or off all three lights, to save operations,
// just shut them all off here and only invoke the "on" states in the
// condition statement bodies.
lights.all.off();
// flashing red
if (this.inches < (lowerLimit + offset)) {
console.log("flashing red");
lights.red.strobe(200);
//goto end
}
// solid red
else if (this.inches > lowerLimit + offset && this.inches < 6 + offset) {
console.log("red");
lights.red.stop().on();
}
// red and yellow
else if (this.inches > 6 + offset && this.inches < 10 + offset) {
console.log("red and yellow");
lights.red.stop().on();
lights.yellow.on();
}
// yellow
else if (this.inches > 10 + offset && this.inches < 20 + offset) {
console.log("yellow");
lights.yellow.on();
}
// green and yellow
else if (this.inches > 20 + offset && this.inches < 35 + offset) {
console.log("green and yellow");
lights.green.on();
lights.yellow.on();
}
// green
// if (this.inches > 35 + offset) {
else if (this.inches > 55 + offset) {
console.log("green");
lights.green.on();
}
});
// Inject the lights object for REPL control
this.repl.inject({
lights: lights
});
console.log("You can interact with the RGB LED via the variable 'led' e.g. led.on();\n Hit control-D to exit.\n >> ");
console.log('try "on", "off", "toggle", "strobe", "stop" (stops strobing)');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment