# How to convert to a function

**URL:** <https://discourse.nodered.org/t/how-to-convert-to-a-function/6170>\
**Category:** General\
**Created:** [27 December 2018 16:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170 "2018-12-27T16:49:05Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 13:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/63 "2018-12-29T13:49:13Z")

</div>

it was usefull to have I just had not realised it had been set to 1000

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 13:50 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/64 "2018-12-29T13:50:28Z")

</div>

probably better if it was put in the change box to start with

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 13:51 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/65 "2018-12-29T13:51:20Z")

</div>

But many thanks to every who has helped on this

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 13:59 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/66 "2018-12-29T13:59:10Z")

</div>

```auto
[{"id":"4b6b6cb2.275e6c","type":"change","z":"ff81c2c4.ef9ce8","name":"","rules":[{"t":"set","p":"lighton","pt":"msg","to":"20","tot":"num"},{"t":"set","p":"lightoff","pt":"msg","to":"25","tot":"num"},{"t":"set","p":"numberofblinks","pt":"msg","to":"3","tot":"num"},{"t":"set","p":"delaystart","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":900,"wires":[["d8c970d7.150a8"]]}]

```

```auto
//get the values of msg.* to var's
var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;
var light_delay_start = msg.delaystart;
//var run;

//how long to wait before starting the light show
var time_in_ms_to_wait_before_first_execution = light_delay_start;

```

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [29 December 2018 14:08 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/67 "2018-12-29T14:08:14Z")

</div>

@gbond 👍

or just this way:

```auto
//get the values of msg.* to var's
var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;
//var run;

//how long to wait before starting the light show
var time_in_ms_to_wait_before_first_execution = msg.delaystart;

```

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 16:16 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/68 "2018-12-29T16:16:37Z")

</div>

Have you got the full coding think i got in a muddle with it now?

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [29 December 2018 16:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/69 "2018-12-29T16:49:17Z")

</div>

This is the function code that should work with your latest change node:

```auto
//get the values of msg.* to var's
var numberofblinks = msg.numberofblinks;
var light_on_for_ms = msg.lighton;
var light_off_for_ms = msg.lightoff;

//how long to wait before starting the light show
var time_in_ms_to_wait_before_first_execution = msg.delaystart || 0;

//total run time of light on and light off
var time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);

//set the count to 1
var count = 1;

var tostopit;

//function to count the number of blinks and stop timmer
function blinks() {
    if (numberofblinks <= count) {
        //stop the recursive setTimeout
        clearTimeout(tostopit);
    } else {
        count++;
    }
}

//function to set payload to true
function payload_true() {
    //set payload to true
    msg.payload = true;
    node.send(msg);

    //display green dot when light is on
    node.status({fill: 'green', shape: 'dot', text: ' '});
}

//function to set payload to false
function payload_false() {
    //set payload to false
    msg.payload = false;
    node.send(msg);

    //clear status when payload false
    node.status({});
}

//run the light show!!!!
setTimeout(function run() {
    //turn the light on by setting msg.payload to true
    payload_true();

    //turn the light off by waiting to set the payload to false by the ammount of time light is on
    setTimeout(payload_false, light_on_for_ms);

    //total ammount of time light can be on or off before it runs again
    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);

    blinks();

}, time_in_ms_to_wait_before_first_execution);

return null;

```

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 17:13 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/70 "2018-12-29T17:13:13Z")

</div>

Yup  
That seem great

Thanks so MUCH for everyones help on this 😀😀😀

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [29 December 2018 17:27 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/71 "2018-12-29T17:27:14Z")

</div>

Awesome !!!!!! Thanks cflurin for showing me how I needed to move the blinks(); outside the inner timeout.

Well gbond now you need to make a gif or vid of it working with a fancy light show when you get it done.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 17:31 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/72 "2018-12-29T17:31:32Z")

</div>

The deathstar gif is pritty much what it does also I have the lego Roller coaster that has a sensor at the top to stop the motors from running & it also now triggers the Camera LED to flash 3 times before it pass it

(there is anothe sensor at the bottom to start it going again when it gets there)

All controlable from my mobil phone!!!

It flashes pritty quick so the video proberbly will not show it

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [29 December 2018 17:37 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/73 "2018-12-29T17:37:13Z")

