If this is your first time using Flora, check out the Flora setup guide to get up and running.

You'll need to install the FastLED library to get this code working.

There is a great tutorial on how to install Arduino libraries in case you haven't done this before. It’s even simpler if using Arduino version 1.6.5 or newer…from the “Sketch” menu, select “Include Library→Manage Libraries…” and use the install option for FastLED. Done!

Then copy and paste the code below into your Arduino IDE.

Update the first few lines in the code with the number of LEDs you actually have in your hat.

Plug your Flora in with a USB cable.  Select Flora from your list of boards under the Tools menu, and choose your USB port under the Ports menu.  Press upload.

Download: file
#include <FastLED.h>

#define LED_PIN     12
#define TAIL_PIN    6
#define NUM_LEDS    20       //Number of LEDs in the front
#define NUM_TAIL    15       //Number of LEDs in the tail
#define NUM_BALL    4        //Number of LEDs in the ball 

#define BRIGHTNESS  255      
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
CRGB tail[NUM_TAIL];



#define UPDATES_PER_SECOND 100

// This example shows how to cross-fade between different color palettes
// using the function nblendPaletteTowardPalette.
//
// The basic idea is that you always have a "current palette" that you're
// pulling colors from with ColorFromPalette, and you have a "target palette"
// which is the 'next' palette that you want to get to.
// 
// After that, implementation is relatively simple: just periodically call
//   nblendPaletteTowardPalette( currentPalette, targetPalette);
// If the current palette is not yet equal to the target palette, this 
// function will make a few small changes to the current palette to make
// it slightly more like the target.  Over time, the current palette will
// come to be equal to the target.
// There's no need to test the current and target for equality; it's safe
// to keep calling nblendPaletteTowardPalette even after current reaches target.
// For faster blending, call nblendPaletteTowardPalette twice per loop.




void setup() {
  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.addLeds<LED_TYPE, TAIL_PIN, COLOR_ORDER>(tail, NUM_TAIL).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );
}


//COLOR PALETTES:  Here are several different ways to set up custom color palettes.  More info can be found at http://fastled.io/docs/3.1/group___colorpalletes.html
// Several palettes come pre-defined with FastLED: RainbowColors_p, ForestColors_p, OceanColors_p, HeatColors_p, LavaColors_p, RainbowStripeColors_p 
// so feel free to use those without needing to define them here.  
// Much more info and a list of color definititions can be found at https://github.com/FastLED/FastLED/wiki/Pixel-reference#chsv


// A red and white striped palette -- RedWhite_p
// "CRGB::Gray" is used as white to keep the brightness more uniform.
const TProgmemRGBPalette16 RedOrange_p FL_PROGMEM =
{  CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red, 
   CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Orange,
   CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red, 
   CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Orange };


   // A mostly green and dark green palette.  Greens_p
const TProgmemRGBPalette16 Greens_p FL_PROGMEM =
{  CRGB::Green, CRGB::Green, CRGB::DarkGreen, CRGB::DarkGreen, 
   CRGB::Green, CRGB::Green, CRGB::DarkGreen, CRGB::DarkGreen,
   CRGB::Green, CRGB::Green, CRGB::DarkGreen, CRGB::DarkGreen,
   CRGB::Green, CRGB::Green, CRGB::DarkGreen, CRGB::DarkGreen, };

// A pure "fairy light" palette with some brightness variations  FairyLight_p
#define HALFFAIRY ((CRGB::FairyLight & 0xFEFEFE) / 2)
#define QUARTERFAIRY ((CRGB::FairyLight & 0xFCFCFC) / 4)
const TProgmemRGBPalette16 FairyLight_p FL_PROGMEM =
{  CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, 
   HALFFAIRY,        HALFFAIRY,        CRGB::FairyLight, CRGB::FairyLight, 
   QUARTERFAIRY,     QUARTERFAIRY,     CRGB::FairyLight, CRGB::FairyLight, 
   CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight };

// A palette of soft snowflakes with the occasional bright one
const TProgmemRGBPalette16 Snow_p FL_PROGMEM =
{  0x304048, 0x304048, 0x304048, 0x304048,
   0x304048, 0x304048, 0x304048, 0x304048,
   0x304048, 0x304048, 0x304048, 0x304048,
   0x304048, 0x304048, 0x304048, 0xE0F0FF };

// A black "off" palette  Black_p;
const TProgmemRGBPalette16 Black_p FL_PROGMEM =
{  CRGB::Black};

