Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Controlling a neon light with the command line
var readline = require('readline'); // required so we can use the command line to read keypresses
var five = require("johnny-five"); // required so we can use the johnny five library
var board = new five.Board(); // initializing our board
function keyPressEvent() {
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
var led = new five.Led(11); // Change this if you want to change which pin your light is connected to
if (key.ctrl && key.name === 'c') { //allows us to be able to prss ctrl + c to exit out of the program
process.exit();
}
if (key.name === '1') { led.on(); } // note that bc the light defaults to on, this and the next will be reversed.
if (key.name === '2') { led.off(); }
if (key.name === '3') { led.pulse(); }
if (key.name === '4') { led.stop().off(); } // for stopping pulsing
});
console.log('Press keys 1, 2, 3, or 4 to control the LED');
}
board.on("ready", function() {
keyPressEvent();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment