So I'm new to writing code and have been trying to figure out how

So I’m new to writing code and have been trying to figure out how to isolate two colors to into a pattern. I want two colors to cycle through the strip and I want to change the width of the stripes by changing how many leds the stripe will consist of (possibly with a fastfader.h trailing effect) to create an anti aliased edge. Can anyone lead me in the right direction with an example please?

I see the

I am having trouble following your description. Have you a video of something that inspired your idea?

Welcome, Jason! I’m not entirely sure I understand the effect you’re looking for, but I think I have the general idea. (The big question left open in my mind about what you described is this: should the stripes completely cover the LED strip like XXYYXXYYXXYY, or do you want some ‘black’ showing between them like XX__YY__XX__YY?) In any case, my overall recommendation is to start with the simple idea, get it working, play with it, and then add bells and whistles.

So for example: I’d first try to get it all up and running without the anti-aliasing or fading – just the stripes with controlled width. Then once you’ve got that working, add in features.

This code draws moving stripes of one color on a black background. The way it works is that it thinks of the LEDs along the strip as part of a repeating cycle: some stripe pixels, some background pixels, some stripe pixels, some background pixels, and so on. The length of the whole cycle is the stripe length plus the length of the gap between stripes. So to draw the stripes, we just loop down every pixel on the strip, figure out where in the cycle we are for that pixel, and draw the right color. The drawStripes function takes one argument telling it where in the cycle to begin, and by changing that in the loop() function, the stripes should move.

#include <FastLED.h>
#define NUM_LEDS 50
CRGB leds[NUM_LEDS];

uint8_t stripeWidth = 6;
uint8_t gapBetweenStripes = 10;
CRGB stripeColor = CRGB::Orange;
CRGB backgroundColor = CRGB::Black;

void setup() {
delay(3000);
FastLED.addLeds<WS2811,3,GRB>(leds, NUM_LEDS);
}

uint8_t gPhase = 0;
void loop() {
// advance overall starting phase
gPhase = gPhase + 1;
// keep it in bounds
gPhase = gPhase % (stripeWidth + gapBetweenStripes);

drawStripes( gPhase);
FastLED.show();
FastLED.delay(20);
}

void drawStripes( uint8_t startingPhase)
{
// calculate the length of the repeating cycle
uint8_t cycleLength = (stripeWidth + gapBetweenStripes);
uint8_t curPhase = startingPhase;

// loop over all the LEDS
for( uint16_t i = 0; i < NUM_LEDS; i++) {
// for each LED, figure out where in the cycle
// we are, and choose the right color
CRGB ledColor;
if( curPhase < stripeWidth) {
// inside a stripe
ledColor = stripeColor;
} else {
// in the gap between stripes
ledColor = backgroundColor;
}

leds[i] = ledColor;

}

// advance to next phase for next pixel
curPhase++;
// if we hit the end of the cycle, start at zero again
if( curPhase == cycleLength) {
curPhase = 0;
}
}

With a relatively small change, we can have the first and last pixel of the stripe be ‘dimmed’ down for a quick-and-dirty anti-aliased effect:

void drawStripes( uint8_t startingPhase)
{
// calculate the length of the repeating cycle
uint8_t cycleLength = (stripeWidth + gapBetweenStripes);
uint8_t curPhase = startingPhase;

// loop over all the LEDS
for( uint16_t i = 0; i < NUM_LEDS; i++) {
// for each LED, figure out where in the cycle
// we are, and choose the right color
CRGB ledColor;
if( curPhase < stripeWidth) {
// inside a stripe
ledColor = stripeColor;

  // first and last pixel should be dimmer
  if( curPhase == 0 || curPhase == (stripeWidth-1)) {
    // dim first and last pixel to 1/4 brightness.
    ledColor.nscale8_video( 64);
  }

} else {
  // in the gap between stripes
  ledColor = backgroundColor;
}

leds[i] = ledColor;

}

// advance to next phase for next pixel
curPhase++;
// if we hit the end of the cycle, start at zero again
if( curPhase == cycleLength) {
curPhase = 0;
}
}

I hope this helps give you a start. For multiple stripes, you could expand this code to think of the ‘cycle’ as having four parts instead of just two: stripe1, gap1, stripe2, and gap2. There are other ways to do it, too, of course! Let us know what you come up with after some experimentation!

