NOOB ALERT: Leveraging function argument for class instantiation?

Hi,

First, been a long time since I've played with code so please forgive ignorance.. also brand spanking new to Arduino.

I'm in the process of soldering up a bunch of NeoPixel sticks and testing in my breadboard with the standard NeoPixel strandtest. I've figured out how to include multiple NeoPixel objects in an array and cycle the array for the setup and loop.

The problem I'm having is that I don't want to go through each function in the standard strandtest example to replace strip.Color with strip[0]. color everytime strip is referenced.

I figured I create a function that was a collection of the strandtest functions - see function: theCycle() and I just figured I could pass the object to that function and have it defined as strip.

So.. strip[ #] gets handed off to theCycle() as an agrument and relabelled to "strip".

When I verify the code, first error that gets thrown is 'cannot convert Adafruit_NeoPixel' to 'int' for argument '1' to voide theCycle(int)...

So.. I get that the object ain't an integer.. but I just don't know how to pass the object into the function.

Here is the code I'm working with:

#include <Adafruit_NeoPixel.h>

//create object array
Adafruit_NeoPixel strip[2] = {
  Adafruit_NeoPixel(8, 12, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(8, 11, NEO_GRB + NEO_KHZ800)};

int STICKS = 2; //global count of NeoPixel sticks in object array

void setup() {
  for(int theSticks = 0; theSticks < STICKS-1; theSticks++){ //setup all the NeoPixel sticks in array
    strip[theSticks].begin();
    strip[theSticks].show(); // Initialize all pixels to 'off'
  };
}

void loop() {
  for(int theSticks = 0; theSticks < STICKS-1; theSticks++){ //have every NeoPixel stick run the strandtest functions
    theCycle(strip[theSticks]);
  };
}

void theCycle(int strip){ //this function is the collection of NeoPixel functions to run against the strip
  colorWipe(strip.Color(255, 255, 255), 50); // White
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue
 :)
  theaterChase(strip.Color(127, 127, 127), 50); // White
  theaterChase(strip.Color(127,   0,   0), 50); // Red
  theaterChase(strip.Color(  0,   0, 127), 50); // Blue

  rainbow(20);
  rainbowCycle(20);
  theaterChaseRainbow(50);
}

If it is any help, here is the code that came with the generic strandtest example:

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();
     
      delay(wait);
     
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
        }
        strip.show();
       
        delay(wait);
       
        for (int i=0; i < strip.numPixels(); i=i+3) {
          strip.setPixelColor(i+q, 0);        //turn every third pixel off
        }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Thanks for any help!
-eko

Your global variable 'strip' is hidden by the function parameter 'strip', the closest scope wins...

Adafruit_NeoPixel strip[2]

//...

void theCycle(int strip){

If you want to keep the variable name the same, you must access the global version explicitly:

colorWipe(::strip.Color(255, 255, 255), 50); // White

Hey, I don't think that is the problem. I changed the variable name to prevent a collision to:

void theCycle(int stripArr)

and I get the same warning: cannot convert Adafruit_NeoPixel to 'int' for argument '1' to 'void theCycle(int)'

that is because Adafruit_NeoPixel isnt an integer, its an array

you need to use a for loop to get the values from the array into that function

EDIT: Im not sure about the last statement but i am sure about the first.

where did you get "strip" from?

Adafruit_NeoPixel(8, 12, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(8, 11, NEO_GRB + NEO_KHZ800)};

looking at this, i think you need to set the strips up like this:

stripdataPin[] = {11,12};
Adafruit_NeoPixel(8, stripdataPin[n], NEO_GRB + NEO_KHZ800);

but I'm not an expert nor did i stay at a holiday inn last night

Eh.. fixed it.. kind of. Now I'm just trying to figure out how to make the set of functions fire on the 2 sticks at the same time instead of running serially...

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip[2] = {
  Adafruit_NeoPixel(8, 12, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(8, 11, NEO_GRB + NEO_KHZ800)};


int STICKS = 2;

void setup() {
  for(int i = 0; i < STICKS; i++){
    strip[i].begin();
    strip[i].show(); // Initialize all pixels to 'off'
  };
}

void loop() {
  theCycle(0);
  theCycle(1);
}

void theCycle(int i){
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip[i].Color(255, 255, 255), 50, i); // White
  colorWipe(strip[i].Color(255, 0, 0), 50, i); // Red
  colorWipe(strip[i].Color(0, 255, 0), 50, i); // Green
  colorWipe(strip[i].Color(0, 0, 255), 50, i); // Blue
  // Send a theater pixel chase in...
  theaterChase(strip[i].Color(127, 127, 127), 50, i); // White
  theaterChase(strip[i].Color(127,   0,   0), 50, i); // Red
  theaterChase(strip[i].Color(  0,   0, 127), 50, i); // Blue

  rainbow(20, i);
  rainbowCycle(20, i);
  theaterChaseRainbow(50, i);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait, int n) {
  for(uint16_t i=0; i<strip[n].numPixels(); i++) {
    strip[n].setPixelColor(i, c);
    strip[n].show();
    delay(wait);
  }
}

void rainbow(uint8_t wait, int i2) {
  uint16_t i, j;
  for(j=0; j<256; j++) {
    for(i=0; i<strip[i2].numPixels(); i++) {
      strip[i2].setPixelColor(i, Wheel((i+j) & 255, i2));
    }
    strip[i2].show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait, int i2) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip[i2].numPixels(); i++) {
      strip[i2].setPixelColor(i, Wheel(((i * 256 / strip[i2].numPixels()) + j) & 255, i2));
    }
    strip[i2].show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait, int i2) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip[i2].numPixels(); i=i+3) {
        strip[i2].setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip[i2].show();

      delay(wait);

      for (int i=0; i < strip[i2].numPixels(); i=i+3) {
        strip[i2].setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait, int i2) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip[i2].numPixels(); i=i+3) {
        strip[i2].setPixelColor(i+q, Wheel( (i+j) % 255, i2));    //turn every third pixel on
      }
      strip[i2].show();

      delay(wait);

      for (int i=0; i < strip[i2].numPixels(); i=i+3) {
        strip[i2].setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos, int i2) {
  if(WheelPos < 85) {
    return strip[i2].Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } 
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return strip[i2].Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } 
  else {
    WheelPos -= 170;
    return strip[i2].Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

the led strips are addressable, why not just connect them together...

Qdeathstar:
the led strips are addressable, why not just connect them together...

I'm not sure if I'll be able to physically do that for my final project, so I'm trying it this way.