From 56a0210764a7b6d7eac2cb6e3e52a386b6fc31a4 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 19 Mar 2016 10:03:18 -0700 Subject: [PATCH] Created FAQ #3 (markdown) --- FAQ-#3.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 FAQ-#3.md diff --git a/FAQ-#3.md b/FAQ-#3.md new file mode 100644 index 0000000..4696cc4 --- /dev/null +++ b/FAQ-#3.md @@ -0,0 +1,31 @@ +### 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* 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(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(); + } +} + +``` \ No newline at end of file