Better way of naming LED strips

Hi, I’m programming some LED strips for my Son’s upcoming birthday.
I’m hoping to lessen the bloat of my code by using the same object name but changing the suffix on the end using a for loop or similar. E.g. strip0, strip1. I’ve tried testing an array on strip1 but the adafruit neopixel doesn’t seem to accept it. I’ve tried different array types, int, char, string etc also.
I haven’t yet expanded out each of the functions to include the 4 LED strips I want to use as I’m hoping there is a cleaner and better expanding way to do it.
I’ve been searching for a solution for the last few hours but I’m probably just not using the right terminology in my searches.
Is there a way to get it to work or do I just have to put down each strip name each time I’m modifying it?

Code is below and I’ve commented out the bits that were causing errors.

#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 2
#define LED_PIN0 5
#define LED_PIN1 6
#define LED_PIN2 7
#define LED_PIN3 8
#define LED_COUNT 100
#define LED_SELECT 0
int strip[]={0,1,2,3};                            //this fails below

Adafruit_NeoPixel strip0(LED_COUNT, LED_PIN0);
//Adafruit_NeoPixel strip[1](LED_COUNT, LED_PIN1);         //this fails
Adafruit_NeoPixel strip2(LED_COUNT, LED_PIN2);
Adafruit_NeoPixel strip3(LED_COUNT, LED_PIN3);

boolean oldState = HIGH;
int mode = 8;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip0.begin();           // trying to initiate these without writing out each
  strip0.show();
  //strip[1].begin();     //testing with an array but it only errors above on compile
  //strip[1].show();
  strip2.begin();
  strip2.show();
  strip3.begin();
  strip3.show();
}

void loop() {
  boolean newState = digitalRead(BUTTON_PIN);

  if ((newState == LOW) && (oldState == HIGH)) {
    delay(20);
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      strip0.setBrightness(50);        // trying to initiate these without writing out each
      //strip[1].setBrightness(50);   //testing with an array but it only errors above on compile
      strip2.setBrightness(50);
      strip3.setBrightness(50);
      if (++mode > 9) mode = 0;
    }
  }
  switch (mode) {
    case 0:
      drip(random(0, 2000));
      break;
    case 1:
      raindrops(50);
      break;
    case 2:
      colorWipe(strip0.Color(255, 0, 0), 50);
      colorWipe(strip0.Color(0, 255, 0), 50);
      colorWipe(strip0.Color(0, 0, 255), 50);
      break;
    case 3:
      theaterChase(strip0.Color(127, 127, 127), 150);
      theaterChase(strip0.Color(127, 0, 0), 150);
      theaterChase(strip0.Color(0, 0, 127), 150);
      break;
    case 4:
      rainbow(10);
      break;
    case 5:
      theaterChaseRainbow(50);
      break;
    case 6:
      strip0.setBrightness(255);
      lightning(random(0, 2000));
      break;
    case 7:
      rainbowrange(10);
      break;
    case 8:
      breathebright(17);
      break;
    case 9:
      breathesat(17);
      break;
  }
  oldState = newState;
}

void breathebright(int wait) {
  int color = 30000;
  for (long fade = 100; fade < 255; fade += 1) {
    for (int i = 0; i < 100; i++) {
      strip0.setPixelColor(i, strip0.gamma32(strip0.ColorHSV(color, 150, fade)));
    }
    strip0.show();
    delay(wait);
  }
  for (long fade = 255; fade > 100; fade -= 1) {
    for (int i = 100; i >= 0; i--) {
      strip0.setPixelColor(i, strip0.gamma32(strip0.ColorHSV(color, 150, fade)));
    }
    strip0.show();
    delay(wait);
  }
}

