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

**URL:** <https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168>\
**Category:** General\
**Created:** [13 June 2019 07:30 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168 "2019-06-13T07:30:56Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoundTriangle](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@RoundTriangle](https://discourse.nodered.org/u/RoundTriangle)\
**Post date:** [13 June 2019 07:30 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/1 "2019-06-13T07:30:56Z")

</div>

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.

```auto
#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 );
  delay(i*5);
  digitalWrite(LED,LOW);
  delay(i*5);
}
  
 }

```

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

Tom

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 June 2019 08:06 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/2 "2019-06-13T08:06:44Z")

</div>

> [@RoundTriangle](#):
>
> I found the Function setTimeout(), or wait() or clearTimeout(). but that doesnt work.

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.

---

<div class="post-metadata">

**Author:** ![RoundTriangle](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@RoundTriangle](https://discourse.nodered.org/u/RoundTriangle)\
**Post date:** [13 June 2019 09:51 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/3 "2019-06-13T09:51:12Z")

</div>

Hey Colin, thanks for your help!

> [@Colin](#):
>
> I can assure you that those functions definitely do work

Do I have to edit a special library for this?

> [@Colin](#):
>
> specify the interval for the delay node withing the message

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.

```auto

[{"id":"690b073f.1350f8","type":"mraa-gpio-dout","z":"f58e00a1.0854a","name":"","pin":"11","set":false,"level":"1","x":643,"y":125,"wires":[]},{"id":"68ac98ea.01bd88","type":"inject","z":"f58e00a1.0854a","name":"Timer","topic":"","payload":"1","payloadType":"num","repeat":"0.01","crontab":"","once":true,"x":182,"y":124,"wires":[["c876cef1.38e89"]]},
{"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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 June 2019 10:22 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/4 "2019-06-13T10:22:16Z")

</div>

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: [https://nodered.org/docs/writing-functions#sending-messages-asynchronously](https://nodered.org/docs/writing-functions#sending-messages-asynchronously)

---

<div class="post-metadata">

**Author:** ![RoundTriangle](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@RoundTriangle](https://discourse.nodered.org/u/RoundTriangle)\
**Post date:** [13 June 2019 10:37 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/5 "2019-06-13T10:37:19Z")

</div>

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 😅

Thank you

best regards

Tom

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 June 2019 12:56 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/6 "2019-06-13T12:56:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 June 2019 20:43 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/7 "2019-06-13T20:43:08Z")

</div>

> [@RoundTriangle](#):
>
> ( _a chat would be nice instead of this thousend of topics._ )

You can use the slack channel if you prefer. - [Redirecting…](https://nodered.org/slack/)

---

<div class="post-metadata">

**Author:** ![RoundTriangle](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@RoundTriangle](https://discourse.nodered.org/u/RoundTriangle)\
**Post date:** [14 June 2019 07:49 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/8 "2019-06-14T07:49:16Z")

</div>

Hey Colin!

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

> [@dceejay](#):
>
> You can use the slack channel if you prefer. - [Redirecting…](https://nodered.org/slack/)

Thank you !

Best regards!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 08:06 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/9 "2019-06-14T08:06:57Z")

</div>

> [@RoundTriangle](#):
>
> I always get the note that this is not defined

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.

---

<div class="post-metadata">

**Author:** ![RoundTriangle](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@RoundTriangle](https://discourse.nodered.org/u/RoundTriangle)\
**Post date:** [14 June 2019 08:41 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/10 "2019-06-14T08:41:58Z")

</div>

This one:

```auto
[{"id":"690b073f.1350f8","type":"mraa-gpio-dout","z":"f58e00a1.0854a","name":"","pin":"11","set":false,"level":"1","x":643,"y":125,"wires":[]},{"id":"68ac98ea.01bd88","type":"inject","z":"f58e00a1.0854a","name":"Timer","topic":"","payload":"1","payloadType":"num","repeat":"0.01","crontab":"","once":true,"x":182,"y":124,"wires":[["c876cef1.38e89"]]},
{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 08:45 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/11 "2019-06-14T08:45:47Z")

</div>

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

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![RoundTriangle](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@RoundTriangle](https://discourse.nodered.org/u/RoundTriangle)\
**Post date:** [14 June 2019 08:53 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/12 "2019-06-14T08:53:23Z")

</div>

okay, done  
thank you

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 08:59 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/13 "2019-06-14T08:59:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 09:16 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/14 "2019-06-14T09:16:41Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/44a0c2b62e81030e947827030ca15e10ff105e94.png)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![RoundTriangle](https://avatars.discourse-cdn.com/v4/letter/r/e480ec/32.png) [@RoundTriangle](https://discourse.nodered.org/u/RoundTriangle)\
**Post date:** [14 June 2019 09:25 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/15 "2019-06-14T09:25:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 June 2019 09:27 UTC](https://discourse.nodered.org/t/basics-faster-led-blink-set-get-function-mosquito-into-an-elephant/12168/16 "2019-06-14T09:27:44Z")

</div>

See [https://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript](https://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript)
