controlling half strips in reverse

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
kirstenw
 
Posts: 21
Joined: Sat Aug 17, 2013 3:57 am

controlling half strips in reverse

Post by kirstenw »

Ok I've hit a brick wall : I've been attempting to mod the BANNED knight rider code from git-hub https://github.com/technobly/NeoPixel-KnightRider
working on flora powered by lipo but I'm stuck (guess my c is really not up to the programming part of this project.. c is weird people ..weird i tell you but hey I'm here to learn )
anyhoooo
I've manged to hack in so it does half the pixels (60) but ..I need to flip it and reverse it so the two sides meet. is this possible..mm it must be cause I'm using;
http://www.slickstreamer.info/2014/06/d ... meter.html as a semi proof..
ok so is there a neat way of making the animation start from the end of the strip rather than the beginning? or a slick /tricky programatic way of making it iterate through the two halves.with minimum code... better brains than mine !

Code: Select all

#include <Adafruit_NeoPixel.h>
#define PIN 9    // 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)
#define N_PIXELS  60
//declare the amount of pixels 
#define N_PIXELS_HALF (N_PIXELS/2)

  Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 9, NEO_GRB + NEO_KHZ800);



void setup() {
  strip.begin();
  clearStrip(); // Initialize all pixels to 'off'
  delay(1000);
}

void loop() {
  // Iterate through a whole rainbow of colors
  for(byte j=0; j<252; j+=7) {
    knightRider(1, 16, 2, colorWheel(j)); // Cycles, Speed, Width, RGB Color
  }
  clearStrip();
  delay(2000);
}

// Cycles - one cycle is scanning through all pixels left then right (or right then left)
// Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
// Width - how wide the trail effect is on the fading out LEDs.  The original display used
//         light bulbs, so they have a persistance when turning off.  This creates a trail.
//         Effective range is 2 - 8, 4 is default for 16 pixels.  Play with this.
// Color - 32-bit packed RGB color value.  All pixels will be this color.
// knightRider(cycles, speed, width, colour);

void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
  uint32_t old_val[N_PIXELS_HALF]; // up to 256 lights!

  // Larson time baby!
  for(int i = 0; i < cycles; i++){
    for (int count = 1; count<N_PIXELS_HALF; count++) {
      strip.setPixelColor(count, color); strip.show();
      delay(speed);
      old_val[count] = color;
      for(int x = count; x>0; x--) {
        old_val[x-1] = dimColor(old_val[x-1], width);
        strip.setPixelColor(x-1, old_val[x-1]); strip.show();
      }
    }
    for (int count = N_PIXELS_HALF-1; count>=0; count--) {
      strip.setPixelColor(count, color); strip.show();
      delay(speed);
      old_val[count] = color;
      for(int x = count; x<=N_PIXELS_HALF ;x++) {
        old_val[x-1] = dimColor(old_val[x-1], width);
        strip.setPixelColor(x+1, old_val[x+1]); strip.show();
      }
    }
  }
}

void clearStrip() {
  for( int i = 0; i<N_PIXELS_HALF; i++){
    strip.setPixelColor(i, 0x000000); strip.show();
  }
}

uint32_t dimColor(uint32_t color, uint8_t width) {
   return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
}

// Using a counter and for() loop, input a value 0 to 251 to get a color value.
// The colors transition like: red - org - ylw - grn - cyn - blue - vio - mag - back to red.
// Entering 255 will give you white, if you need it.
uint32_t colorWheel(byte WheelPos) {
  byte state = WheelPos / 21;
  switch(state) {
    case 0: return strip.Color(255, 0, 255 - ((((WheelPos % 21) + 1) * 6) + 127)); break;
    case 1: return strip.Color(255, ((WheelPos % 21) + 1) * 6, 0); break;
    case 2: return strip.Color(255, (((WheelPos % 21) + 1) * 6) + 127, 0); break;
    case 3: return strip.Color(255 - (((WheelPos % 21) + 1) * 6), 255, 0); break;
    case 4: return strip.Color(255 - (((WheelPos % 21) + 1) * 6) + 127, 255, 0); break;
    case 5: return strip.Color(0, 255, ((WheelPos % 21) + 1) * 6); break;
    case 6: return strip.Color(0, 255, (((WheelPos % 21) + 1) * 6) + 127); break;
    case 7: return strip.Color(0, 255 - (((WheelPos % 21) + 1) * 6), 255); break;
    case 8: return strip.Color(0, 255 - ((((WheelPos % 21) + 1) * 6) + 127), 255); break;
    case 9: return strip.Color(((WheelPos % 21) + 1) * 6, 0, 255); break;
    case 10: return strip.Color((((WheelPos % 21) + 1) * 6) + 127, 0, 255); break;
    case 11: return strip.Color(255, 0, 255 - (((WheelPos % 21) + 1) * 6)); break;
    default: return strip.Color(0, 0, 0); break;
  }
}
project working vid : https://www.facebook.com/video.php?v=10 ... 7364402230

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: controlling half strips in reverse

Post by adafruit_support_bill »

c is weird people ..weird i tell you
Don't look now, but you are actually programming in C++. Scientifically proven to be 4-times as weird as C.
ok so is there a neat way of making the animation start from the end of the strip rather than the beginning? or a slick /tricky programatic way of making it iterate through the two halves.with minimum code...
Every time you write a pixel, write it's mirrored pixel. The mirrored pixel index can be calculated as the total number of pixels minus the index of the pixel.

Code: Select all

    for (int count = N_PIXELS_HALF-1; count>=0; count--) 
     {
      strip.setPixelColor(count, color);    // set a pixel from one end
      strip.setPixelColor(N_Pixels - count, color);  // mirror it from the other end
       strip.show();
       ...
       ...
      }

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”