@Mark_Ortiz Yes, the spiraling Christmas tree going to try Mark Kriegman’s code today and test it out. I am working with a TM1803 tricolor strip from RadioShack so I am limited by pixel resolution. I’m really impressed with some f the work which has been created in this community though!

Ah! For that effect your fastest route might be to use a color palette with just the colors you want. Check out the Palette examples that come with the library. It’d be a snap to make a palette of just Red and Green stripes…

Mark, the code you gave me fills all of the leds on the strip and alternates between the two defined colors. Is there a way to fade between the two of them?

This code flashes the whole strip on and off for me too. Jason - I don’t think it was meant to do that. I feel like something is inside the loop that’s meant to be outside or vice-versa

@Mark_Kriegsman I just tried creating my own palette and replacing some of the palettes in the colorpalette example. I don’t think i’m going about this correctly. I got the code to compile, but nothing showed up on the strip. Do I need to create a palette of colors outside of the .ino file so that when I try to use the palette it uses from colorpalette.h / colorpalette.cpp file?

Heh oops. Sorry about that. I’ll check later on- have to make lunch for the troops and deal with real life for a bit.

Yep-- my bad, sorry. Try this… and also change the FastLED.delay to about 40 or so.

Beyond this, also do check out the palette examples for how to choose your own colors and make multiple stripes.

void drawStripes( uint8_t startingPhase)
{
// calculate the length of the repeating cycle
uint8_t cycleLength = (stripeWidth + gapBetweenStripes);
uint8_t curPhase = startingPhase;

// loop over all the LEDS
for( uint16_t i = 0; i < NUM_LEDS; i++) {
// for each LED, figure out where in the cycle
// we are, and choose the right color
CRGB ledColor;
if( curPhase < stripeWidth) {
// inside a stripe
ledColor = stripeColor;
// first and last pixel should be dimmer
if( curPhase == 0 || curPhase == (stripeWidth-1)) {
// dim first and last pixel to 1/4 brightness.
ledColor.nscale8_video( 64);
}
} else {
// in the gap between stripes
ledColor = backgroundColor;
}

leds[i] = ledColor;
// advance to next phase for next pixel
curPhase++;
// if we hit the end of the cycle, start at zero again
if( curPhase == cycleLength) {
  curPhase = 0;
}

}
}

Here’s code that:

  • sets up a palette of red and green stripes
  • uses the palette colors to fill the LEDs
  • animates the stripes down the LED strip

Because it’s using the “BLEND” option on fill_palette, the colors automatically crossfade into one another where they meet.

Here’s the relevant part, just for easy review; the full sketch is in the gist URL above.

CRGBPalette16 currentPalette;

void setupStripedPalette( CRGB A, CRGB AB, CRGB B, CRGB BA)
{
// Sets up a palette with alternating stripes of
// colors “A” and “B” – with color “AB” between
// where A fades into B, and color “BA” where B fades
// into A.
// The stripes of “A” are narrower than the stripes of “B”.
currentPalette = CRGBPalette16(
A, A, A, A, AB, B, B, B, B, B, B, B, B, B, B, BA );
}

void setup() {
FastLED.addLeds<WS2811,LED_PIN,GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

setupStripedPalette( CRGB::Red, CRGB::Red, CRGB::Green, CRGB::Green);
}

void loop()
{
static uint8_t startIndex = 0;
startIndex = startIndex + 2; /* higher = faster motion */

fill_palette( leds, NUM_LEDS,
startIndex, 8, /* higher = narrower stripes */
currentPalette, 255, BLEND);

FastLED.show();
FastLED.delay(1000 / 60);
}

Myself, I particularly like the ability to set up a palette with a simple, explicit pattern of colors, as shown in “setupStripedPalette” here. If you want the “A” color stripes wider, just add more A’s and delete some B’s.

You can, of course, create a palette from code that uses a parameterized loop to adjust the width of the stripes, but for ‘getting started’ purposes, I really like being able to just type out the pattern I want.

@Mark_Kriegsman Thanks Mark, that code worked well! Here’s a video of my girlfriends tree with salmon and goldenrod used for colors: http://youtu.be/scl-oYYK15s

Hey! That’s great! Congratulations on getting it going.