Before You Start

If this is your first foray into the world of arduino-based microcontrollers, you'll need to install some software first. Head over to the Circuit Playground Lesson 0 guide for detailed installation and setup instructions.  

You'll only need to do all this once, so be a little patient if it seems like a lot!

FastLED Library

You will also need to install the FastLED library in Arduino (Sketch > Include Library > Manage Libraries...)

One other note:  if you're using FastLED with Circuit Playground, be sure to #include the Circuit Playground library FIRST and the FastLED library second, or you may run into problems.

Upload Code

Once you've got everything installed and your computer can talk to the Circuit Playground, it's time to upload the code.

Plug your Circuit Playground into your computer and select the Circuit Plaground under Tools > Boards.  Then select the Circuit Playground as the Port.

Copy and paste this code into a new Arduino window and click "upload".

// Code by Erin St Blaine for Adafruit.com, based on FastLED animations by Mark Kriegsman

#include <Adafruit_CircuitPlayground.h>
#include <FastLED.h>
#include <Wire.h>
#include <SPI.h>
#include <math.h>

// tell FastLED all about the Circuit Playground's layout

#define CP_PIN      17   //circuit playground's neopixels live on pin 17
#define DATA_PIN     9    //LED data on pin 9
#define CLK_PIN     6     // Clock pin on pin 6
#define NUM_LEDS     300   // total number of LEDs in your strip
#define COLOR_ORDER BGR    // Dotstar color order -- change this if your colors are coming out wrong
#define COLOR_ORDER_CP GRB  // Circuit Playground color order

uint8_t brightness = 250;  // Set brightness level

int STEPS = 6;  //makes the rainbow colors more or less spread out
int NUM_MODES = 5;  // change this number if you add or subtract modes
int CYCLETIME = 60;  // number of seconds on each mode, for mode cycling

CRGB leds[NUM_LEDS];      // set up an LED array

CRGBPalette16 currentPalette;
TBlendType    currentBlending;

int ledMode = 0;       //Initial mode 
bool leftButtonPressed;
bool rightButtonPressed;


// SETUP -----------------------------------------------------
void setup() {
  Serial.begin(57600);
  CircuitPlayground.begin();
  FastLED.addLeds<WS2812B, CP_PIN, COLOR_ORDER_CP>(leds, 10).setCorrection( TypicalLEDStrip );
  FastLED.addLeds<DOTSTAR,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // Use this line for if using dotstars
  //FastLED.addLeds<WS2812B, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); // Use this line if using neopixels
  currentBlending = LINEARBLEND;
  set_max_power_in_volts_and_milliamps(5, 5000);               // FastLED 2.1 Power management set at 5V, 5000mA

  
}

void loop()  {

  leftButtonPressed = CircuitPlayground.leftButton();
  rightButtonPressed = CircuitPlayground.rightButton();

  if (leftButtonPressed) {  //left button cycles through modes
    clearpixels(); 
    ledMode=ledMode+1;
    delay(300);
    if (ledMode > NUM_MODES){
    ledMode=0;
     }
  }
    if (rightButtonPressed) {  // on off button
    ledMode=99;
  
    }
  
 switch (ledMode) {
       case 0: modeCycle(); break;
       case 1: currentPalette = RainbowColors_p; rainbow(); break;
       case 2: currentPalette = OceanColors_p; rainbow(); break;                    
       case 3: currentPalette = LavaColors_p; rainbow(); break;   
       case 4: currentPalette = ForestColors_p; rainbow(); break;
       case 5: currentPalette = PartyColors_p; rainbow(); break;
       case 99: clearpixels(); break;
       
}
}
void clearpixels()
{
  CircuitPlayground.clearPixels();
   for( int i = 0; i < NUM_LEDS; i++) {
   leds[i]= CRGB::Black;
    }
   FastLED.show();
}

void rainbow()
{
  
  static uint8_t startIndex = 0;
  startIndex = startIndex + 1; /* motion speed */

  FillLEDsFromPaletteColors( startIndex);

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

//this bit is in every palette mode, needs to be in there just once
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{ 
  for( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
    colorIndex += STEPS;
  }
}


int cycleMode=0;

void modeCycle()
{
   switch (cycleMode) {
       case 0: currentPalette = RainbowColors_p; rainbow(); break;
       case 1: currentPalette = OceanColors_p; rainbow(); break;                    
       case 2: currentPalette = LavaColors_p; rainbow(); break;   
       case 3: currentPalette = ForestColors_p; rainbow(); break;
       case 4: currentPalette = PartyColors_p; rainbow(); break;
       case 5: cycleMode=0; break;
}

   EVERY_N_SECONDS(CYCLETIME) {
    cycleMode++;
   }
   
}

If all goes well, the lights on the Circuit Playground will come on.  Press the right side button to turn the lights on and off.  Press the left side button to toggle between color modes.  

The initial mode will cycle through five different color modes, changing every 60 seconds.  Change this duration in the code with the CYCLETIME variable.  

The other modes use FastLEDs built in color palettes for a variety of different looks.

Add your own modes or customize the modes that are already there.  If you want to add your own color palettes, check out this Neopixel Parasol guide for ideas and instructions on how to do that with FastLED.

This guide was first published on Aug 16, 2017. It was last updated on Aug 16, 2017.

This page (Code) was last updated on Jul 18, 2017.

Text editor powered by tinymce.