void breathesat(int wait) {
  int color = 45000;
  for (long fade = 0; fade < 255; fade += 2) {
    for (int i = 0; i < 100; i++) {
      strip0.setPixelColor(i, strip0.gamma32(strip0.ColorHSV(color, fade, 150)));
    }
    strip0.show();
    delay(wait);
  }
  for (long fade = 255; fade > 0; fade -= 2) {
    for (int i = 100; i >= 0; i--) {
      strip0.setPixelColor(i, strip0.gamma32(strip0.ColorHSV(color, fade, 150)));
    }
    strip0.show();
    delay(wait);
  }
}

void lightning(int freq) {
  int wait = 0;
  uint32_t color = strip0.Color(127, 127, 127);
  for (int i = strip0.numPixels(); i >= 0; i--) {
    strip0.setPixelColor(i, color);
    strip0.show();
    delay(wait);
  }
  strip0.clear();
  strip0.show();
  delay(freq);
}

void raindrops(int wait) {
  int qty = 20;
  int array[qty];
  uint32_t color = strip0.Color(150, 150, 255);
  for (int i = 0; i < qty; i++) {
    int a = random(0, strip0.numPixels());
    strip0.setPixelColor(a, color);
    strip0.show();
    strip0.setPixelColor(array[i], (255, 0, 0));
    strip0.show();
    array[i] = a;
    delay(wait);
  }
}

void drip(int freq) {
  int b = 500;
  int wait = b;
  uint32_t color = strip0.Color(110, 110, 255);
  for (int i = strip0.numPixels(); i >= 0; i--) {
    int accel = pow(2, i);
    strip0.setPixelColor(i, color);
    strip0.show();
    strip0.clear();
    delay(wait);
    //wait = (max((i /2), 10)*1.5);
    wait = (max(wait - sqrt(b), 6)) / 1.8;
    b = wait;
  }
  strip0.clear();
  strip0.show();
  delay(freq);
}

void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip0.numPixels(); i++) {
    strip0.setPixelColor(i, color);
    strip0.show();
    delay(wait);
  }
}

void theaterChase(uint32_t color, int wait) {
  for (int a = 0; a < 10; a++) { 
    for (int b = 0; b < 3; b++) {
      strip0.clear();
      for (int c = b; c < strip0.numPixels(); c += 3) {
        strip0.setPixelColor(c, color);
      }
      strip0.show();
      delay(wait);
    }
  }
}

void rainbow(int wait) {
  for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
    for (int i = 0; i < strip0.numPixels(); i++) {
      int pixelHue = firstPixelHue + (i * 65536L / strip0.numPixels());
      strip0.setPixelColor(i, strip0.gamma32(strip0.ColorHSV(pixelHue)));
    }
    strip0.show();
    delay(wait);
  }
}

void rainbowrange(int wait) {
  for (long firstPixelHue = 15000; firstPixelHue < 25000; firstPixelHue += 156) {
    for (int i = 0; i < strip0.numPixels(); i++) {
      int pixelHue = firstPixelHue + (i * 25000L / strip0.numPixels());
      strip0.setPixelColor(i, strip0.gamma32(strip0.ColorHSV(pixelHue)));
    }
    strip0.show();
    delay(wait);
  }
  for (long firstPixelHue = 25000; firstPixelHue > 15000; firstPixelHue -= 156) {
    for (int i = strip0.numPixels(); i >= 0; i--) {
      int pixelHue = firstPixelHue + (i * 25000L / strip0.numPixels());
      strip0.setPixelColor(i, strip0.gamma32(strip0.ColorHSV(pixelHue)));
    }
    strip0.show();
    delay(wait);
  }
}

void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;
  for (int a = 0; a < 30; a++) {
    for (int b = 0; b < 3; b++) {
      strip0.clear();
      for (int c = b; c < strip0.numPixels(); c += 3) {
        int hue = firstPixelHue + c * 65536L / strip0.numPixels();
        uint32_t color = strip0.gamma32(strip0.ColorHSV(hue));
        strip0.setPixelColor(c, color);
      }
      strip0.show();
      delay(wait);
      firstPixelHue += 65536 / 90;
    }
  }
}

Hello,

