I am working on a project where I am required to work with a WS2812B LED strip that have 360 LEDs. I am using Adafruit_NeoPixel.h library to drive the LED strip. I have a class called lights which is responsible for driving the LED
s. Here is a snipped of my code
//---- in Lights.h
private:
Adafruit_NeoPixel m_LedStrip;
//----------------------------------------
//--- in Lights.cpp
Lights::Lights()
{
m_LedStrip = Adafruit_NeoPixel(360, m_LedPin, NEO_GRB + NEO_KHZ800);
#if defined (AVR_ATtiny85)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
m_LedStrip.begin();
Serial.print("m_LedStrip.numPixels() is ");
Serial.println(m_LedStrip.numPixels());
}
//----------------------------------------
Now, m_LedStrips.numPixels() shows that it have 0 pixels. Strangely it works when I put the number of pixels to something smaller. 360 also works on my prototype code that only has lights control. I suspect that this might be a memory issue but my Arduino memory is not even full. Here are the memory usage stats.
Program size: 9,186 bytes (used 30% of a 30,720 byte maximum) (1.64 secs)
Minimum Memory Usage: 928 bytes (45% of a 2048 byte maximum)
Can someone give some advice as to how I can fix this issue? Thanks!
Yes you're running out of memory. The Neopixel library uses 3 bytes per pixel = 1080 bytes. This (like all local variables) isn't allocated until you run the sketch so it doesn't show in the compile-time memory figures. The figure 928 that the compiler gives there is only the global variables.
1080 + 928 = 2008, which only leaves 40 bytes for all your other run-time storage.
You might be able to squeeze the rest of the sketch down - share the code here and people will have suggestions.
Oh, I see. Here is another question for squeezing the code down. I have quite a few Serial.println in my code and it seems like strings are chewing up memory super fast. Is there any way to store those strings on flash memory? Thank you.
Look at the f macro.
Serial.println(F("This string will be stored in flash memory"));
.
Instead of squeezing the file down, you could squeeze the processor up.
Try a mega.
BulldogLowell:
Instead of squeezing the file down, you could squeeze the processor up.
Try a mega.
That would probably be the best solution to this problem if it was possible. The system I am working on is already well established at this point, and the usage of so many LED lights was unexpected by the designer (It was me lol. I did not account for future expansions
)
Well, there are other ways to write to these addressable leds without a buffer for every color, for every pixel.
Do you need to address a single led, or are you happy just shifting the color of the entire strip?
I don't need to access every single led, but I still need some control over them. Basically, I can't just initialize the whole 360 pixels at once, since different sections of the strip need to be different colors. Having said that, what was your proposal? I can't think of any way to do what you are proposing and I am very curious as to how this can be done.