// little twinkles
const TProgmemRGBPalette16 Twinkles_p FL_PROGMEM =
{  CRGB::Black, CRGB::Black, CRGB::Black, CRGB::Black, 
   CRGB::Black, CRGB::Black, CRGB::Black, CRGB::Black,
   CRGB::Gray, CRGB::Black, CRGB::Black, CRGB::Black,
   CRGB::White, CRGB::Black, CRGB::Black, CRGB::Black };

   // purple blue
const TProgmemRGBPalette16 Purples_p FL_PROGMEM =
{  CRGB::Purple, CRGB::Magenta, CRGB::Blue, CRGB::DarkBlue, 
   CRGB::Purple, CRGB::Magenta, CRGB::Blue, CRGB::DarkBlue,
   CRGB::Purple, CRGB::Magenta, CRGB::Blue, CRGB::DarkBlue, 
   CRGB::Purple, CRGB::Magenta, CRGB::Blue, CRGB::DarkBlue };


//define starting palettes here

CRGBPalette16 currentPalette( CRGB::Black);
CRGBPalette16 tailPalette(CRGB::Black);

CRGBPalette16 targetPalette( RedOrange_p );
CRGBPalette16 tailtargetPalette( Greens_p );


void loop()
{  ball();
  ChangePalettePeriodically();

  // Crossfade current palette slowly toward the target palette
  //
  // Each time that nblendPaletteTowardPalette is called, small changes
  // are made to currentPalette to bring it closer to matching targetPalette.
  // You can control how many changes are made in each call:
  //   - the default of 24 is a good balance
  //   - meaningful values are 1-48.  1=veeeeeeeery slow, 48=quickest
  //   - "0" means do not change the currentPalette at all; freeze
  
  uint8_t maxChanges = 24; 
  nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
  nblendPaletteTowardPalette( tailPalette, tailtargetPalette, maxChanges);

  static uint8_t startIndex = 0;
  startIndex = startIndex + 1; /* motion speed */
  FillLEDsFromPaletteColors( startIndex);

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

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
  uint8_t brightness = 120;          //You can change the brightness of the front and the tail independenty.
  uint8_t brightnesstail = 255;       // On my hat, having the tail brighter makes the hat look more balanced.
  
  for( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);
    colorIndex += 3;
  }

  for( int t = 0; t < (NUM_TAIL-NUM_BALL); t++) {
    tail[t] = ColorFromPalette( tailPalette, colorIndex + sin8(t*16), brightnesstail);
    colorIndex += 3;
  }
}

//Set up your playlist here

void ChangePalettePeriodically()
{
  uint8_t secondHand = (millis() / 1000) % 225;
  static uint8_t lastSecond = 250;
  
  if( lastSecond != secondHand) {
    lastSecond = secondHand;

     if( secondHand == 5)  { targetPalette = RedOrange_p; tailtargetPalette = Greens_p; }  // red and green Elf
     if( secondHand == 23)  { targetPalette = Black_p; tailtargetPalette = Black_p;} //fade through black
     if( secondHand == 25)  { targetPalette = RainbowColors_p; tailtargetPalette = RainbowColors_p; } //Rainbow
     if( secondHand == 50)  { targetPalette = CloudColors_p; tailtargetPalette = CloudColors_p;} //Clouds
     if( secondHand == 75)  { targetPalette = Twinkles_p; tailtargetPalette = FairyLight_p;} //Fairy lights & Twinkles
     if( secondHand == 100)  { targetPalette = ForestColors_p; tailtargetPalette = OceanColors_p; }  // Green and blue
     if( secondHand == 125)  { targetPalette = FairyLight_p; tailtargetPalette = Greens_p; } // White and red "Santa"
     if( secondHand == 147)  { targetPalette = Black_p; tailtargetPalette = Black_p;} //fade through black
     if( secondHand == 150)  { targetPalette = Snow_p; tailtargetPalette = RedOrange_p;} //Green and white elf
     if( secondHand == 175)  { targetPalette = Purples_p; tailtargetPalette = Purples_p;} //Purple palette
     if( secondHand == 200)  { targetPalette = Twinkles_p; tailtargetPalette = Twinkles_p;}  //twinkles
  }
}

void ball(){

for (uint8_t b = (NUM_TAIL - NUM_BALL); b < NUM_TAIL; b++)
tail[b] = CHSV(160, 0, 200);   
  
FastLED.show();
}

This code is fairly easy to modify.  You can create your own color palettes and set up your own "playlist" of animations that will change over time.  

The way it's written, the code will cycle through several different modes over 225 seconds.  Play with the different palettes to come up with your own modes and your own timing.

This guide was first published on Dec 05, 2016. It was last updated on Dec 05, 2016. This page (Code) was last updated on Oct 31, 2019.