Try

Adafruit_NeoPixel strips[] =
{
  Adafruit_NeoPixel(LED_COUNT, LED_PIN0),
  Adafruit_NeoPixel(LED_COUNT, LED_PIN1),
  ...
};

size_t numStrips = sizeof(strips) / sizeof(Adafruit_NeoPixel);

...

for ( uint8_t i = 0; i < numStrips; i++ )
{
  strips[i].begin();
  strips[i].show();
  ...
}

Thank you for your quick reply! This worked perfectly!
Only issue now is that I’ve used delays so each strip is taking it in turns to do the animation (which could be cool for a new animation), so I will need to do some more tweaking to implement blink without delay so the other strips don’t have to wait in line.

I’ve updated the breathebright function below to work with all strips, now to do it for the rest and change out the delays.

Thanks again.

#include <Adafruit_NeoPixel.h>
#define BUTTON_PIN 2
#define LED_PIN0 5
#define LED_PIN1 6
#define LED_PIN2 7
#define LED_PIN3 8
#define LED_COUNT 100
#define LED_SELECT 0

Adafruit_NeoPixel strip[] = {
  Adafruit_NeoPixel(LED_COUNT, LED_PIN0),
  Adafruit_NeoPixel(LED_COUNT, LED_PIN1),
  Adafruit_NeoPixel(LED_COUNT, LED_PIN2),
  Adafruit_NeoPixel(LED_COUNT, LED_PIN3),
};

size_t numStrips = sizeof(strip) / sizeof(Adafruit_NeoPixel);

boolean oldState = HIGH;
int mode = 8;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  for (uint8_t i = 0; i < numStrips; i++) {
    strip[i].begin();
    strip[i].show();
  }
}

void loop() {
  boolean newState = digitalRead(BUTTON_PIN);

  if ((newState == LOW) && (oldState == HIGH)) {
    delay(20);
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      for (uint8_t i = 0; i < numStrips; i++) {
        strip[i].setBrightness(50);
      }
      if (++mode > 9) mode = 0;
    }
  }
  switch (mode) {
    case 0:
      drip(random(0, 2000));
      break;
    case 1:
      raindrops(50);
      break;
    case 2:
      colorWipe(strip[0].Color(255, 0, 0), 50);
      colorWipe(strip[0].Color(0, 255, 0), 50);
      colorWipe(strip[0].Color(0, 0, 255), 50);
      break;
    case 3:
      theaterChase(strip[0].Color(127, 127, 127), 150);
      theaterChase(strip[0].Color(127, 0, 0), 150);
      theaterChase(strip[0].Color(0, 0, 127), 150);
      break;
    case 4:
      rainbow(10);
      break;
    case 5:
      theaterChaseRainbow(50);
      break;
    case 6:
      strip[0].setBrightness(255);
      lightning(random(0, 2000));
      break;
    case 7:
      rainbowrange(10);
      break;
    case 8:
      for (uint8_t a = 0; a < numStrips; a++) {
        breathebright(17, a);
      }
      break;
    case 9:
      breathesat(17);
      break;
  }
  oldState = newState;
}

void breathebright(int wait, int stripno) {
  int color = 30000;
  for (long fade = 100; fade < 255; fade += 1) {
    for (int i = 0; i < 100; i++) {
      strip[stripno].setPixelColor(i, strip[stripno].gamma32(strip[stripno].ColorHSV(color, 150, fade)));
    }
    strip[stripno].show();
    delay(wait);
  }
  for (long fade = 255; fade > 100; fade -= 1) {
    for (int i = 100; i >= 0; i--) {
      strip[stripno].setPixelColor(i, strip[stripno].gamma32(strip[stripno].ColorHSV(color, 150, fade)));
    }
    strip[stripno].show();
    delay(wait);
  }
}

