// Forum: https://forum.arduino.cc/t/fastled-visual-looping-issue/1134527
// This Wokwi project: https://wokwi.com/projects/366722641131463681
//
// A led sequence of four leds:
// The first is dim
// The second is bright
// The third is dim
// The fourth is off (the default is off)
//
// Update: variable array size for "sequence" array.
#include <FastLED.h>
#define NUM_LEDS 12 // Number of NeoPixel leds
#define PIN 5 // Data pin for ledstrip
int index = 0; // index of led at the front
CRGB leds[NUM_LEDS];
CRGB sequence[] = {CRGB::DarkViolet, CRGB::Violet, CRGB::DarkViolet, CRGB::Black};
void setup()
{
FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.clear(); // turn all leds off
}
void loop()
{
EVERY_N_MILLISECONDS(150)
{
// put the sequence somewhere on the leds
int n = sizeof(sequence)/sizeof(CRGB);
for(int i=0; i<n; i++)
leds[ToIndex(index-i)] = sequence[i];
// Update the ledstrip
FastLED.show();
// advance index for next time
index++;
if(index >= NUM_LEDS)
index = 0;
}
}
// Bring a number into the range of the array.
// A negative number can not be too large.
// but it will work for this code.
int ToIndex(int i)
{
if(i < 0)
i += NUM_LEDS;
return(i % NUM_LEDS);
}