How to make two Neopixel Rings flow in reverse RainbowCycle?

Hello,

I’m a long time listener, first time caller. Novice to coding and stumbling around in the dark with this project.

I have a little rainbow cycle going on one neopixle ring on Pin, and I would like to send the same pattern flowing in reverse to another ring on Pin2, but everything I try to add breaks the code.

Could you please set me straight? I’ve added a Define Direction but I can’t figure out how to insert it into the rainbow cycles or to duplicate the cycle for Pin2
Thank you!

#include <Adafruit_NeoPixel.h>


#define PIN 6
#define PIN2 7
#define direction Direction = forward
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(12, PIN2, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(255);  // Lower brightness and save eyeballs!
  strip.show(); // Initialize all pixels to 'off'
}
 
  
void loop() {
  // Some example procedures showing how to display to the pixels:
  rainbowCycle(1);

}
 

// 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);
  }
}
 
// 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);
  }
}

Just a quick guess. I can’t be sure of what you pattern does.

jschrosen:

// Slightly different, this makes the rainbow equally distributed throughout

void rainbowCycle(uint8_t wait) {
  uint16_t i, j, k;

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));
    for( k = strip2.numPixels( ); k; k-- )
      strip2.setPixelColor(k - 1, Wheel((((k - 1) * 256 / strip2.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

jschrosen:

Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(12, PIN2, NEO_GRB + NEO_KHZ800);

void setup() {
 strip.begin();
 strip.setBrightness(255);  // Lower brightness and save eyeballs!
 strip.show(); // Initialize all pixels to ‘off’
}

You have strip & strip2, but only initialize strip. Also, strip2.show(); is not used below in the rainbowCycle function.

jschrosen:

// 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

You don’t need the *5 since it’s running in a loop… unless you want it to cycle 5 times and stop.

jschrosen:

    for(i=0; i< strip.numPixels(); i++) {

strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

The delay() might cause some issues since nothing else in the code will run during the ‘wait’ length. That’s why the use of state machines and millis() is preferred. There’s info on that at the top of the boards in the sticky.

Maybe something like this (it compiles but may not give the intended result):

void rainbowCycle(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 * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    
    for(int h=11; h>0; h--)  // start at pixel 11 (assuming 0-11 numbering). if it's greater than 0, then subtract 1
    {
      strip2.setPixelColor(h, Wheel(((h * 256 / strip2.numPixels()) + j) & 255));
    }
    strip2.show();
    delay(wait);
   }
}