Hi, I’m trying to do a chase pattern where a new tail gets added to the pattern in response to a cloud event occurring. And, depending on the cloud event, I want each tail to be a different color. So, I need a way to store the order of colors–trying to use a dynamic 2d array to do this.
It seems I need to declare the array with a set number of dimensions. But, I want the dimensions to increase as more cloud events occur. Is there a way to add more and more rows to an array dynamically in real time like this?
Here is my code:
#include "application.h"
#include "neopixel/neopixel.h"
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 864
#define PIXEL_PIN D1
#define PIXEL_TYPE SK6812RGBW
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int ideaCount = 0;
int colCount = 3;
int tailCount = 0;
uint8_t** ary = new uint8_t*[50];
void setup() {
delay(2000);
strip.begin();
strip.show();
// We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
// This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.
Particle.variable("ideaCount", ideaCount);
Particle.function("led",ledToggle);
for(int i = 0; i < 50; i++) {
ary[i] = new uint8_t[3];
}
ary[0][0] = 0;
ary[0][1] = 100;
ary[0][2] = 0;
}
void loop() {
chase();
}
// We're going to have a super cool function now that gets called when a matching API request is sent
// This is the ledToggle function we registered to the "led" Particle.function earlier.
int ledToggle(String command) {
/* Particle.functions always take a string as an argument and return an integer.
Since we can pass a string, it means that we can give the program commands on how the function should be used.
In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
Then, the function returns a value to us to let us know what happened.
In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
and -1 if we received a totally bogus command that didn't do anything to the LEDs.
*/
if (command=="idea") {
// Do not run more than 15 seconds of these, or the b/g tasks
// will be blocked.
//--------------------------------------------------------------
ary[ideaCount][0] = 200;
ary[ideaCount][1] = 200;
ary[ideaCount][2] = 0;
ideaCount++;
return 0;
}
else if (command=="comment") {
ary[ideaCount][0] = 0;
ary[ideaCount][1] = 0;
ary[ideaCount][2] = 200;
ideaCount++;
return 1;
}
else {
return -1;
}
}
void chase() {
for(uint16_t i=0; i<strip.numPixels()+(ideaCount*15); i++) {
//tailCount = 0;
for(uint16_t j=0; j<ideaCount*15; j=j+15) {
strip.setPixelColor(i-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 200)); // Draw new pixel
strip.setPixelColor(i-1-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 100)); // Draw new pixel
strip.setPixelColor(i-2-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 75)); // Draw new pixel
strip.setPixelColor(i-3-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 50)); // Draw new pixel
strip.setPixelColor(i-4-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 25)); // Draw new pixel
strip.setPixelColor(i-5-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 25)); // Draw new pixel
strip.setPixelColor(i-6-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 10)); // Draw new pixel
strip.setPixelColor(i-7-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 0)); // Draw new pixel
strip.setPixelColor(i-8-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 0)); // Draw new pixel
strip.setPixelColor(i-9-j, strip.Color(ary[(j/15)][0], ary[(j/15)][1], ary[(j/15)][2], 0)); // Draw new pixel
strip.setPixelColor(i-10-j, 0);// Erase pixel a few steps back
//tailCount ++;
}
strip.show();
delay(25);
};
};