void breathesat(int wait) {
  int color = 45000;
  for (long fade = 0; fade < 255; fade += 2) {
    for (int i = 0; i < 100; i++) {
      strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(color, fade, 150)));
    }
    strip[0].show();
    delay(wait);
  }
  for (long fade = 255; fade > 0; fade -= 2) {
    for (int i = 100; i >= 0; i--) {
      strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(color, fade, 150)));
    }
    strip[0].show();
    delay(wait);
  }
}

void lightning(int freq) {
  int wait = 0;
  uint32_t color = strip[0].Color(127, 127, 127);
  for (int i = strip[0].numPixels(); i >= 0; i--) {  
    strip[0].setPixelColor(i, color);    
    strip[0].show();
    delay(wait);
  }
  strip[0].clear();
  strip[0].show();
  delay(freq);
}

void raindrops(int wait) {
  int qty = 20;
  int array[qty];
  uint32_t color = strip[0].Color(150, 150, 255);
  for (int i = 0; i < qty; i++) {
    int a = random(0, strip[0].numPixels());
    strip[0].setPixelColor(a, color);
    strip[0].show();
    strip[0].setPixelColor(array[i], (255, 0, 0));
    strip[0].show();
    array[i] = a;
    delay(wait);
  }
}

void drip(int freq) {
  int b = 500;
  int wait = b;
  uint32_t color = strip[0].Color(110, 110, 255);
  for (int i = strip[0].numPixels(); i >= 0; i--) { 
    int accel = pow(2, i);
    strip[0].setPixelColor(i, color);
    strip[0].show();  
    strip[0].clear();
    delay(wait);
    //wait = (max((i /2), 10)*1.5);
    wait = (max(wait - sqrt(b), 6)) / 1.8;
    b = wait;
  }
  strip[0].clear();
  strip[0].show();
  delay(freq);
}

void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip[0].numPixels(); i++) { 
    strip[0].setPixelColor(i, color);   
    strip[0].show();
    delay(wait);
  }
}

void theaterChase(uint32_t color, int wait) {
  for (int a = 0; a < 10; a++) {   // Repeat 10 times...
    for (int b = 0; b < 3; b++) {  //  'b' counts from 0 to 2...
      strip[0].clear();            //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for (int c = b; c < strip[0].numPixels(); c += 3) {
        strip[0].setPixelColor(c, color); 
      }
      strip[0].show();
      delay(wait);
    }
  }
}

void rainbow(int wait) {
  for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
    for (int i = 0; i < strip[0].numPixels(); i++) {
      int pixelHue = firstPixelHue + (i * 65536L / strip[0].numPixels());
      strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(pixelHue)));
    }
    strip[0].show();
    delay(wait);
  }
}

void rainbowrange(int wait) {
  for (long firstPixelHue = 15000; firstPixelHue < 25000; firstPixelHue += 156) {
    for (int i = 0; i < strip[0].numPixels(); i++) {
      int pixelHue = firstPixelHue + (i * 25000L / strip[0].numPixels());
      strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(pixelHue)));
    }
    strip[0].show();
    delay(wait);
  }
  for (long firstPixelHue = 25000; firstPixelHue > 15000; firstPixelHue -= 156) {
    for (int i = strip[0].numPixels(); i >= 0; i--) {
      int pixelHue = firstPixelHue + (i * 25000L / strip[0].numPixels());
      strip[0].setPixelColor(i, strip[0].gamma32(strip[0].ColorHSV(pixelHue)));
    }
    strip[0].show();
    delay(wait);
  }
}

void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;
  for (int a = 0; a < 30; a++) {
    for (int b = 0; b < 3; b++) {
      strip[0].clear();
      for (int c = b; c < strip[0].numPixels(); c += 3) {
        int hue = firstPixelHue + c * 65536L / strip[0].numPixels();
        uint32_t color = strip[0].gamma32(strip[0].ColorHSV(hue)); 
        strip[0].setPixelColor(c, color); 
      }
      strip[0].show();
      delay(wait);
      firstPixelHue += 65536 / 90;
    }
  }
}

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