Neopixels state from last sketch retained

Hello All,

I am experiencing something strange (probably because of my own lack of knowledge). I am just experimenting with WS2812 led strips, at the moment I have one with 56 LEDs connected to Arduino (both power and data pins). When I upload a sketch (say, Adafruit example, strandtest; this is just an example, but this happens with anything I upload) and use a large number, any number, of LEDs for the animation e.g. 56 or 50 or whatever number… the first time I upload the sketch, everything works fine and as expected. Now, while the sketch is running and Arduino is connected, I modify the sketch, change the number of Leds to a lower number, say 5 or 10 and then re-upload the sketch. This time, the new sketch only affects the new number of Leds, but the other Leds remain in the same state as they were in the previous sketch (just stuck in the state they were in when new sketch was uploaded)

So for example, if they were showing a particular color when the new sketch was uploaded, they would retain the same color, while the number of LEDs in the new sketch would continue with their animation.

I would expect the new sketch would just turn everything off before it initializes the strip but for some reason it is not the case? What am I missing? I have tried to use strip.clear(); but it only clears the new number of leds new sketch initializes the strip with new number.

Is there a way to turn everything off before a new sketch gets uploaded without plugging arduino out of the computer everytime I need to upload a new sketch with different number of Leds?

One way I could think of was to initialize the strip with the max number of LEDs anyways, call strip.clear(); then only use limited number of leds in animations. But that would mean I won’t be able to use strip level functions anymore such as strip.fill();

Is this normal behaviour or am I missing something?

The neopixels have their own state, and won’t turn off unless you specifically tell them too.
(Especially if you have a smaller number in the new sketch. What’s it supposed to do: guess how many neopixels MIGHT be out there?)

1 Like

Write and keep open on you desktop a sketch for the same kind of strip that just

turns off way more LEDs than you have

upload it whilst thinking and tinkering. Save your eyes, save power &c.

The same small sketch is good for finding the end of a strip. I use strips of various lengths and got sick of counting… I’ll leave it to you to write the hack that can rapidly help you determine how many are actually present.

It’s good also for testing, again your own opinion will inform what kind of functionality you want for that kind of thing.

Being a small sketch, it will whistle right up and do its thing. All set for the real sketch when it’s ready fir another go.

HTH

a7

1 Like

Write and keep open on you desktop a sketch for the same kind of strip that just
turns off way more LEDs than you have

Thanks. This is a good idea!

Now this would be above my paygrade I suppose. What I have gathered so far by reading around on this topic is that I would at least need to feed the other end of the strip back into the Arduino if I wanted to detect the number of pixels and that would just be an overhead for me, my strips don’t have any wires connected to the other end.

If there is a pure software hack for this kind of thing, I would definitely like to learn if you don’t mind sharing? Thanks.

Haha, no.

Here’s a crude hack:

Just light up one pixel at or near the number you think. Like I might think I have 250, so I just light up one at, say 247 and count the rest, or sit there and do a kind of guessing game simply editing my code.

Don’t forget that they index from 0, so if number 59 is the last, you’ve 60 pixels.

Like I say, crude and def not automatic.

But this has got me thinking - I haven’t done yet, but something that lit up every tenth pixel would make it easy to count by ten and then a few extra at the end.

A simple program is good for testing colors too, yup, you guessed it, just tweak and upload. Again, an opportunity for some less lazy more ambitious than I to write something that would be a bit more clever.

Mainly I use it to turn off the LEDs because, as I may have said, they be very bright and sometimes a programming error lands you up with them all blazing, scare the cat &c.

Another test this facilitates is verifying that you have specified the correct WRGB type of neopixel, man do things get weird when you talk to RGBs like they were WRGBs or whatever.

a7

1 Like

The number of leds you define in your sketch basically determines how many clock pulses get send down the wire, advancing the data to the next, etc. You could always clock out 200 pixels worth of 0’s to turn off everything and the go about your actual code. If you every want to connect more than 200, make it bigger. There is no harm in clocking out more data than available leds other than a waste of time, but that is small on the human scale.

I get this, but the problem is that I need to initialize the strip with that big number right? then, when I actually want to use a smaller number, I wouldn’t be able to use the same strip object as it will try to perform every operation on that initially used big number right? So I think best solution so far is to use a different sketch altogether for this, reset everything and then come back to the actual sketch as proposed by alto777.

cool tips :+1: but most of all really like that reset sketch idea. Thanks again.

If you mean always clocking out a large number during the regular operation of a program that only has/uses fewer, the waste of time can be a problem.

I do animations at a high frame rate and is is not untypical for the strip.show (strip show, that always kills me) to consume a fair amount of the frame time. So I def want to use the number of pixels that actually need data.

Also you’ve eaten up memory with the pixel buffer which is a waste.

The APA102 has data and clock lines, here you can “manually” clock out 0s all day long yet have only the real amount of storage reserved when using the library mechanisms to set and transmit the strip contents to the physical strip.

If that’s not what you meant, nevermind. :wink:

a7

You missed the key part of the quote @alto777

and then go about your actual code

I saw that but found it to be ambiguous. So you are talking about one sketch.

The simple program below just flashes a ten pixel strip blue and green. That’s the “actual” part.

How would I modify it to use your idea of first clearing a strip of 200 (more than more than enough) LEDs that may be physically present?

# include <Adafruit_NeoPixel.h>

# define PIN 6
# define NLAMPS	10

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NLAMPS, PIN, NEO_GRBW + NEO_KHZ800);

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

void loop() {

	for (int ii = 0; ii < 10; ii++) {
		strip.setPixelColor(ii, 0x00000040); // blue
	}	
	strip.show(); delay(100)

	for (int ii = 0; ii < 10; ii++) {
		strip.setPixelColor(ii, 0x00040000); // green
	}	
	strip.show(); delay(100);	
}

With the APA102 LEDs, i’d just clock out 0s enough. Using the library, I am missing the trick you must have in mind. Same with fastLED, again missing any trick.

a7

Unfortunately, there isn’t any real trick. The easiest way would be to copy the .show() function but do not update the pixel pointer so it just clock out the same starting pixel as much as you want.

Yes, OK. Modify the library code, easy.

Thanks.

a7

I am not advocating modifying the library. Just use it as a guide to create your own function that does almost everything .show() does, but without needing a large array of pixel data.

You could try .updateLength(), though it is marked deprecated:

/*!
  @brief   Change the length of a previously-declared Adafruit_NeoPixel
           strip object. Old data is deallocated and new data is cleared.
           Pin number and pixel format are unchanged.
  @param   n  New length of strip, in pixels.
  @note    This function is deprecated, here only for old projects that
           may still be calling it. New projects should instead use the
           'new' keyword with the first constructor syntax (length, pin,
           type).
*/
void Adafruit_NeoPixel::updateLength(uint16_t n) {

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