Led Strip Lights up Randomly When Code is in Loop() but Is fine in Setup()

Hello,

I am trying to light up a pair of 144 LED strips(sk6812 rgbw) from an Arduino nano (old bootloader) using the NeoPixelBus library.

When my LED related code is in the Setup() method the led strip lights up as expected, no random LEDs turning random colours:

#include <NeoPixelBus.h>

#define DataPin 4
#define NumLeds 144

NeoPixelBus<NeoRgbwFeature, Neo800KbpsMethod> strip(NumLeds , DataPin );
RgbwColor red(0,255,0,0);
RgbwColor off(0,0,0,0);

void setup() {
 //single led blink
  strip.Begin();
  strip.SetPixelColor(50,red);
  strip.Show();
  delay(1000);
  strip.SetPixelColor(50,off);
  strip.Show();
}

void loop()
{
}

^ Appears to Work!

However, I try to execute LED related code in the for loop, then I get mostly random LEDs lighting up and sometimes the expected behavior occurs:

#include <NeoPixelBus.h>

#define DataPin 4
#define NumLeds 144

NeoPixelBus<NeoRgbwFeature, Neo800KbpsMethod> strip(NumLeds , DataPin );
RgbwColor red(0,255,0,0);
RgbwColor off(0,0,0,0);

void setup() {
  strip.Begin();
}

void loop()
{
    //looping led blink
    strip.SetPixelColor(i,red);
    strip.Show();
    delay(1000);
    strip.SetPixelColor(50,off);
    strip.Show();
  
}

^ Doesnt work as expected

I’m not sure whats going on or how to debug this further. I think maybe it could be hardware related (using rgbw instead of rgb LEDs). I tried with the FastLED library and followed the rgbw hack HERE… but i still ran into the same issue.

Thanks for your time, and sorry if something doesn’t make sense.

I'm going to guess (I've not tried anything to be sure) that it's caused by the fact that immediately after

   strip.SetPixelColor(50,off);
   strip.Show();

You have

  strip.SetPixelColor(i,red);
   strip.Show();

I'm guessing that because you are sending red before the sending of off has had time to complete it's screwing things up.

Perry, you may be right, but a "Newbie" may not understand you.

Batman, keep in mind that the "Loop" will run over and over, with virtually no delay in between. Setup runs only once.

When doing it from setup, you turn it red, wait a second, turn it off. Done!

When doing it from loop, you turn it red, wait a second, turn it off and immediately restart the loop causing it to turn red immediately after having turned it off.

However, did you include the ENTIRE program for us to analyze? Where is the "i" defined for "strip.SetPixelColor(i,red);" ?

So yeah, that 'i' in the red call is suspicious, I assumed the OP had properly 50 there.

Then all you'd need to do to address the issue @PerryB and @JaBa raise (which explanations still might leabe a noob scratching his head) is to

Put a delay(1000) after the 2nd strip.Show call.

Strip show, haha, did I never notice that before? :wink:

But do try to get your mind around what they have pointed up. The setup/loop thing is the core of AArduino sketchery and in one form or another will crop in most embeds.

a7

PerryBebbington:
I'm going to guess (I've not tried anything to be sure) that it's caused by the fact that immediately after

   strip.SetPixelColor(50,off);

strip.Show();




You have



strip.SetPixelColor(i,red);
  strip.Show();




I'm guessing that because you are sending red before the sending of off has had time to complete it's screwing things up.

Ah yeah that makes sense Idk why but I thought I was automatically good because I had the delay in between the two.

JaBa:
Perry, you may be right, but a "Newbie" may not understand you.

Batman, keep in mind that the "Loop" will run over and over, with virtually no delay in between. Setup runs only once.

When doing it from setup, you turn it red, wait a second, turn it off. Done!

When doing it from loop, you turn it red, wait a second, turn it off and immediately restart the loop causing it to turn red immediately after having turned it off.

However, did you include the ENTIRE program for us to analyze? Where is the "i" defined for "strip.SetPixelColor(i,red);" ?

Thanks for the good explanation! Ill try adding a delay to ensure each signal doesn't get interrupted in the for loop. Also the "i" is supposed to be 50 as well, its just remnants of a for loop to light up all the leds, but after running into my issues I ran a more simple example.

alto777:
So yeah, that 'i' in the red call is suspicious, I assumed the OP had properly 50 there.

Then all you'd need to do to address the issue @PerryB and @JaBa raise (which explanations still might leabe a noob scratching his head) is to

Put a delay(1000) after the 2nd strip.Show call.

Strip show, haha, did I never notice that before? :wink:

But do try to get your mind around what they have pointed up. The setup/loop thing is the core of AArduino sketchery and in one form or another will crop in most embeds.

