Skip to content

Instantly share code, notes, and snippets.

@brencerddwr
Last active February 22, 2019 19:05
Show Gist options
  • Save brencerddwr/85cc31227d2fcb0fc3818ab6a488c836 to your computer and use it in GitHub Desktop.
Save brencerddwr/85cc31227d2fcb0fc3818ab6a488c836 to your computer and use it in GitHub Desktop.
a pattern for an infinity mirror cleaned up and no longer requires first color to be CRGB::Black
#include <FastLED.h>
#define NUM_LEDS 30
#define DATA_PIN 6
int FrameDelay = 100;
int colorBarPosition = 1;
int frameCounter = 0;
//list of colors these are RGB hex colors
long colorPallet[] = {
CRGB::Blue,
CRGB::OrangeRed,
CRGB::Purple,
CRGB::Yellow,
CRGB::Red,
CRGB::Green,
CRGB::DeepPink,
CRGB::DarkCyan,
};
// calculate number of colors in the array, needed later
int numberofColors=(sizeof(colorPallet)/sizeof(colorPallet[0]));
// calculate color bar length based on number of leds
int colorBarLength = (NUM_LEDS/numberofColors)*4;
int palletPosition = 0;
bool clearLEDS = false;
// setup LED array
CRGBArray<NUM_LEDS> leds;
void setup()
{
delay(3000); // sanity delay for recovery if needed
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.setBrightness (128);
FastLED.show();
}
void loop()
{
EVERY_N_MILLISECONDS(FrameDelay)
{
// loop to shift LED values lower the array
for (int x=0; x<NUM_LEDS-1; x++)
{
leds[x] = leds[x+1];
}
// set last LED to the correct color based on color bar position and color bar length
if ((colorBarPosition <= colorBarLength) && !clearLEDS)
{
leds[NUM_LEDS-1] = colorPallet[palletPosition];
colorBarPosition++;
}
// check pallet position and bar position if at the end of both, start clear process
if ((palletPosition == numberofColors-1) && (colorBarPosition > colorBarLength) && !clearLEDS)
{
//set last LED to black(0x000000)
leds[NUM_LEDS-1]=CRGB::Black;
// reset variables to start for bar positions
palletPosition = 0;
colorBarPosition = 1;
//set clear LEDS flag to complete clearing
clearLEDS= true;
}
if ((colorBarPosition > colorBarLength) && !clearLEDS)
{
// reset bar position and step to next color
colorBarPosition = 1;
palletPosition = palletPosition+1;
}
// check if clearing process is complete, if so clear flag
if (clearLEDS && !leds(0,NUM_LEDS-1))
{
clearLEDS = false;
}
}
FastLED.show ();
}
@brencerddwr
Copy link
Author

I updated this file to make it easier to adapt to other projects.

I also eliminated the need to have CRGB::Black as the first color in the color pallet.

@neoneomaxim
Copy link

neoneomaxim commented Mar 19, 2017

Hi
I am working with the above code. It is working fine.
But I couldn't understand how the pixels shift from last LED

for (int x=0; x<NUM_LEDS-1; x++)
{
leds[x] = leds[x+1];
}

Also please explain quoted above what it does?

Thanks in advance

@brencerddwr
Copy link
Author

I just noticed this question.

It copies the value of leds[x+1] to leds[x] for each led in the array.

each line below is a single loop of the for loop
leds[1] copied to leds[0]
leds[2] copied to leds[1]
leds[3] copied to leds[2]
...
leds[NUM_LEDS-1] copied to leds[NUM_LEDS-2]

Does this help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment