Creating Functions with Adafruit_Neopixel Object as an Argument

I want to create a void function that takes an object as an argument other than an int, double, float, etc. Specifically, I am trying to take a previously defined Adafruit_Neopixel object. When I run this test code, it executes the function once, then does not loop it. It's as if the function redefines or renames the Adafruit_Neopixel object, such that it cannot execute again during void loop().

#include <Adafruit_NeoPixel.h>
#define MAX_BRIGHTNESS 250 // (max = 255)
#define LED_COUNT 36

Adafruit_NeoPixel lowstrip(LED_COUNT, 6, NEO_GRB + NEO_KHZ800);

void setup() {

  lowstrip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  lowstrip.show();            // Turn OFF all pixels ASAP
  lowstrip.setBrightness(MAX_BRIGHTNESS);
  
}

void loop() {

  stripper(lowstrip);
  lowstrip.clear();

}

void stripper (Adafruit_NeoPixel strip) {
   for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, strip.Color(20, 30, 40));
    strip.show();
    delay(40);
  }
}

Welcome to the forum.

You want to operate on the object "lowstrip". That means the code in the function stripper has to reach beyond the stack to the "lowstrip" object.
You can do that with a pointer or "by reference".

To change the parameter 'strip' into "by reference", add a '&'

void stripper (Adafruit_NeoPixel & strip) {

After that the 'strip' is a reference to the object "lowstrip" and all the code operates on the object "lowstrip".

Thanks for the quick reply - worked like a charm!

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