Calling multiple constructor class instances in a for loop.

I'm working with LED strips (like plenty of others) and I've created a sequence of what I call fireworks on a single strip. Each shell is created using a constructor class and updated in each iteration of the loop to allow for overlaps, random lifespans and different colors. Even though it's not a difficult prospect to type out one line to update each shell, I'd rather place a for statement inside the main loop that uses a variable to determine how many shells to update each time. My understanding is that each instance of the FireworkShell that is created needs to be placed inside of an array I can parse through, but I don't know what the correct syntax is to get that working.

Class Statement

class FireworkShell {
    int stringPos;    //origin of the blast
    int age;          //how long the blast has been on the string
    int maxLife;      //how old this one can get
    uint8_t hue;       //color of the shell

  public:
    FireworkShell(int xPos, int howOld, int deathAge, uint8_t shellColor) {
      stringPos = xPos;
      age = howOld;
      maxLife = deathAge;
      hue = shellColor;
    }

    void Update() {

      if (age < maxLife) {
        int newPosR = stringPos + age;
        int newPosL = stringPos - age;
        if (newPosR < NUM_LEDS) leds[newPosR] +=  CHSV(hue, 255, 255);
        if (newPosL >= 0) leds[newPosL] +=  CHSV(hue, 255, 255);

        if (age > 0) {


          int tailR = newPosR - 1;
          int tailL = newPosL + 1;
          if (tailR > 0 && tailR < NUM_LEDS) {
            leds[tailR] = CHSV(hue + 40, 255, random(50, 100));
          }
          if (tailL > 0 && tailL < NUM_LEDS) {
            leds[tailL] = CHSV(hue + 40, 255,  random(50, 100));
          }



        }

      }
      if (age < maxLife + 20) {
        age++;
      } else {
        age = 0;
        hue = random(0, 213);
        maxLife = random(5, lifespan);
        stringPos =  random(0, NUM_LEDS);
        //Serial.println(maxLife);
      }
    }
};

Instancing

FireworkShell shell1(random(0, NUM_LEDS), 0, random(5, lifespan), 171);
FireworkShell shell2(random(0, NUM_LEDS), 0, random(5, lifespan), 0);
etc . . .

Main loop

void loop() {
  EVERY_N_MILLISECONDS(100) {
    fadeToBlackBy(leds, NUM_LEDS, 30);

    shell1.Update();
    shell2.Update();
    /* etc . . . */
    FastLED.show();
  };
}

I'd even love to get around writing out the creation of each instance, instead leaving them all the be created via another loop with random colors and just set a totalShells variable to feed into both the instancing and the update calls in the loop.

Would someone mind telling me how I could go about doing this or point me in the right direction?

make an array of instances

for(int i = 0; i < whatever number; i++)
{
    FireworkShell[i] (/* constructor info here */) ;
}

So I took that and placed it inside of a new function:

void createShells() {
  for (int i = 0; i <= 5; i++)
  {
   FireworkShell[i] (shell+i(random(0, NUM_LEDS), 0, random(5, lifespan), 171)) ;
  }
}

but that gets me:

myFireworks.ino: In function 'void createShells()':
myFireworks:71: error: expected unqualified-id before '[' token
expected unqualified-id before '[' token

I recalled there having to be a type in front of an array declaration, so I tried putting INT before FireworkShell, but that popped a whole slew of different errors.

You need to take your snippets to http://snippets-r-us.com, where you will get excellent advice.

jedimasta:
Instancing

FireworkShell shell1(random(0, NUM_LEDS), 0, random(5, lifespan), 171);

FireworkShell shell2(random(0, NUM_LEDS), 0, random(5, lifespan), 0);
etc . . .

Try something like:

FireworkShells[] = {
    FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), 171),
    FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), 0)
}

'FireworkShells' does not name a type

is what I get there.

jedimasta:
is what I get there.

FireworkShell shells[] = {
    FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), 171),
    FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), 0)
};

Great, that gets me my array, but now my loop fails. It doesn't like my length check on the FireworkShell shells array. I'm assuming it's a syntax issue again:

 for(int i=0;i<FireworkShell shells.length;i++){
      shells[i].Update();
    }

Or is this a case where I have to set the array length manually ahead of time?

for(int i = 0; i < sizeof(shells)/sizeof(shells[0]); i++){

This is C++ not Java or the crude mixture you seem to use.

In C++11 you can loop over an array using this new syntax:

for (auto &shell: shells) {
    shell.Update();
}

oqibidipo:
...using this new syntax:

new, yeah...like my new iPhone 4?

:slight_smile:

Whandall:
This is C++ not Java or the crude mixture you seem to use.

Oh jeez. Yeah, yer right. I've been staring at javascript for the past week on a separate project. It's all starting to bleed together. Thanks for the clarification.