Call by Reference with function and array with NeoPixels

I have an array of NeoPixels and would
send the whole array to a function.

It works fine if i send one array element "as pointer" but
i would like to send the whole arrays as pointer.
If i send the array direct in this way:

colorWipeAll(neoStrips, cWHITE, 100, 200);
it is compiling but not working with arduino and the neopixel.

Adafruit_NeoPixel neoStrips[] = {
  Adafruit_NeoPixel(NUM_NEO_PIXEL[0], NEOPIN[0], NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUM_NEO_PIXEL[1], NEOPIN[1], NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUM_NEO_PIXEL[2], NEOPIN[2], NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUM_NEO_PIXEL[3], NEOPIN[3], NEO_GRB + NEO_KHZ800)
};

I can pass it to my function:

for(int i=0; i<NUMSTRIPS; i++)
    colorWipe(&neoStrips[i], cGREEN, 10, 100);

void colorWipe(Adafruit_NeoPixel *strip, uint32_t c, uint8_t wait, int waiting) {
  for(uint16_t i=0; i < strip->numPixels(); i++) {
    strip->setPixelColor(i, c);
    strip->show();
    delay(wait);
  }
  delay(waiting);
}

which works fine!

But now i want to pass the whole NeoPixel Array like:

colorWipeAll(&neoStrips, cWHITE, 100, 200);

void colorWipeAll(Adafruit_NeoPixel *neoStrips[], uint32_t c, uint8_t wait, int waiting) 
{
...
}

But this does not work.
I get only "not convert 'Adafruit_NeoPixel (*)[4]' to 'Adafruit_NeoPixel**".
I have tried also different other ways but found no solution till now.

This declares an argument of type “pointer to pointer to Adafruit_NeoPixel”, which is not what you want. You want an argument of type “reference to array of Adafruit_NeoPixel” (or “pointer to Adafruit_NeoPixel” if you have to).

For example:

void colorWipe(Adafruit_NeoPixel &strip, uint32_t c, uint8_t wait, int waiting) {
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
        strip.show();
        delay(wait);
    }
    delay(waiting);
}

template <size_t N>
void colorWipeAll(Adafruit_NeoPixel (&neoStrips)[N], uint32_t c, uint8_t wait, int waiting) {
    for (auto &strip : neoStrips)
        colorWipe(strip, c, wait, waiting);
}

colorWipeAll(neoStrips, cWHITE, 100, 200);
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.