Last active
August 26, 2017 20:08
-
-
Save MimiOnuoha/15baa2f0c63b5260da712317bf67a256 to your computer and use it in GitHub Desktop.
Controlling a neon light with the command line
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 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