Basics,Faster LED Blink,Set/Get function: mosquito into an elephant

Hey Dear Node-Red Forum.

I am still new to Node-Red

I have serveral questions, for you maybe basic, maybe i am just stupid, but its making me crazy(a chat would be nice instead of this thousend of topics.).. anyway:

I programm just for testing in Arduino IDE a programm which makes a LED blinking faster with each blink.. til its on, and starts from beginning.

#define LED D1 //define LED output
void setup() {
 pinMode(LED, OUTPUT); //Port declare Output

void loop() {
for (int i=80; i>=0;i--) {  //80 Steps, faster and faster
  digitalWrite(LED, HIGH );

So now i want to create it in Node-Red, and there is the question:
Is it possible to change the value of the previous Inject Time interval in a function like with a set / get function?? Or am I going the wrong way?

Then i want to have a delay.. I found the Function setTimeout(), or wait() or clearTimeout(). but that doesnt work.

Best regards


PS: I know, there doesnt exist stupid questions.. but i am close to it.

I can assure you that those functions definitely do work.
You cannot change the interval of an inject node at run time. One way is to write it in javascript, much as you have written it for the Arduino, using setTimeout to control the interval. An alternative would be using a delay node in a loop, note that you can specify the interval for the delay node withing the message so you could have a function node that outputs the led state and the time it should stay at that. Feed that to the led and also to the delay node, then feed the output of the delay node back to the function node for it to do the next blink.

Hey Colin, thanks for your help!

Do I have to edit a special library for this?

So it changes at runtime?
This point i not really understand how to do this..
My next idea was to give them a continusly Start signal which i delay with clearTimeout(count) . Count is the basis of the delaytime.

{"id":"8db24fd1.37c7f","type":"function","z":"f58e00a1.0854a","name":"Counter","func":"count = msg.count;\ncount -=1;\nmsg.count=count;\nmsg.checken=\"F1 \"+msg.payload+\" \"+count;\n\ncontext.level = !context.level || 0;\nif (context.level===true) {\n    msg.payload=1;\n    }else{msg.payload=0;}\nreturn msg;\n","outputs":"1","noerr":0,"x":465,"y":125,"wires":[["155bb389.aae80c","690b073f.1350f8"]]},
{"id":"155bb389.aae80c","type":"debug","z":"f58e00a1.0854a","name":"","active":true,"console":"true","complete":"true","x":734,"y":193,"wires":[]},{"id":"c876cef1.38e89","type":"function","z":"f58e00a1.0854a","name":"delay","func":"var count=global.get('count')|| 100;\nmsg.count=count;\ncount=msg.count;\nclearTimeout(msg.count*10);\nreturn msg;","outputs":1,"noerr":0,"x":319,"y":125,"wires":[["8db24fd1.37c7f"]]}]

Best regards

Why not code a function node and add the loop you have only changing the digitalWrite(LED, HIGH ); and digitalWrite(LED, LOW ); to node.send({payload: 1}); and node.send({payload: 0}); and send that to the gpio node?

you might want to read about the node.send:

Hi zenofmud, thanks for your help!

Yes, it would work with node.send, but that is actually coding again. I want to get familiar with the nodes and the submitting Data between them. I have still problems with the "other" thinking in NodeRed :sweat_smile:

Thank you

best regards


I think this is one case where it really is more sensible to use a simple function node with setTimeout rather than trying to build a convoluted flow using standard nodes.

1 Like

You can use the slack channel if you prefer. - Redirecting…

1 Like

Hey Colin!

Do you know how i can implement this function? I always get the note that this is not defined

Thank you !

Best regards!

Show us the code you are using.
If you have moved to Slack for this problem then please say so here so we can stop.

This one:

{"id":"8db24fd1.37c7f","type":"function","z":"f58e00a1.0854a","name":"Counter","func":"count = msg.count;\ncount -=1;\nmsg.count=count;\nmsg.checken=\"F1 \"+msg.payload+\" \"+count;\n\ncontext.level = !context.level || 0;\nif (context.level===true) {\n    msg.payload=1;\n    }else{msg.payload=0;}\nreturn msg;\n","outputs":"1","noerr":0,"x":465,"y":125,"wires":[["155bb389.aae80c","690b073f.1350f8"]]},
{"id":"155bb389.aae80c","type":"debug","z":"f58e00a1.0854a","name":"","active":true,"console":"true","complete":"true","x":734,"y":193,"wires":[]},{"id":"c876cef1.38e89","type":"function","z":"f58e00a1.0854a","name":"delay","func":"var count=global.get('count')|| 100;\nmsg.count=count;\ncount=msg.count;\nsetTimeout(msg.count*10);\nreturn msg;","outputs":1,"noerr":0,"x":319,"y":125,"wires":[["8db24fd1.37c7f"]]}]

Please see this post on how to share flows so that it is importable, you can edit your previous post accordingly.

okay, done
thank you

I don't understand at all what that flow is supposed to be doing. You do not need to keep triggering it like that, it just needs a single function node using setTimeout() and using the callback to do what you want, so you end up with code rather like your Arduino code. Search for javascript setTimeout for some examples of how to do it.

Here is something to get you going that just keeps inverting the output


[{"id":"d7ff0732.2f81d8","type":"tab","label":"test 3","disabled":false,"info":""},{"id":"6effc6b1.ef0ea8","type":"inject","z":"d7ff0732.2f81d8","name":"Timer","topic":"","payload":"1","payloadType":"num","repeat":"0.01","crontab":"","once":true,"x":78,"y":149,"wires":[[]]},{"id":"931a2839.cccde8","type":"function","z":"d7ff0732.2f81d8","name":"Counter","func":"count = msg.count;\ncount -=1;\nmsg.count=count;\nmsg.checken=\"F1 \"+msg.payload+\" \"+count;\n\ncontext.level = !context.level || 0;\nif (context.level===true) {\n    msg.payload=1;\n    }else{msg.payload=0;}\nreturn msg;\n","outputs":"1","noerr":0,"x":361,"y":150,"wires":[[]]},{"id":"73f790fb.ad9d68","type":"function","z":"d7ff0732.2f81d8","name":"delay","func":"var count=global.get('count')|| 100;\nmsg.count=count;\ncount=msg.count;\nsetTimeout(msg.count*10);\nreturn msg;","outputs":1,"noerr":0,"x":215,"y":150,"wires":[["931a2839.cccde8"]]},{"id":"b04319c5.1bd738","type":"inject","z":"d7ff0732.2f81d8","name":"Click me to start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":127.5,"y":299,"wires":[["cbc684ed.f8b4c"]]},{"id":"cbc684ed.f8b4c","type":"function","z":"d7ff0732.2f81d8","name":"DoIt","func":"var currentInterval = 1000     // initial delay\nvar currentState = 0           // current output state\n\nwait(currentInterval)\nreturn null\n\n// wait for the given interval then flick the output\nfunction wait(interval) {\n    setTimeout(invertAndRestart, interval)\n}\n\n// invert the output and go round again\nfunction invertAndRestart() {\n    currentState = currentState===0 ? 1 : 0\n    var newMsg = {payload: currentState}\n    node.send(newMsg)\n    wait(currentInterval)\n}","outputs":1,"noerr":0,"x":296.5,"y":299,"wires":[["507954cd.ce7724"]]},{"id":"507954cd.ce7724","type":"debug","z":"d7ff0732.2f81d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":455.5,"y":299,"wires":[]}]

okay that is a lot smarter...
didnt used/understand the setTimeout function correctly.
I just wantet to "wait" .. like a delay in c#

This code i am not 100% understandig. because isnt it always equal?

currentState = currentState===0 ? 1 : 0


1 Like