Wednesday, 31 January 2018

Rasperry Pi, Node and Johnny-five: Yes this is a JavaScript help post

I've been playing with Raspberry Pi and Node for fun. I thought for a simple experiment what if I grabbed some user input to turn an LED on and off.const readline = require('readline'); const log = console.log; const five = require('johnny-five'); const raspi = require('raspi-io'); const board = new five.Board({ io: new raspi(), }); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); board.on('ready', function() { const led = new five.Led('P1-7'); const recursiveAsyncReadLine = function () { rl.question('Command: ', function (answer) { switch(answer) { case 'on': log('Got it! Your answer was: "', answer, '"'); led.on(); break; case 'off': log('Got it! Your answer was: "', answer, '"'); led.stop().off(); break; default: } recursiveAsyncReadLine(); }); }; recursiveAsyncReadLine(); }); It works however I get 2 strange bugs. In the console output below you'll see it prompts for my input... I enter my input, then it repeats my input in a distorted string of text (see Example 1). Then after my verification message is output (Got it! Your answer was: " on ") I am met with a "ReferenceError: on is not defined" (Example 2) even though the LED lit up perfectly.Command: on oonn //Example 1 Got it! Your answer was: " on " Command: ReferenceError: on is not defined //Example 2 >> off ooffff Got it! Your answer was: " off " Command: ReferenceError: off is not defined >> on oonn Got it! Your answer was: " on " Command: ReferenceError: on is not defined >> off ooffff Got it! Your answer was: " off " Command: ReferenceError: off is not defined >> on oonn Got it! Your answer was: " on " Command: ReferenceError: on is not defined >> off ooffff Got it! Your answer was: " off " Command: ReferenceError: off is not defined I am thinking this is not so much a Raspberry Pi/Johnny-five thing and just a plain old JavaScript or Node issue. Any ideas?

Submitted February 01, 2018 at 02:06AM by joWebDev

No comments:

Post a Comment