</div>

nice gbond,

oh I learned about recursive timer via this link.  
[https://javascript.info/settimeout-setinterval](https://javascript.info/settimeout-setinterval)

thanks gbond for helping me expand my brain.  
thanks cflurin for helping put my brain back together.

Ive always used setTimeout for a one time delay trigger.  
this is a loop form of using it and id never done such a thing.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 17:40 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/74 "2018-12-29T17:40:24Z")

</div>

I looked at that, did not understand it!. I can see how it works in there Test but not how you then put it in to node-red to do anything

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 18:52 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/75 "2018-12-29T18:52:04Z")

</div>

![sizeInked20181229_182045_Trim](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1cb060942b66f6fc090508ef948301d41562bffe.gif)

I think this sort off shows it!

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [29 December 2018 19:15 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/76 "2018-12-29T19:15:19Z")

</div>

oh that's way more impressive than my imagination came up with from your description.

nice work!

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [29 December 2018 23:03 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/77 "2018-12-29T23:03:33Z")

</div>

Havent work out the best way to stop it at the station and relaunch it. Did think it might be possible to use electro magnets somehow, but not shore its possible to get anything small/powerfull enought

Also looking for a way to automaticly turn the lights on in the carts (again finding it difficult to find anything small enough to do it) (magnetic reed switch)

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [30 December 2018 00:40 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/78 "2018-12-30T00:40:12Z")

</div>

So just an idea for cart lights.

you could use a micro mercury switch and a 555 timer. If you can solder small.  
the mercury switch would turn on the timer and the lights would stay on until timer runs out.

more done method would be to use something like this

> **[2384\_Web.pdf](https://media.digikey.com/pdf/Data%20Sheets/Adafruit%20PDFs/2384_Web.pdf)**
>
> 491.81 KB

connected to something like this

> **[3435\_Web.pdf](https://media.digikey.com/pdf/Data%20Sheets/Adafruit%20PDFs/3435_Web.pdf)**
>
> 552.22 KB

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [30 December 2018 00:55 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/79 "2018-12-30T00:55:16Z")

</div>

Interesting the motion sensor might work

Not so sure on the board as there is no way to get a signal to it

Garrath

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [30 December 2018 01:12 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/80 "2018-12-30T01:12:02Z")

</div>

the motion sensor is the signal for the board. instead of pressing the button with your finger. wire the motion sensor to the button. when the cart starts moving it will start the timer. as the timer keeps getting trigered by the motion sensor it rests the count down.

when the cart stops it will stop triggering the motion sensor. then after that the timer runs out and the lights turn off.

---

<div class="post-metadata">

**Author:** ![gbond](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbond/32/38851_2.png) [@gbond](https://discourse.nodered.org/u/gbond)\
**Post date:** [30 December 2018 01:25 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/81 "2018-12-30T01:25:43Z")

</div>

I get it the motion can’t work on its own then! not sure there is room for both I will investigate

---

<div class="post-metadata">

**Author:** ![meeki007](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/meeki007/32/12499_2.png) [@meeki007](https://discourse.nodered.org/u/meeki007)\
**Post date:** [30 December 2018 01:49 UTC](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170/82 "2018-12-30T01:49:55Z")

</div>

well if you can solder real small you can make it compact around the motion sensor.

the arrow in the photo is pointing to the 555 timer.

![size](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/ee1852e214622b55e37768f5d2872fe8b5f20bfa.png)

the whole board is about the size of a quarter. so the timer is small.

buy the timer and needed parts to do this.

> **[555 Delay OFF Timer circuit For Delay Before Turn OFF circuit](https://www.circuitspedia.com/555-timer-delay-off-timer-circuit-power-off-delay-timer/)**
>
> Here a circuit diagram of Delay Power OFF Timer circuit For OFF time Delay, Switch OFF Delay Timer Circuit Diagram. Timer circuit for Delay Switch OFF. When Appliance or Load is Turn ON then After a fix time duration Automatic Switch OFF. Automatic...

  
and build it all in one of the carts

[Previous page](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170.md?page=3)

[Next page](https://discourse.nodered.org/t/how-to-convert-to-a-function/6170.md?page=5)
