The Node-Red flow works perfectly. But now I want to create an infinite loop with the rainbow.
This is the code from the flow:
var numberOfLEDs = 120; for(i=0; i<255; i++){ for(j=0; j<numberOfLEDs; j++){ var pos = 0; pos = Math.round(((j*255/numberOfLEDs)+i))&255; if(pos<85){ var red = pos*3; var green = 255-pos*3; var blue = 0; } else if(pos < 170){ pos -= 85; var red = 255 - pos*3; var green = 0; var blue = pos*3; } else{ pos -= 170; var red = 0; var green = pos*3; var blue = 255-pos*3; } var setColor = j+','+ red +','+ green +','+ blue; node.send({payload:setColor}); } } Can I change the begin lines like this: var numberOfLEDs = 120; while(true){ for(i=0; i<255; i++){ for(j=0; j<numberOfLEDs; j++){ ........... ........... }
I hope you tell me if this is correct.
Thank you and regards,
Yvonne
Hi Yvonne.
I can’t tell just looking at the code. You need to experiment and see what you get.
There’s a similar function written in Python. Maybe you can take a look at it and adapt it to use with Node-RED.
Here’s the example: https://github.com/jgarff/rpi_ws281x/blob/master/python/examples/strandtest.py Take a look at the rainbow() and rainbowcycle() functions.
I hope this helps.
Regards,
Sara
We also have this tutorial that controls a WS2812B LED strip using Node-RED with Raspberry Pi.
Might be helpful: https://randomnerdtutorials.com/node-red-with-ws2812b-addressable-rgb-led-strip/
It has an option for a rainbow effect.
Regards,
Sara
Hi Sara,
The code above comes from https://randomnerdtutorials.com/node-red-with-ws2812b-addressable-rgb-led-strip/.
The only line of code I added is:
while(true){ and at the end }
But I don’t know if this is correct, because the code did not run, so I wandered if you could see if anything is wrong.
Thank you for your help.
Yvonne
Hi.
But are you following that specific project? Or are you trying to adapt to other project?
Do you have a strip connected to the Raspberry Pi?
You need something that triggers that node to run. In that case, it is a button.
Regards,
Sara
Dear Sara,
I followed the project, the code is triggered with the button as it is in the project described in your tutorial.
In this tutorial the LED strip changes 1 time the whole rainbow cycle and then it stays as it is.
I want the rainbow cycle infinite, so my only question is:
can I add the line while(true) above the code that you wrote in the project?
Is this correct?
Thanks again.
Regards,
Yvonne
Hi.
Now I understand what you want to do.
I think putting the while true makes sense and that it should work to make the rainbow effect continuously.
What issue are you facing with that?
Regards,
Sara
Hi Sara,
The problem I encountered is that the code is not running when I change the code as suggested. It is as if the whole Raspberry Pi is frozen. That’s why I asked if the code is correct. If so, I have to try again.
Thank you and regards,
Yvonne
Hi.
You are right.
If you try to run a while loop inside the function node it will crash.
We modified the flow, so that it runs the same function after it is over.
We’ve also created a new button to stop the rainbow effect.
Once you press the STOP button, it will only stop the effect after it completed an entire raibow loop.
You can import the node by copying the code in this link:https://gist.github.com/sarasantos/587f08dfef312a428df4f89bc5555c64
Tell me if it works for you.
Regards,
Sara
Dear Sara,
I have an additional question.
I attached my WS2812 LED strip to a Raspberry Pi zero W.
I use a 5V 2A adapter. What is the max. of LED’s that I can use in the strip?
In other words: I have a strip with 120 LED’s (2m). Do I have to cut it and if so, on what length?
If I use a 3A adapter is it then possible to have a longer strip?
Is the limitation the Raspberry Pi GPIO’s or is it the adapter?
I have build the circuit exactly as mentioned in your tutorial, so with the level converter.
Thank you for your help and patience.
Yvonne
Hi.
You can determine the amps you need from the amount of power each LED requires.
An individual LED draws up to 60 mA at full brightness (which produces white light), but since you’ll rarely need all LEDs at their maximum value for any length of time, you can safely estimate 20 mA per LED.
So if your strip is 120 LEDs long, you’ll need a 5 V power source with approximately 120 × 14 = 1680 mA.
So, 5V 2A should be fine on the strip’s +5V pin.
Try it out and see what you get.
Regards,
Sara
Hi Sara,
The 5V 2A power supply: is this the one you use for the Raspberry Pi or is it an extra external power supply?
In your tutorial, I see the Led strip attached to the Raspberry Pi. But then I saw this page on the internet and I started to doubt. Here they use an external power supply for the Led strip, but they don’t use the a converter.
https://tutorials-raspberrypi.com/connect-control-raspberry-pi-ws2812-rgb-led-strips/
Thank you for your help.
Regards,
Yvonne
Hi.
In our tutorial the strip is being powered by the Raspberry Pi because we’re controlling a small number of LEDs.
For 120 LEDs you should use an external power supply.
Regards,
Sara
Hi Sara,
Do you ever sleep? I suppose it’s still very early where you are.
Thanks again. I will build the circuit and try the new code.
I’ll let you know.
Regards,
Yvonne
Hi Yvonne.
I usually work all day.
But I sleep quite well (at least 8 hours).
It’s almost midday (so, it’s not that early:)).
Anyway, then let me know how the project went.
Regards,
Sara
Hi Sara,
I thought you live in the USA, but now I see you live in Portugal, so we are in the same time zone.
Sara, if I use an external power source, can I delete the level converter and attach DIN from the LED strip directly to GPIO18?
Thank again.
Yvonne
Hi.
The level converter is used to convert the 3.3V signal into a 5V signal. So, in theory you need the logic level converter.
However, we’ve tested the project with and without the logic converter and it worked fine in both scenarios.
But, using the level converter is a good practice. You may get unexpected results if you don’t use it (but it always worked fine for me).
Regards,
Sara
Hi Sara,
I’m very sorry to say, but the new code you send me doesn’t work at all.
I have tried it with an external power adapter of 5V 2A and with and without the level converter.
The original rainbow code works fine, only with the shortcoming I mentioned before.
I don’t understand why it doesn’t work, but I really tried every combination with power supply and level converter I could image.
So, thanks for your help.
Regards,
Yvonne
Hi.
What do you mean by it “doesn’t work”? How does it behave? Do you get any errors? Or the strip simply doesn’t do anything?
It worked for us. We tested the new code before sending it to you.
If the other code works, it is not something related with the power supply. The other code worked with as much LEDs, but the newer code didn’t?
Regards,
Sara
Hi Sara,
That’s right.
The ‘old’ code works perfect, but with the disadvantage I mentioned earlier. The ‘new’ code does everything, except the rainbow. In the debug window I can see that something is happening, but the leds don’t lit up.
I have made a print screen, but I don’t know how I can upload it to you, so you can see what is happening.
In the debug window there is this text:
msg.payload:string[11] “25, 153, 102, 0”
msg.payload:string[11] “26, 159, 96, 0”
msg.payload:string[11] “27, 165, 90, 0”
And so on.
The edit delay node is counting down and stops by zero.
I hope this helps for you to understand what is going on.
Thank you so much.
Regards,
Yvonne
Hi again.
Accordingly to what you described, it should be working.
To share an image, you can share a link to imgur, google drive or dropbox, for example.
If you can share the printscreen, it can help.
I’ll take a look again at the project and see if I can find out what might be happening.
Regards,
Sara
Hi.
I received your print screen in my email.
And it seems that everything is working properly. I don’t know why you don’t get it working.
It worked fine for us, and in your printscreen the debugging window was printing the right information.
I’m out of ideas on what might be wrong :/
Hi Sara,
Still searching for a solution.
When running the loop, I always see node: f188 etc and msg.payload: string[12].
At the end I see node: WS2812B LED STRIP
“Invalid payload : LOOP”
It is in capital letters. I can’t find anywhere LOOP in de nodes/code, only loop (small letters).
Could this be the problem?
The msg.payload should be loop.
5-6-2020 20:15:07node: f188899a.75f3e8msg.payload : string[12]
“119,0,243,12”
5-6-2020 20:15:07node: f188899a.75f3e8msg.payload : string[4]
“loop”
5-6-2020 20:15:07node: WS2812B LED STRIPmsg : string[22]
“Invalid payload : LOOP”
5-6-2020 20:15:07node: f188899a.75f3e8msg.payload : string[4]
“stop”
I used your code, so maybe you know if that is the problem.
Sorry for disturbing you again.
Thank you and have a nice weekend.
Regards,
Yvonne
Hi again.
We’ve built a different flow.
Try it now:https://gist.githubusercontent.com/sarasantos/587f08dfef312a428df4f89bc5555c64/raw/de6e9f005cb7eab6ebafc2205ff25b3333740faa/WS2812_RPi_Node_RED_loop_rainbow
Tell me if it worked.
Regards,
Sara