# This code NEARLY works. Help needed. (Function node JS code)

**URL:** <https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381>\
**Category:** General\
**Created:** [21 April 2024 10:10 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381 "2024-04-21T10:10:48Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 10:10 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/1 "2024-04-21T10:10:48Z")

</div>

I have 8 (0-7) Neopixel leds in a strip.

At a given time I want to _walk_ a 3 led mark along them from 0 to 7.

This is the code I have now.

```auto
if (msg.delay_time == undefined)
    msg.delay_time = 400;
var delay_time = msg.delay_time;
if (msg.sunset == undefined)
    msg.sunset = "196,10,0";
var sunset = msg.sunset;

sendLater(0, msg, "rgb,0," + sunset);
sendLater(delay_time * 1, msg, "rgb,1," + sunset);
sendLater(delay_time * 2, msg, "rgb,1," + sunset);
sendLater(delay_time * 3, msg, "rgb,2," + sunset);
if (msg.existing[0] == undefined) {
    msg.existing[0] = "0,0,0";
}
sendLater(delay_time * 4, msg, "rgb,0," + msg.existing[0]);
sendLater(delay_time * 5, msg, "rgb,3," + sunset);
if (msg.existing[1] == undefined) {
    msg.existing[1] = "0,0,0";
}
sendLater(delay_time * 6, msg, "rgb,1," + msg.existing[1]);
sendLater(delay_time * 7, msg, "rgb,4," + sunset);
if (msg.existing[2] == undefined) {
    msg.existing[2] = "0,0,0";
}
sendLater(delay_time * 8, msg, "rgb,2," + msg.existing[2]);
sendLater(delay_time * 9, msg, "rgb,5," + sunset);
if (msg.existing[3] == undefined) {
    msg.existing[3] = "0,0,0";
}
sendLater(delay_time * 10, msg, "rgb,3," + msg.existing[3]);
sendLater(delay_time * 11, msg, "rgb,6," + sunset);
if (msg.existing[4] == undefined) {
    msg.existing[4] = "0,0,0";
}
sendLater(delay_time * 12, msg, "rgb,4," + msg.existing[4]);
sendLater(delay_time * 13, msg, "rgb,7," + sunset);

if (msg.existing[5] == undefined) {
    msg.existing[5] = "0,0,0";
}
sendLater(delay_time * 14, msg, "rgb,5," + msg.existing[5]);
if (msg.existing[6] == undefined) {
    msg.existing[6] = "0,0,0";
}
sendLater(delay_time * 15, msg, "rgb,6," + msg.existing[6]);

sendLater(delay_time * 16, msg, "rgb,7," + msg.existing[7]);
if (msg.existing[7] == undefined) {
    msg.existing[7] = "0,0,0";
}

/**
* @param {number} delayMS
* @param {NodeMessage} msg
* @param {string} payload
*/
function sendLater(delayMS, msg, payload) {
// node.warn(`In sendLater(${delayMS},${msg},${payload}) - setting up a timer to send payload '${payload}' in ${(delayMS/1000).toFixed(2)} seconds time`)
    const callback = function (msg, payload) {
// node.warn("Sending delayed payload " + payload);
        msg.payload = payload;
        node.send(msg);
    }
    setTimeout(callback, delayMS, RED.util.cloneMessage(msg), payload)
}

```

I don't know how I missed it for so long but there is a bug in the code.

The 3 red LED's walk along the strip but when it gets to number 6, there is a problem.

My best effort to explain what happens:  
LED 0 is set to RED  
LED 1 is set to RED  
LED 2 is set to RED and 0 is turned off.  
LED 3 is set to RED and 1 is turned off.  
LED 4 is set to RED and 2 is turned off.  
(repeat)  
LED 6 is set to RED and 4 is tuned off.  
LED 7 is set to RED and 5 is turned off - and also 6.

I think (hope) if I look at it more I may see the mistake, but.....

Sod's law says if I don't ask, I won't be able to see the mistake.

So I'm asking.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 April 2024 11:04 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/2 "2024-04-21T11:04:31Z")

</div>

> [@Trying\_to\_learn](#):
>
> ```auto
> sendLater(delay_time * 16, msg, "rgb,7," + msg.existing[7]);
> if (msg.existing[7] == undefined) {
> msg.existing[7] = "0,0,0";
> }
> 
> ```

here is one stand out issue. Surely that should be:

```auto
if (msg.existing[7] == undefined) {
    msg.existing[7] = "0,0,0";
}
sendLater(delay_time * 16, msg, "rgb,7," + msg.existing[7]);

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 11:11 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/3 "2024-04-21T11:11:04Z")

</div>

Yeah, ok, I'll cop that.

But I suspect it is something to do with how the code _works_.

If there is a _next LED_ things are good, but code for `6` looks different somehow and I think that is the problem.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 April 2024 11:18 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/4 "2024-04-21T11:18:19Z")

</div>

Some improvements to your code:

```auto

const delay_time = msg.delay_time | 400;
const sunset = msg.sunset || "196,10,0";

// Ensure msg.existing is an array
let existing = msg.existing
if (!Array.isArray(existing)) {
    existing = []
}

// Ensure elements 0 ~ 7 have a _some value_
for (let index = 0; index < 8; index++) {
    existing[index] = existing[index] || "0,0,0"
}

sendLater(0, msg, "rgb,0," + sunset);
sendLater(delay_time * 1, msg, "rgb,1," + sunset);
sendLater(delay_time * 2, msg, "rgb,1," + sunset);
sendLater(delay_time * 3, msg, "rgb,2," + sunset);
sendLater(delay_time * 4, msg, "rgb,0," + existing[0]);
sendLater(delay_time * 5, msg, "rgb,3," + sunset);
sendLater(delay_time * 6, msg, "rgb,1," + existing[1]);
sendLater(delay_time * 7, msg, "rgb,4," + sunset);
sendLater(delay_time * 8, msg, "rgb,2," + existing[2]);
sendLater(delay_time * 9, msg, "rgb,5," + sunset);
sendLater(delay_time * 10, msg, "rgb,3," + existing[3]);
sendLater(delay_time * 11, msg, "rgb,6," + sunset);
sendLater(delay_time * 12, msg, "rgb,4," + existing[4]);
sendLater(delay_time * 13, msg, "rgb,7," + sunset);
sendLater(delay_time * 14, msg, "rgb,5," + existing[5]);
sendLater(delay_time * 15, msg, "rgb,6," + existing[6]);
sendLater(delay_time * 16, msg, "rgb,7," + existing[7]);

/**
* @param {number} delayMS
* @param {NodeMessage} msg
* @param {string} payload
*/
function sendLater(delayMS, msg, payload) {
    // node.warn(`In sendLater(${delayMS},${msg},${payload}) - setting up a timer to send payload '${payload}' in ${(delayMS/1000).toFixed(2)} seconds time`)
    const callback = function (msg, payload) {
        // node.warn("Sending delayed payload " + payload);
        msg.payload = payload;
        node.send(msg);
    }
    setTimeout(callback, delayMS, RED.util.cloneMessage(msg), payload)
}

```

It makes it more readable - perhaps you can spot the issue with this version?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 April 2024 11:21 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/5 "2024-04-21T11:21:33Z")

</div>

And lastly, an even better (more node-red way)

Code:

```auto

const delay_time = msg.delay_time | 400;
const sunset = msg.sunset || "196,10,0";

// Ensure msg.existing is an array
let existing = msg.existing
if (!Array.isArray(existing)) {
    existing = []
}

// Ensure elements 0 ~ 7 have a _some value_
for (let index = 0; index < 8; index++) {
    existing[index] = existing[index] || "0,0,0"
}

sendMessage(0, msg, "rgb,0," + sunset);
sendMessage(delay_time * 1, msg, "rgb,1," + sunset);
sendMessage(delay_time * 2, msg, "rgb,1," + sunset);
sendMessage(delay_time * 3, msg, "rgb,2," + sunset);
sendMessage(delay_time * 4, msg, "rgb,0," + existing[0]);
sendMessage(delay_time * 5, msg, "rgb,3," + sunset);
sendMessage(delay_time * 6, msg, "rgb,1," + existing[1]);
sendMessage(delay_time * 7, msg, "rgb,4," + sunset);
sendMessage(delay_time * 8, msg, "rgb,2," + existing[2]);
sendMessage(delay_time * 9, msg, "rgb,5," + sunset);
sendMessage(delay_time * 10, msg, "rgb,3," + existing[3]);
sendMessage(delay_time * 11, msg, "rgb,6," + sunset);
sendMessage(delay_time * 12, msg, "rgb,4," + existing[4]);
sendMessage(delay_time * 13, msg, "rgb,7," + sunset);
sendMessage(delay_time * 14, msg, "rgb,5," + existing[5]);
sendMessage(delay_time * 15, msg, "rgb,6," + existing[6]);
sendMessage(delay_time * 16, msg, "rgb,7," + existing[7]);

/**
* @param {number} delay
* @param {NodeMessage} msg
* @param {string} payload
*/
function sendMessage(delay, msg, payload) {
    const m = RED.util.cloneMessage(msg)
    m.payload = payload
    m.delay = delay
    node.send(m);
}

```

Just feed that to a delay node set to have variable delay (instead of set timeouts, callbacks etc)

example:

```auto
[{"id":"16518bb781195249","type":"debug","z":"90e2df527c725ffb","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":280,"wires":[]},{"id":"5e2e7772c79d63e0","type":"function","z":"90e2df527c725ffb","name":"function 2","func":"\nconst delay_time = msg.delay_time | 400;\nconst sunset = msg.sunset || \"196,10,0\";\n\n// Ensure msg.existing is an array\nlet existing = msg.existing\nif (!Array.isArray(existing)) {\n existing = []\n}\n\n// Ensure elements 0 ~ 7 have a _some value_\nfor (let index = 0; index < 8; index++) {\n existing[index] = existing[index] || \"0,0,0\"\n}\n\nsendMessage(0, msg, \"rgb,0,\" + sunset);\nsendMessage(delay_time * 1, msg, \"rgb,1,\" + sunset);\nsendMessage(delay_time * 2, msg, \"rgb,1,\" + sunset);\nsendMessage(delay_time * 3, msg, \"rgb,2,\" + sunset);\nsendMessage(delay_time * 4, msg, \"rgb,0,\" + existing[0]);\nsendMessage(delay_time * 5, msg, \"rgb,3,\" + sunset);\nsendMessage(delay_time * 6, msg, \"rgb,1,\" + existing[1]);\nsendMessage(delay_time * 7, msg, \"rgb,4,\" + sunset);\nsendMessage(delay_time * 8, msg, \"rgb,2,\" + existing[2]);\nsendMessage(delay_time * 9, msg, \"rgb,5,\" + sunset);\nsendMessage(delay_time * 10, msg, \"rgb,3,\" + existing[3]);\nsendMessage(delay_time * 11, msg, \"rgb,6,\" + sunset);\nsendMessage(delay_time * 12, msg, \"rgb,4,\" + existing[4]);\nsendMessage(delay_time * 13, msg, \"rgb,7,\" + sunset);\nsendMessage(delay_time * 14, msg, \"rgb,5,\" + existing[5]);\nsendMessage(delay_time * 15, msg, \"rgb,6,\" + existing[6]);\nsendMessage(delay_time * 16, msg, \"rgb,7,\" + existing[7]);\n\n/**\n* @param {number} delay\n* @param {NodeMessage} msg\n* @param {string} payload\n*/\nfunction sendMessage(delay, msg, payload) {\n const m = RED.util.cloneMessage(msg)\n m.payload = payload\n m.delay = delay\n node.send(m);\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":280,"wires":[["2b60646066e7c095"]]},{"id":"c2918b5f5e94fbc0","type":"inject","z":"90e2df527c725ffb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":280,"wires":[["5e2e7772c79d63e0"]]},{"id":"2b60646066e7c095","type":"delay","z":"90e2df527c725ffb","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":280,"wires":[["16518bb781195249"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 11:45 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/6 "2024-04-21T11:45:53Z")

</div>

Ah, that's nice!

I like how you use the `delay` node to do the timing rather than how I did it.

Good news, bad news.... (Sorry.)

LED `6` is still not playing the game.

What's happening - just to be clear:  
Normally - other LED's - do this:

Get turned on.  
Stay on. (as the _bar_ goes past them) then go off.

LED `6` isn't doing that.

Dunno if I can make a video of the LED's....

I'll try to (poorly) explain it graphically (ascii art)

(start - all LEDs off)

```auto
. . . . . . . .
. . . . . . . R
. . . . . . R R
. . . . . R R R
. . . . R R R .
. . . R R R . .
. . R R R . . .
. R R R . . . .
R . R . . . . .

```

That isn't a perfect depiction.  
Alas there are subtle things happening between the actual things shown.

But I hope it shows you what I am seeing and maybe you can see where the code is failing.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 11:59 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/7 "2024-04-21T11:59:39Z")

</div>

I'll try, but do you know if I can post videos to the forum?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 12:15 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/8 "2024-04-21T12:15:16Z")

</div>

Here's a few screenshots of the problem.

In time order.

So top is first, going down the screen is going forward in time.

# EDITED

Better version with more information seen.

 ![Screenshot from 2024-04-21 22-17-15](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58e940b06f66a7eac8e04766506c0e73ee594447.jpeg)

 ![Screenshot from 2024-04-21 22-17-19](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c4e4c6f976a03dc209eb9e6df3f8c12da587089.jpeg)

 ![Screenshot from 2024-04-21 22-17-22](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf248067e604ab077c19ece88b0a0ea70819c328.jpeg)

 ![Screenshot from 2024-04-21 22-17-30](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf4149b71570f0a7989c109f980b30164e1d68cc.jpeg)

 ![Screenshot from 2024-04-21 22-17-33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf2f7c7dce0e1a5e45cc294de4deb9fae11fdb5d.jpeg)

 ![Screenshot from 2024-04-21 22-17-43](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/9/3966b55afee736f0712ed741acb9a65f90fd32e7.jpeg)

 ![Screenshot from 2024-04-21 22-17-46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74bd96a16ba090312524bb285fc47426f54ba73b.jpeg)

 ![Screenshot from 2024-04-21 22-17-50](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac5be2e0a0bbe9585b0b3029003f90d5448c3492.jpeg)

I hope that helps you see what is happening better.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 12:43 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/9 "2024-04-21T12:43:33Z")

</div>

Stepping back and looking at this, I am confused about something else new now.

The `msg.delay` value.

Originally I had to increase the `message_delay` value to control the speed at which the `function` node spat out the messages - I think. (Never really understood it. It just kinda worked.)

This new way all the messages are sent through the `delay` node set to `rate limit` and that value is determined by `msg.delay` to control the speed at which they are sent.

So why does that need to increase?

It gets _message 1_ (ok, no delay) then gets _message 2_ with a delay. I'm taking it that the delay is applied to the time BEFORE sending the next message.  
Ok, so ITMT, _Message 3_ arrives with a `msg.delay` value.  
That is queued and sent it's value of `msg.delay` time after it has sent _message 2_.  
So that would be the same value as the time between _message 1_ and _message 2_ - yes?

Sorry, I seem to be missing something there too.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 April 2024 13:30 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/10 "2024-04-21T13:30:54Z")

</div>

> [@Trying\_to\_learn](#):
>
> So why does that need to increase?

Because all messages are sent in sequence immediately (but in order). If you set all delay values the same, they will all be delayed the same amount of time and all release at the same time. That is how the delay node is designed to work - UNLESS - you set the mode to "rate limit" where it will space them out.

* * *

As for your issue, I really dont know what your issue is.

Perhaps if you list out EXACTLY what you expect to be sent, i can better help.

e.g.

| Seq | Time (ms) | Payload | Expected LED Status |
| --- | --- | --- | --- |
| 1 | 0 | rgb,0,196,10,0 | `0 0 0 0 0 0 0 R` |
| 2 | 400 | rgb,1,196,10,0 | `0 0 0 0 0 0 R R` |
| 3 | 800 | rgb,1,196,10,0 | `0 0 0 0 0 R R 0` |
| ... | ... | ... | ... |
| 16 | 6400 | rgb,7,0,0,0 | `0 0 0 0 0 0 0 0` |

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2024 13:42 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/11 "2024-04-21T13:42:49Z")

</div>

> [@Trying\_to\_learn](#):
>
> I'll try, but do you know if I can post videos to the forum

I recommend using the ScreenToGif app. Captures what's on screen into a video that you can save out as an animated gif which you can post here.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 21:35 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/12 "2024-04-21T21:35:34Z")

</div>

Thanks.  
I'll look for it and ....

Is it for Ubuntu?  
(Sorry, just checking.)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 21:37 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/13 "2024-04-21T21:37:02Z")

</div>

# SORRY ALL!

I am stupid - you don't have to agree right away. 😉

Alas I had written some other code and not really checked it 100%.  
It was running when it shouldn't have been.

Which was causing the problem with LED 6 being turned off.

Found the problem, fixed the code, now all working fine.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2024 21:39 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/14 "2024-04-21T21:39:30Z")

</div>

> [@Trying\_to\_learn](#):
>
> Is it for Ubuntu?

Windows.

> **[ScreenToGif - Record your screen, edit and save as a gif or video](https://www.screentogif.com)**
>
> Free screen recorder tool. Record, edit and save as a gif or video.

There are probably equivalents for Linux.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [21 April 2024 21:39 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/15 "2024-04-21T21:39:49Z")

</div>

☹  
I'm Ubuntu.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 April 2024 21:40 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/16 "2024-04-21T21:40:34Z")

</div>

> **[Best GIF Recorder Tools for Linux](https://itsfoss.com/best-gif-recorder-linux/)**
>
> Brief: We list the best Linux GIF recorders for Linux in this article and show you how to use these tools to record your screen as GIF in Linux.
> 
> 
> 
> 
> 
> When we talk of screen recording tools for Linux, most of us think about video recording. But, for...

---

<div class="post-metadata">

**Author:** ![KilianHertel](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@KilianHertel](https://discourse.nodered.org/u/KilianHertel)\
**Post date:** [25 April 2024 08:01 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/17 "2024-04-25T08:01:17Z")

</div>

I am just curious. Do you need just an animation? Why are you not saving all your animation steps in an array like:  
animation array = [  
{led1:'off',led2:'off',led3:'off',led4:'off',led5:'off',led6:'off'.led7:'off'},  
{led1:'off',led2:'off',led3:'off',led4:'off',led5:'off',led6:'off'.led7:'on},  
{led1:'off',led2:'off',led3:'off',led4:'off',led5:'off',led6:'on'.led7:'on},  
{led1:'off',led2:'off',led3:'off',led4:'off',led5:'on',led6:'on'.led7:'on'},  
{led1:'off',led2:'off',led3:'off',led4:'on',led5:'on',led6:'on'.led7:'off'},  
... etc.  
]

and just cycle through the array with a set timeout When a message appears. Or is there more logic you need?

And after drawing one part of the array you just set the timeout to draw the next =\> until it is done.

Did I miss something in your logic?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [25 April 2024 09:05 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/18 "2024-04-25T09:05:12Z")

</div>

Thanks.

Alas I am stuck between _keeping it simple_ and trying to explain the complicated story on how the LED's are controlled.

Their conditions are controlled by MANY factors. ☹ (Well, that's life for them)

I really stuffed up with some other code controlling LED 6 which was causing it to go blank as it was.

---

<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:** [9 May 2024 09:05 UTC](https://discourse.nodered.org/t/this-code-nearly-works-help-needed-function-node-js-code/87381/19 "2024-05-09T09:05:31Z")

</div>

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