Adafruit neopixel mirror example

I've taken the AdaFruit Neopixel "standtest_wheel.ino" sketch and edited is slightly to only display a green "snake" going from one end of the LED strip to the other.

I'd like to have the same effect, but mirror it from the center and have two "snakes" originating from the center and head out towards each end of the strip in opposite directions.

I've done some reading on "MIRROR" posts, but nothing seems to be working quite right.

Here is the code I'm working with:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

//  NUM_LEDS and MIRROR are not yet used in the code below
#define NUM_LEDS 70
#define MIRROR 35
#define PIN 6

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

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code

  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  rainbowCycle(20);

}


// 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) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    // return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
    return strip.Color(0, 0, 0);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 0);
  }
  WheelPos -= 170;
  return strip.Color(0, 255 - WheelPos * 3, 0);
}

I appreciate any guidance you can provide!

may be try something like this

basically you fill up half the strip and then you just fill up the other half by copying the pixel color from the first half in reverse order to get the mirror effect

click to see the code
#include <Adafruit_NeoPixel.h>

#define NUM_LEDS 70
#define PIN 6

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

void setup() {
  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  animate(20);
}


// Slightly different, this makes the rainbow equally distributed throughout
void animate(uint8_t wait) {
  static uint16_t start = 0;
  strip.fill(0, 0,NUM_LEDS/2); // erase the first half
  // fill up that half  strip
  for (int32_t i = NUM_LEDS / 2-start; i >=0; i--) {
    strip.setPixelColor(i, 0xFF0000); // red
  }
  // now mirror
  for (int32_t i = NUM_LEDS - 1; i >= NUM_LEDS / 2; i--) {
    strip.setPixelColor(i, strip.getPixelColor(NUM_LEDS - 1 - i));
  }
  if (++start >= NUM_LEDS / 2) start = 0;
  strip.show();
  delay(wait);
}

Thank you so much for replying!
This version lights all of the lights at once and then starts to turn them off one by one in both directions (starting from the center of the strip).

I was looking for a way to start the lights from the center and light one light at a time in both directions until about 30 total lights were lit. Then start shutting them off one by one from the center and turning them one one by one snaking towards each end. The effect becomes two "snakes" of 15 lights each "moving" away from each other and both originating from the center.
-----------------------|-------------------------
<**********----------***********>

I will look to understand your code to see if it's something I can edit.
Thank you!

The purpose of my code was more to show how you can mirror. The animation was just as an example

➜ focus on calculating your snake on one half of the strip then just mirror the leds' status with a simple for loop

1 Like

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