Skip to content

Instantly share code, notes, and snippets.

@jhalbrecht
Forked from rwaldron/J5TennisBall.js
Last active December 28, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhalbrecht/7557511 to your computer and use it in GitHub Desktop.
Save jhalbrecht/7557511 to your computer and use it in GitHub Desktop.
This is @rwaldron suggestion (unTested) for me to learn a bit better coding style. Removing some of Rick's "Nit's" See lines 18, 21 for my additions, and 37 for pesky error (fixed).
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
});
lights = new Array(); // ?init array?
// [
[
{ 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 j5.Leds();
/*
/home/jeffa/Development/node/j/rick.js:38
lights.all = new Leds();
^
TypeError: undefined is not a function
*/
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