Search code examples
c++arraysarduinoadafruit

Adafruit_NeoPixel objects abnormal behavior when used in an array


I've been having problems trying to work out how to loop through an array of Adafruit_NeoPixel objects. But I cant for the life of me get my head aroud what is going wrong. Ive looked up this issue on google and trough both Ardrino and stack over flow ive tryed adapting the code to what ive seen other people have done (for example instead of listing the adafruit_neopixles objects in the array,create the neopixles inside the array) to get it to work and still I have no luck.

so heres an simple example: this script should make the first 6 Leds light up in green blue

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strips[] = {
  Adafruit_NeoPixel(32, 5, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(32, 6, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(32, 7, NEO_GRB + NEO_KHZ800),
};

#define NUMSTRIPS (sizeof(strips)/sizeof(strips[0]))

void setup() {
     //Edit2
         Serial.begin(115200);
     //end Edit2
  
  for(int i=0; i<NUMSTRIPS; i++)
  {
    strips[i].begin();
    strips[i].setBrightness(255); //adjust brightness here
    
    /*This is code that ive added in AFTER i made the orgional post to see if it had any difference   
    */
        for (int j=0; j<10;j++){
          strips[0].setPixelColor(j, 0,100,0);
      }
    
     /* End of edited code
     */
    strips[i].show(); // Initialize all pixels to 'off'
  }
  //Edit2
    Serial.println("Loop end");
  //End Edit2

  //strips[0].begin();
  strips[0].setBrightness(255);
  strips[0].setPixelColor(0, 0,100,255);
  strips[0].setPixelColor(1, 0,100,255);
  strips[0].setPixelColor(2, 0,100,255);
  strips[0].setPixelColor(3, 0,100,255);
  strips[0].setPixelColor(4, 0,100,255);
  strips[0].setPixelColor(5, 0,100,255);
  strips[0].show();
}

However that does nothing no led's light up at all. -WHY!? Yet when I comment out the For loop and un-comment the strips[0].begin it does work. So why is this? what am I not able to understand?

EDIT: So I tried changing a few things in my code to test if it had any affect i added in

    for (int j=0; j<10;j++){
          strips[0].setPixelColor(j, 0,100,0);
      }

it worked inside the loop but now anything after the loop no longer works.

EDIT 2: so after using serial i found out that the device is crashing when the loop ends. So why would that be?.

Edit 3: First I would like to say sorry to ocrdu they tried to edit this but still not quite had the right idea.

Second I want to say that half the problem has been worked out the abnormal behavior was caused by the micro controller crashing when ever the strips were being illiterate through. As for the why that is still what I need help to understand.

So I made it through 1 loop now before it crashes so progress? I took Botje's advice and I used strips[i].numPixels(); instead of using a fixed value

however I decided to do a few more tests to see if I had fixed the problem and well it crashed before test 2 even got to start. Here is the new code.

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strips[] = {
  Adafruit_NeoPixel(32, 5, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(32, 6, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(32, 7, NEO_GRB + NEO_KHZ800),
};

#define NUMSTRIPS (sizeof(strips)/sizeof(strips[0]))

void setup() {
  Serial.begin(115200);
  
  Serial.println("1st Test Do a loop with out crashing over all the strips");
  for(int i=0; i<NUMSTRIPS; i++)
  {
    strips[i].begin();
    strips[i].setBrightness(255);
    for (int j=0; j<strips[i].numPixels();j++){
          strips[i].setPixelColor(j, 0,100,0);
          Serial.println(j);
      }
    Serial.println("b4 show");
    strips[i].show(); 
  }
  Serial.println("Loop end");
  Serial.println("If you are reading this then the microcontroller did not crash on 1st Test");



  delay(150);



  Serial.println("2nd Test Do a loop with out crashing over 1 strip");
  for (int k=0; k<strips[0].numPixels();k++)
  {
    strips[0].setPixelColor(k, 0,100,255);
    strips[0].show();
    Serial.println(k);
  }
  Serial.println("Loop end");
  Serial.println("If you are reading this then the microcontroller did not crash the 2nd Test");



  delay(150);



  Serial.println("3rd Test Do a loop changing onley 6 pixles on 1 strip with out crashing");
  for (int l=0; l<=5; l++)
  {
    strips[0].setPixelColor(l, 200,0,0);
    strips[0].show();
    Serial.println(l);
  }
  Serial.println("Loop end");
  Serial.println("If you are reading this then the microcontroller did not crash the 3rd Test");
}


Solution

  • So I've fixed this issue by swapping out the adafruit liabery with fastled no more crashing or abnormal behaviour.