-
-
Notifications
You must be signed in to change notification settings - Fork 265
FAQ #3
Michael Miller edited this page Mar 19, 2016
·
6 revisions
I wish to dynamically set the number pixels, but I don't see a way to do this and other libraries expose a method to change the number of pixels like updateLength() or setPixelCount().
The key word here is dynamic. The best practice when dealing with key feature change is to dynamical create the object and recreate it when things change. It will often lead to smaller program code size even though you may have to type more. See the following snippet of code as an example of how to do this.
// declare your object as dynamic, a pointer to it, the *
// a good practice is to set it NULL
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod>* strip = NULL;
void setup() {
...
// inside setup, allocate your default, or maybe you don't do this and just wait for outside influence
PixelCountChanged(1);
...
}
void PixelCountChanged(uint16_t newCount) {
if (strip != NULL) {
delete Strip; // delete the previous dynamically created strip
}
strip = new NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod>(newCount, Pin); // and recreate with new count
strip->Begin();
}
void loop() {
// other parts of your code, you set the colors, and show
if (strip != NULL) {
strip->SetPixelColor(0, RgbColor(0));
strip->Show();
}
}