# Blinking LED in function

**URL:** <https://discourse.nodered.org/t/blinking-led-in-function/23322>\
**Category:** General\
**Created:** [19 March 2020 19:12 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322 "2020-03-19T19:12:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenRott](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@StevenRott](https://discourse.nodered.org/u/StevenRott)\
**Post date:** [19 March 2020 19:12 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/1 "2020-03-19T19:12:25Z")

</div>

Hi there,

I want to have a blinking LED.....how can i Do this in a function ???  
I have already the following:

```auto
abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
if (abs < 20){
 do 
{return {payload:1};

}
while(alarmon === true)

}
if (alarmon === false){return {payload:0};}
return msg;

```

I need after the first "return payload"another payload return with 0 an a delay......I hope you know what I mean...  
Thanks for your help !!

---

<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:** [19 March 2020 22:44 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/2 "2020-03-19T22:44:24Z")

</div>

Normally I'd write out the solution but here is a flow demonstrating it turning on and off a ui-led using a `switch`, `trigger`, `delay`and `change` nodes. You should be able to adapt it to your needs.

 ![Screen Shot 2020-03-19 at 6.43.20 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/85924114c4cc9f7cbdd4334e78ead00120eb3a7b.png)

```auto
[{"id":"9f6951d1.47eee","type":"tab","label":"Test","disabled":false,"info":""},{"id":"66bfc89c.55c1f","type":"switch","z":"9f6951d1.47eee","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":290,"y":220,"wires":[["99a0ef7d.62fcf"],["b002a39.ef7776"]]},{"id":"99a0ef7d.62fcf","type":"trigger","z":"9f6951d1.47eee","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"1","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":460,"y":180,"wires":[["7aa06b8a.acd8b4","3163e7ae.c42568"]]},{"id":"3163e7ae.c42568","type":"delay","z":"9f6951d1.47eee","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":460,"y":80,"wires":[["99a0ef7d.62fcf"]]},{"id":"7aa06b8a.acd8b4","type":"ui_led","z":"9f6951d1.47eee","group":"b853aa95.01f38","order":1,"width":0,"height":0,"label":"","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"green","value":"1","valueType":"num"},{"color":"red","value":"0","valueType":"num"}],"allowColorForValueInMessage":false,"name":"","x":910,"y":180,"wires":[]},{"id":"e98ec34b.4f4a4","type":"inject","z":"9f6951d1.47eee","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["66bfc89c.55c1f"]]},{"id":"4dd5c1b.e1fbdc","type":"inject","z":"9f6951d1.47eee","name":"","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":260,"wires":[["66bfc89c.55c1f"]]},{"id":"b002a39.ef7776","type":"change","z":"9f6951d1.47eee","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":260,"wires":[["8f1dbf6.b70354","a0219e49.8cb4c8"]]},{"id":"88aa448a.423f98","type":"inject","z":"9f6951d1.47eee","name":"Manual and startup reset","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":210,"y":360,"wires":[["b002a39.ef7776"]]},{"id":"a0219e49.8cb4c8","type":"change","z":"9f6951d1.47eee","name":"needed to turn of ui-led","rules":[{"t":"set","p":"payload","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":220,"wires":[["7aa06b8a.acd8b4"]]},{"id":"8f1dbf6.b70354","type":"link out","z":"9f6951d1.47eee","name":"to RESET","links":["b3ed9183.7e5668"],"x":660,"y":300,"wires":[],"l":true},{"id":"b3ed9183.7e5668","type":"link in","z":"9f6951d1.47eee","name":"RESET","links":["8f1dbf6.b70354"],"x":250,"y":120,"wires":[["3163e7ae.c42568","99a0ef7d.62fcf"]],"l":true},{"id":"b853aa95.01f38","type":"ui_group","z":"","name":"Default","tab":"c0e9b3.7bfe565","disp":true,"width":"14","collapse":false},{"id":"c0e9b3.7bfe565","type":"ui_tab","z":"","name":"Test","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![StevenRott](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@StevenRott](https://discourse.nodered.org/u/StevenRott)\
**Post date:** [20 March 2020 07:47 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/3 "2020-03-20T07:47:56Z")

</div>

Is it possible to do this in JSON ??

---

<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:** [20 March 2020 08:41 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/4 "2020-03-20T08:41:05Z")

</div>

If you need to send two (or more) messages from within a function then you need to use `node.send(...)` - see Sending Messages Asynchronously here - [https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)  
Note you also have setTimeout and setInterval available to you in a function so you may use those also.

---

<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:** [20 March 2020 08:47 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/5 "2020-03-20T08:47:02Z")

</div>

What do you mean by do it in JSON?

---

<div class="post-metadata">

**Author:** ![StevenRott](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@StevenRott](https://discourse.nodered.org/u/StevenRott)\
**Post date:** [20 March 2020 08:49 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/6 "2020-03-20T08:49:41Z")

</div>

SO i want to have it in the function, like this:

```auto
abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
if (abs < 20){
 do 
{return {payload:1};
delay(1000);
return {payload:0};
delay(1000);

}
while(alarmon === true)

}
if (alarmon === false){return {payload:0};}
return msg;

```

So while alarmon ist true the function will send "1" then wait 1 second, then send "0", wait 1 second, then send again "1", wait.....and so on....do you understand what i mean ?!🙂

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [20 March 2020 09:46 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/7 "2020-03-20T09:46:34Z")

</div>

delay is not a valid built in function in javascript, read again what @dceejay wrote and google a bit about that for javascript, that is the best way learning and not just expecting ready recipes

---

<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:** [20 March 2020 10:23 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/8 "2020-03-20T10:23:53Z")

</div>

FYI  
JSON (JavaScript Object Notation) is an open standard file format  
`functions' are written using JavaScript (a programming language)

I'll be intereated to see the solution you come up with in your function node.

---

<div class="post-metadata">

**Author:** ![StevenRott](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@StevenRott](https://discourse.nodered.org/u/StevenRott)\
**Post date:** [20 March 2020 11:26 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/9 "2020-03-20T11:26:08Z")

</div>

Okay...sorry, but I'm obviously too stupid to do this !!! SO my LED will not blink....so it is !!

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [20 March 2020 11:36 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/10 "2020-03-20T11:36:27Z")

</div>

Don't give up that easy, you are here to learn, we have all walked that long journey. But you will not help yourself if we just serve you a ready solution

---

<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:** [20 March 2020 11:57 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/11 "2020-03-20T11:57:31Z")

</div>

So let's break this down a bit... and maybe re-arrange

if the alarmon is false we need to cancel any running flash sequence and send out a 0 (as you do already) - and return.  
if abstand is greater (or =) to 20 then cancel and return. (though you haven't said if this is what should happen if it goes back above 20 rather than being turned off)  
otherwise  
if there is an existing flash sequence running do nothing (return null)  
or start a flash sequence

In this case a flash sequence is probably best set by a setInterval running every (say 1) second to turn it on - and containing another setTimeout call after 0.5 sec to turn it off... - as this is non-obvious I will give it to you 🙂

```auto
tick = setInterval(function() { 
    node.send({payload:1}); 
    setTimeout(function() {
        node.send({payload:0})
        }, 500)
}, 1000);

```

to ensure that we preserve the flash operation between multiple message arriving (so we can tell if it's already flashing or not and don't start another at the same time - we need to save it is context -  
so once started you will need to save it like `context.set("timer",tick) `  
and then that can be used (further up the function) where you need to test if it's running or not - to clear a setInterval you can then use `clearInterval(context.get("tick")`

Hopefully enough clues there to get you going - and googling - and trying some stuff and asking more questions 🙂

---

<div class="post-metadata">

**Author:** ![StevenRott](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@StevenRott](https://discourse.nodered.org/u/StevenRott)\
**Post date:** [21 March 2020 10:31 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/12 "2020-03-21T10:31:51Z")

</div>

Hi,

i tested it like this:

```auto
abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
tick = setInterval(function() { 
    node.send({payload:1}); 
    setTimeout(function() {
        node.send({payload:0})
        }, 500)
}, 1000);

if (abs < 20){
 do 
{tick();
context.set("timer",tick);
}
while(alarmon === true)

}
if (alarmon === false){
    clearInterval(context.get("tick"));
return {payload:0};}
return msg;

```

My LED is blinking, but always !!!☹

Sorry, that I'm so stupid.....

---

<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:** [21 March 2020 21:24 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/13 "2020-03-21T21:24:56Z")

</div>

Not stupid - just need more practise... here you go...

```auto
abs = msg.payload.abstand;
alarmon = msg.payload.alarm;
tick = context.get("timer") || null; // retrieve saved timer (if there is one)

if (alarmon === false) { // alarm is off
    if (tick) { // if running - stop and clear it
        clearInterval(tick);
        context.set("timer",null);
    }
    return;
}

if (abs >= 20) { // if above the limit stop
    if (tick) { // if running - stop and clear it
        clearInterval(tick);
        context.set("timer",null);
    }
    return;
}

if (tick) { return; } // if already running - leave it alone

// otherwise start it running
tick = setInterval(function() { 
    node.send({payload:1}); 
    setTimeout(function() {
        node.send({payload:0})
        }, 500)
}, 1000);
context.set("timer",tick); // save it 

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 May 2020 21:37 UTC](https://discourse.nodered.org/t/blinking-led-in-function/23322/14 "2020-05-20T21:37:35Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
