CYTHEIA AIOpen the editor

FastLED Comet

A comet of colour sweeps along a 30-pixel LED strip, leaving a fading tail. Written with the popular FastLED library.

Open this circuit and run it

Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.

How it works

The strip is 30 WS2812B LEDs on a single data wire (pin 6), the same kind of pixel as in the NeoPixel example but driven with the FastLED library. FastLED keeps the colour of every pixel in an array, leds[]. You draw by changing the array, and FastLED.show() sends it to the strip. The brightness is limited to 80 out of 255.

Each pass of loop() makes one frame. fadeToBlackBy(leds, NUM_LEDS, 40) dims every pixel a little, which is what leaves the fading tail behind the head. beatsin16(20, 0, NUM_LEDS - 1) is a smooth wave that swings between pixel 0 and pixel 29 twenty times a minute; its current value is the comet’s position, where a bright pixel is placed in the current hue. EVERY_N_MILLISECONDS(30) nudges the hue forward every 30 milliseconds so the comet slowly changes colour. FastLED.delay(10) paces the animation at roughly 100 frames a second.

Parts

Wiring

Arduino code

// FastLED Comet
// The strip's colours live in the leds[] array. Each frame the whole strip
// fades a little (the tail), a bright pixel is placed at a position that
// swings back and forth, and show() sends the result to the strip.
#include <FastLED.h>

#define NUM_LEDS 30
#define DATA_PIN 6

CRGB leds[NUM_LEDS];
uint8_t hue = 0;

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(80);
}

void loop() {
  fadeToBlackBy(leds, NUM_LEDS, 40);

  // 20 swings a minute between the first and last pixel
  int pos = beatsin16(20, 0, NUM_LEDS - 1);
  leds[pos] = CHSV(hue, 255, 255);

  FastLED.show();

  EVERY_N_MILLISECONDS(30) {
    hue++;
  }
  FastLED.delay(10);
}

Try changing it

Open this circuit and run it

Next challenge

More projects in LED & Lighting