a7

lol yeah i might need to change the name to LEDstrip. Your are right I had 50 there instead of 'i', just messed up when posting to this forum. Thanks for the input and I'll try to get a better understand arduino's core coding environment.

I tried added the delay like stated above so the Loop() now looks like this:

void loop() {
  //looping blinking light
  delay(1000);
  strip.SetPixelColor(50,off);
  strip.Show();
  delay(1000);
  strip.SetPixelColor(50,red);
  strip.Show();
  
}

How ever I'm still getting random colors turning on near the start of the led strip. The red light sometimes turns as expected but not always.

“sometimes but not always “ is the hallmark of a hardware issue.

How is the strip wired and powered? A hand drawn schematic may provide a clue.

Everyone just leaves the object name “strip”, haha. I use “myStrip” or “lamps” just so I can see what I’ve done not from a tutorial or borrowed code.

HTH

a7

Here is my layout related to the LED strip and nano. I have a few other components just connected to the nano and shouldn’t be interfering with the LED circuit (like mpu-6050, micro SD, amplifier, button).

Okay, good.

I've not experimented with using less than 5 volts for LED strips. If you can, try the identical program but power you LEDs with real 5 volts from a supply with sufficient current capability.

If you are aiming this for a portable device powered from one lipo, time to look into active voltage regulators that can step up to 5 volts and provide the sufficient amperage.

They are cheap and work quite well I use them all time.

Later, if this turns out to be what's up. First try with 5 volts.

a7

Hmmm, I do have a boost converter (up to 5V) shipping in by the end of September that i can test with that, but I can't verify that's the problem till end of September.

In the mean time I think the amps might limit me here. If i do one 3.7v li-ion with a boost to 5v I get a limit from the boost converter of 1A, but I need about 6A to run all the leds of a single color. My other option would be to use two 3.7 in series and step down to 5V and I could get a limit of 5A, however my Project is a light saber, and the handle might not fit 2 li-ion batteries.

I guess its just trail and error now, and maybe 2 li-ion batteries is the best choice.

The one thing that throws me off is I've seen many lightsaber projects online with 2 or 3 strips of 144 leds running on just one 3.7v li-ion battery fine???.

Anyway now I'm rambling and I can't update this post till I get my new electronic parts.

Thanks for your time and help!

I'll be sure to update this post if the boost converter fixes it or I find another solution.

If you have no source of 5 volts yet then we'll stay tuned.

Those other projects may have a boost regulator.

Lastly, you are calculating max possible current draw. That's all LEDs on at max volume, so to speak. I have never come close to using that much current

  1. the LEDs are very bright even at reduced color values
  2. rarely are all LEDs on at once

so you may be able to get away with much less absolute capability.

The big capacitor should probably help if you current intermittently spikes up, a smaller supply might be able in this way to ride through short periods of high demand.

But test as it goes, you def don't want to sag on any of the supply lines and lipos should be kept well within their design parameters.

Don't ask how I know that…

a7

alto777:
the core of AArduino sketchery

Ha! I like that!

-jim lee

JaBa:
Perry, you may be right, but a "Newbie" may not understand you.

There's no right or wrong answer to this and each person has their own preferences. Mine is to give an answer that leaves someone with something to think about. My answer is complete but I deliberately did not explicitly state that immediately after the last line in loop, with no appreciable delay, the first line in loop is executed again. I wanted batman to think about it an realise for himself. If he didn't get it he could ask for clarification. If he did get it he'd get that wonderful feeling that comes with suddenly realising something new.

batman_von_vader:
Ah yeah that makes sense Idk why but I thought I was automatically good because I had the delay in between the two.

As I think you know by now there is no appreciable delay between those 2, note the order I put them in, the bottom one at the end of loop first then the top one at the start of loop, no delay between them.

Okay so I got the new component, a J5019 DC-DC Step Up Boost Module. I set it up so it outputs 5V DC and hooked it up to my battery and LEDs.

Sadly the random lights is still occurring so I don't thinks voltage is the issue. However the random lights occurred in both setup() and loop() this time so at least that's consistent now and its more likely a electronics issue.

It maybe could still be software issue, as libraries for RGBW LED strip are not well supported (however my gut tells me its probably not the issue).

I think my last option is to remove the wire connections between the ends of the two LEDs strip and test that. Then if that fails test each LED strip hooked up individually.

After that I might give lul. Idk.

Worst worst case I un-solder all the other components from the nano even though they are not being used software wise, as they might somehow be interfering with the LED strip. (took me awhile to put it together) :cry:

I'll create and upload an image of my full circuit, and maybe I'll find the issue while I'm drawing it out.

Thanks for all the help and support! You guys are great.