Node Red + Johnny five simple LED function

899 views
Skip to first unread message

Chris Ray

unread,
May 31, 2015, 1:53:21 AM5/31/15
to node...@googlegroups.com
Being very new to Node Red, Johnny Five, and Arduino, I have been experimenting with functions trying to do some very simple things like setup a flow to blink an LED or turn it off depending on if I send in a 1 or 0.

I can do this easily with the GPIO J5 node, but when I try to do it via my own written function I can blink the LED fine, but when I try to turn it off it fails to stop and I get "pin in use".  I tried this on both pin 9 and 13.

Can someone point me to this very simple example flow, which would be two inject nodes, one injecting "1", the other injecting "0", to a function that blinks and LED on pin 9/13 when a "1" is received and turns it off when a 0 is received.

Here is my very simple code:

[{"id":"c0ec5f33.06047","type":"inject","name":"","topic":"","payload":"1","payloadType":"string","repeat":"","crontab":"","once":false,"x":254.5999755859375,"y":996.3999633789062,"z":"3ac3e7e3.bb80bc","wires":[["e18a9bf1.a08288"]]},{"id":"82b51393.dccf9","type":"debug","name":"","active":true,"console":"false","complete":"false","x":829.7999267578125,"y":1025.2000732421875,"z":"3ac3e7e3.bb80bc","wires":[]},{"id":"66f46563.30fcd4","type":"inject","name":"","topic":"","payload":"0","payloadType":"string","repeat":"","crontab":"","once":false,"x":265,"y":1098,"z":"3ac3e7e3.bb80bc","wires":[["e18a9bf1.a08288"]]},{"id":"e18a9bf1.a08288","type":"function","name":"new","func":"var five = context.global.jfive;\nvar led = new five.Led(13);\ncontext.switch = context.switch || 0;\ncontext.switch = msg.payload;\nif (context.switch == 1) {\nled.blink(500);\n}\nif (context.switch == 0) {\nled.stop().off();\n}\nreturn msg;","outputs":1,"valid":true,"x":523.4000244140625,"y":1024.7999267578125,"z":"3ac3e7e3.bb80bc","wires":[["82b51393.dccf9"]]}]

Thank you!

Dave C-J

unread,
May 31, 2015, 6:26:54 AM5/31/15
to node...@googlegroups.com
Hi,

subtle one this... - you are creating a new LED instance every time yo invoke the function... when you need to re-use the existing one... - so you need to also save it in the local context of the function (rather than the global context)...  eg

[{"id":"99965e69.6669a","type":"inject","name":"","topic":"","payload":"1","payloadType":"string","repeat":"","crontab":"","once":false,"x":189,"y":558,"z":"f307b843.0cf848","wires":[["c6ba22d6.3945e"]]},{"id":"c6ba22d6.3945e","type":"function","name":"new","func":"var five = context.global.jfive;\ncontext.led = context.led || new five.Led(13);\ncontext.switch = context.switch || 0;\ncontext.switch = msg.payload;\nconsole.log(typeof(context.switch));\nif (context.switch == 1) {\n    context.led.blink(500);\n}\nif (context.switch == 0) {\n    context.led.stop().off();\n}\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":620,"z":"f307b843.0cf848","wires":[["f79ec9c9.086138"]]},{"id":"cfb008b2.304ff8","type":"inject","name":"","topic":"","payload":"0","payloadType":"string","repeat":"","crontab":"","once":false,"x":196.4000244140625,"y":691.6000366210938,"z":"f307b843.0cf848","wires":[["c6ba22d6.3945e"]]},{"id":"f79ec9c9.086138","type":"debug","name":"","active":true,"console":"false","complete":"false","x":608,"y":620,"z":"f307b843.0cf848","wires":[]}]

Chris Ray

unread,
May 31, 2015, 10:49:02 AM5/31/15
to node...@googlegroups.com
Dave,

Thank you so much!  I can't tell you how many times I went back and forth with something similar to:

context.led = context.led || new five.Led(13);

The part I missed and could not find any examples of was the "|| new five.Led(13)"  

I knew I was creating a new led every time and that was wrong, I just did not know that you could || to instantiate (or whateve it is called in node.jd) the object.  It makes perfect sense now.  It is so hard to find example code for this stuff.  I really appreciate you help.  

Thanks again!

Julian Knight

unread,
May 31, 2015, 11:30:57 AM5/31/15
to node...@googlegroups.com
Chris, just to be clear that the code given is a shorthand way of saying:

set led to led unless led doesn't exist, in that case create the led.

So you could do it in a more long-winded way with an if statement. The shortcut Dave used is, however, a common one in JavaScript programming.

Sorry if you already know that, just wanted to clarify.

Dave C-J

unread,
May 31, 2015, 5:17:19 PM5/31/15
to node...@googlegroups.com
thanks for that,


as it works left to right you can actually have more than two things... and it returns the first non-falsey one.
var foo = a || b || c || etc || "default";

Randy Lust

unread,
Dec 30, 2015, 10:37:56 AM12/30/15
to Node-RED
Dave C_J

Could you post the code that you used as a result of this fix.
I am looking to make a led blink X number of times with a passed integer
Thanks!

Dave C-J

unread,
Dec 30, 2015, 11:50:02 AM12/30/15
to node...@googlegroups.com

Does the code in my first reply in this thread not work ?
(Not near computer for a couple of days so can't check myself)

Randy Lust

unread,
Jan 6, 2016, 11:02:10 AM1/6/16
to Node-RED
When I use the code posted above I get the following error:

Type error: Cannot read property  'Led' of undefined

I am using a rasp pi 

Thanks!

Dave C-J

unread,
Jan 6, 2016, 4:17:40 PM1/6/16
to node...@googlegroups.com
what is in your settings.js file  ? - section called functionGloablContext....
mine is... (the // lines are commented out so ignore those)

    functionGlobalContext: {

        // os:require('os'),

        // bonescript:require('bonescript'),

        jfive:require('johnny-five'),

        j5board:require("johnny-five").Board({repl:false})

    },

Reply all
Reply to author
Forward
0 new messages