How to repllicate a barber pole type effect on string on WS8211 pixels OctoWS2811

Status
Not open for further replies.

JulianCollison

New member
Hi all
I have five trees in my garden, all of which have a string of 150 LED pixels on them. They are driven by a Teensy 3.5 running OctoWS8211 library
I have a number of effects running on them (wind up tree, wind down tree, random fill, random empty, fade colours across trees etc - full code attached);

I am now trying to code a barber pole effect. Along the string I would like a number (say 10) of LEDs to be white, then the next 10 red, then the next 10 white, then red etc through to the end
Then as a loop runs I would like the blocks to move along the strip ie
start RRRRRRRRRRWWWWWWWWWWRRRRRRRRRRRWWWWWWWWWW
then WRRRRRRRRRRWWWWWWWWWWRRRRRRRRRRWWWWWWWWWW
then WWRRRRRRRRRRWWWWWWWWWWRRRRRRRRRRWWWWWWWWW
etc

Please could somebody advise on the best way to code this ?
Many thanks
Jules
Code:
#include <OctoWS2811.h>

const int pixelsperchannel = 150;
const int numberofchannels = 5;

//set up OctoWS2811 instance
DMAMEM int displaymemory[pixelsperchannel*6];
int drawingmemory[pixelsperchannel*6];
const int config = WS2811_RGB | WS2811_800kHz;

OctoWS2811 leds(pixelsperchannel, displaymemory, drawingmemory, config);

//Sequence global variables
int rainbowcolours[180];            // array that holds the RGB values for all of the HSV colour wheel
long oldmillis;
int firstcolour, shift;             // starting colour on the HSV wheel and phase shift variable
int randomorder[pixelsperchannel];  // array holding a random series of numbers with no repeats

void setup() {
  int hue, saturation, lightness, x;
  
  // pre-compute the 180 rainbow colors and populate the array with the RGB colours
    for (x = 0; x < 180; x++) {
    hue = x * 2;
    saturation = 100;
    lightness = 40;
    rainbowcolours[x] = makeColor(hue, saturation, lightness);
  }
  
  //fill random order array sequentially
  for (x=0; x<pixelsperchannel; x++) {
    randomorder[x]=x;
  }
  //then mix them up randomly
  randomisearray();
  
  leds.begin();
}


void loop() {
  //light up from bottom in single colour per tree. First tree is random but all are sequential from that with equal shift across spectrum
  firstcolour = random(180);
  shift = 360 / numberofchannels;
  lightfrombottom(firstcolour,shift);
  
  //random sparkle for a minute while stationary colour
  oldmillis=millis();
  Serial.println("Sparkling");
  while (millis()-oldmillis <60000) {
      sparkle(numberofchannels * pixelsperchannel);
      delayMicroseconds(100000);
      Serial.print(".");
  }
  Serial.println("Finished static sparkle");
  
  //colour fade between trees for a minute making sure we start from the current tree colours
  oldmillis=millis();
  while (millis()-oldmillis <60000) {
    colourfade(firstcolour, shift, 10000);
  }
  
  //unbuild downwards with current colours
  clearfromtop();

  //speckle fill each tree with random colours from a limited area of sprectrum
  randomfill(150, 180);                   // pinks and purples
  //random sparkle for a minute while stationary colour
  oldmillis=millis();
  Serial.println("Sparkling");
  while (millis()-oldmillis <20000) {
      sparkle(numberofchannels * pixelsperchannel);
      delayMicroseconds(100000);
      Serial.print(".");
  }
  Serial.println("Finished static sparkle");
  
  randomfill(0, 20);                      // reds and oranges
  //random sparkle for a minute while stationary colour
  oldmillis=millis();
  Serial.println("Sparkling");
  while (millis()-oldmillis <20000) {
      sparkle(numberofchannels * pixelsperchannel);
      delayMicroseconds(100000);
      Serial.print(".");
  }
  Serial.println("Finished static sparkle");
  
  randomfill(80, 110);                    // blues
  //random sparkle for a minute while stationary colour
  oldmillis=millis();
  Serial.println("Sparkling");
  while (millis()-oldmillis < 20000) {
      sparkle(numberofchannels * pixelsperchannel);
      delayMicroseconds(100000);
      Serial.print(".");
  }
  Serial.println("Finished static sparkle");
  
  //speckle unbuild (ie randomly fill with black)
  randomempty();

  //fill with coloured dots stacking up from the bottom
  firstcolour=random(180);
  filldotsfromtop(firstcolour, shift);

  //random sparkle for a minute while stationary colour
  oldmillis=millis();
  Serial.println("Sparkling");
  while (millis()-oldmillis <60000) {
      sparkle(numberofchannels * pixelsperchannel);
      delayMicroseconds(100000);
      Serial.print(".");
  }
  Serial.println("Finished static sparkle");

  //fill with coloured dots stacking up from the bottom
  firstcolour=random(180);
  filldotsfromtop(firstcolour, shift);

  //random sparkle for a minute while stationary colour
  oldmillis=millis();
  Serial.println("Sparkling");
  while (millis()-oldmillis <60000) {
      sparkle(numberofchannels * pixelsperchannel);
      delayMicroseconds(100000);
  }
  Serial.println("Finished static sparkle");

  //fill with coloured dots stacking up from the bottom
  firstcolour=random(180);
  filldotsfromtop(firstcolour, shift);

  //random sparkle for a minute while stationary colour
  oldmillis=millis();
  Serial.println("Sparkling");
  while (millis()-oldmillis <60000) {
      sparkle(numberofchannels * pixelsperchannel);
      delayMicroseconds(100000);
  }
  Serial.println("Finished static sparkle");
 
  clearfromtop();
  
}

//---------- All sequence functions are below here ----------------------//

void lightfrombottom(int firstcol, int shift)
{
  int x,y,index;
  Serial.println("Building up");
  for (x = 0; x < pixelsperchannel; x++) {
    for (y = 0; y < numberofchannels; y++) {
      index = (firstcol + y * shift / 2) % 180;   
      leds.setPixel(x + y * pixelsperchannel, rainbowcolours[index]);
    }
    leds.show();
    delayMicroseconds(50000);
    Serial.print(".");
  }
   Serial.println("Finished building");
}

void clearfromtop() {
  int x,y;
  Serial.println("Clearing down");
  for (x = pixelsperchannel; x > 0; x--){
    for (y = 0; y < numberofchannels; y++) {
      leds.setPixel(x + y * pixelsperchannel, 0, 0, 0);  
    }
    leds.show();
    delayMicroseconds(50000);
  }
  Serial.println("All cleared");
}

void sparkle(int totalleds)
{
  int currentled;
  int currentcolour;
  currentled=random(totalleds);
  currentcolour=leds.getPixel(currentled);
  leds.setPixel(currentled,255,255,255);
  leds.show();
  leds.setPixel(currentled,currentcolour);
}

void colourfade (int firstcol, int shift, int cycletime)
{
  int x, y, index, wait, colour;
  
  Serial.println("Colour fading between whole trees");
  wait = cycletime * 1000 / pixelsperchannel;
  for (colour=0; colour<180; colour++) {
    for (y=0; y < 8; y++) {
      index = (colour + firstcol + y*shift/2) % 180;
      for (x=0; x<pixelsperchannel; x++) {
        leds.setPixel(x + y * pixelsperchannel, rainbowcolours[index]);
      }
    }
    sparkle(numberofchannels * pixelsperchannel);
    //leds.show();
    delayMicroseconds(wait);
  }
  Serial.println("Finished fading");
}

void alloff()
{
  for (int x=0; x<pixelsperchannel*numberofchannels;x++) {
    leds.setPixel(x,0);
  }
  leds.show();
}

void rainbow(int phaseshift, int cycletime)
{
  int colour, x, y, wait;

  wait = cycletime * 1000 / pixelsperchannel;
  for (colour=0; colour < 180; colour++) {
    for (x=0; x < pixelsperchannel; x++) {
      for (y=0; y < 8; y++) {
        int index = (colour + x + y*phaseshift/2) % 180;
        leds.setPixel(x + y*pixelsperchannel, rainbowcolours[index]);
      }
    }
    leds.show();
    delayMicroseconds(wait);
  }
}

void randomfill(int startpos, int endpos)
{
  int x, y, currentled;
  
  Serial.println("Filling with random colours in speckled form");
  //pick a random colour for all trees
  randomisearray();
  
  for (x = 0; x < pixelsperchannel; x++) {
    for (y = 0; y < numberofchannels; y++) {
      currentled = randomorder[x] + y * pixelsperchannel;
      leds.setPixel(currentled, rainbowcolours[random(startpos, endpos)]);   
    }
    leds.show();
    delayMicroseconds(50000);
  }
  Serial.println("Finished filling");
}

void randomempty()
{
  int x, y, currentled;
  
  Serial.println("Now emptying trees"); 
  for (x = 0; x < pixelsperchannel; x++) {
    for (y = 0; y < numberofchannels; y++) {
      currentled = randomorder[x] + y * pixelsperchannel;
      leds.setPixel(currentled, 0, 0, 0);   
    }
    leds.show();
    delayMicroseconds(50000);
  }
  Serial.println("Finished emptying");
}

void filldotsfromtop(int firstcol, int shift) 
{
  int x,y, z, index;
  
  Serial.println("Filling from the top");
  z = 0;
  while (z < pixelsperchannel)
  {
    for (x = pixelsperchannel; x>=z; x--) {
      for (y = 0; y < numberofchannels; y++) {
        index = (firstcol + y * shift / 2) % 180;
        leds.setPixel(x + y*pixelsperchannel, rainbowcolours[index]);
        leds.setPixel(x + 1 +y*pixelsperchannel, 0, 0, 0);
      }
      leds.show();
      delayMicroseconds(10000);
    }
    z++;
  }
  Serial.println("Finished filling");
}

void randomisearray ()
{
  int position1, position2, temp1, temp2;
  //randomly jumble up the random order array
  randomSeed(analogRead(0));
  for (position1 = 0; position1 < pixelsperchannel; position1++) {
    position2 = random(pixelsperchannel);
    temp1 = randomorder[position1];
    temp2 = randomorder[position2];
    randomorder[position1] = temp2;
    randomorder[position2] = temp1;
  }
}
 
I imagine you just loop through all your leds to shift colour up 1.

Then insert a new colour at the first led.

Loop through the above 10 times each for red and then white.

Loop through all of the above for however long you want to do this.
 
Status
Not open for further replies.
Back
Top