Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h"
- //
- //******************
- //*** WORKS ***
- //******************
- //
- // This sketch is based HEAVILY on the Timed Playlist
- // by Mark Kriegsman -- a key contributor to the
- // FastLED community on Google+
- // There are modifications made by me, so if things
- // don't run, it's my fault, not Mark's or FastLed.
- // A "ResetPlaylist" method is provided so that the playlist can be
- // restarted from a custom external trigger, e.g., a button or event.
- // For demonstration purposes, the playlist is reset if the
- // sketch receives a letter "r" on the serial port.
- // -Mark Kriegsman, January 2015
- //
- //*****************************************************************
- //*** The following ***
- //*** VOID LIST ***
- //*** sometimes isn't needed, depending ***
- //*** on how things compile. I use it just to be safe. ***
- //*****************************************************************
- void nextPattern();
- void rainbow();
- void rainbowWithGlitter();
- void addGlitter( fract8 chanceOfGlitter); //NOT void addGlitter();
- //void confetti();
- void sinelon();
- void bpm();
- void juggle();
- void applause();
- void fadeToBlack();
- void oneByOneFill();
- //
- //
- //#define DATA_PIN 8
- // I skipped the above because lower down I define for
- // a bunch of data pins
- //
- //#define CLK_PIN 4
- // The above isn't needed for my WS2812B's
- //
- #define LED_TYPE WS2812B
- // The above states the type of LEDs I want to run
- //
- #define COLOR_ORDER GRB
- // Many strip types prefer RGB for the above.
- // If your colors aren't displaying properly
- // try changing the order
- //
- #define NUM_LEDS 300
- // The above notes how many LEDs you're controlling.
- // Since I'm using multiple strips of different lengths
- // I've set this number for the longest strip.
- //
- CRGB leds[NUM_LEDS];
- // One of these days I'll learn what the above does :)
- //
- #define FRAMES_PER_SECOND 120
- // Not sure what this does at this point
- // I'm sure it sets some parameter(s) for an effect(s) below
- //
- // List of patterns to cycle through. Each is defined as a separate function below.
- typedef void (*SimplePattern)();
- typedef SimplePattern SimplePatternList[];
- typedef struct { SimplePattern mPattern; uint16_t mTime; } PatternAndTime;
- typedef PatternAndTime PatternAndTimeList[];
- //
- // ********************
- //----------*** THE PLAYLIST ***----------
- // ********************
- //
- // The numbers after the effect name below are times are in seconds,
- // but could be changed to milliseconds if desired;
- // there's some discussion further below.
- //
- const PatternAndTimeList gPlaylist = {
- // { confetti, 120 },
- // I don't care for the above pattern so have commented it out
- // to remove it from the playlist until I can modify it.
- { juggle, 300 },
- { oneByOneFill, 300},
- { bpm, 300 },
- { rainbowWithGlitter, 300 },
- //{ applause, 120 }, // I decided to pull this one from the list as well
- { fadeToBlack, 3 } //NOTE, no comma on the last effect
- };
- //
- // If you want the playlist to loop forever, set the
- // bool function below to true. If you want the playlist to
- // play once, and then stay on the final pattern
- // until the playlist is reset, set it to false.
- bool gLoopPlaylist = true;
- //
- //******************
- //*** VOID SETUP ***
- //******************
- //
- void setup() {
- //
- delay(3000);
- // Above is a 3 second delay before anything starts
- // for recovery purposes
- //
- FastLED.clear();
- // Above turns all the LEDs "off" so there aren't
- // any lingering effects from previous sketches.
- //
- FastLED.setBrightness(127); // Can be 0 to 255
- // The above sets the overall brightness for the entire
- // sketch. For example, if it's set to 64, that means
- // everything later will be one-quarter as bright as it's
- // actually shown -- 64/255 = one-quarter. For
- // leds[i] = CHSV( 0, 255, 127); the actual brightness
- // would be one-quarter of 127, or about 32.
- //
- // The following lines tell FastLED about the
- // multiple strips I have. LED_TYPE, COLOR_ORDER, and NUM_LEDS
- // were set above.
- //
- FastLED.addLeds<LED_TYPE, 8, COLOR_ORDER>(leds, NUM_LEDS);
- // the 8 above refers to pin 8
- FastLED.addLeds<LED_TYPE, 9, COLOR_ORDER>(leds, NUM_LEDS);
- // the 8 above refers to pin 9
- FastLED.addLeds<LED_TYPE, 10, COLOR_ORDER>(leds, NUM_LEDS);
- // the 8 above refers to pin 10
- FastLED.addLeds<LED_TYPE, 11, COLOR_ORDER>(leds, NUM_LEDS);
- // the 8 above refers to pin 11
- FastLED.addLeds<LED_TYPE, 12, COLOR_ORDER>(leds, NUM_LEDS);
- // the 8 above refers to pin 12
- //
- RestartPlaylist();
- //
- Serial.begin(57600);
- // I believe the above sets up communication back to the
- // computer. I think it's only needed if you want to "reset"
- }
- //
- uint8_t gCurrentTrackNumber = 0; // Index number of which pattern is current
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- bool gRestartPlaylistFlag = false;
- //
- //*****************
- //*** VOID LOOP ***
- //*****************
- //
- void loop()
- {
- // Call the current pattern function once, updating the 'leds' array
- gPlaylist[gCurrentTrackNumber].mPattern();
- // send the 'leds' array out to the actual LED strip
- //
- FastLED.show(); //Displays the LEDs you've called/set above
- //
- // insert a delay to keep the framerate modest
- // I don't know why the delay includes dividing by the frames instead of just a number.
- FastLED.delay(1000/FRAMES_PER_SECOND);
- //
- // For demo purposes, restart the playlist any time we read
- // the letter "r" character from the serial port. Type "r" into
- // the Arduino serial monitor and press return to restart the playlist.
- // In practice, you could have this check a 'restart' button, or something similar.
- if( Serial.read() == 'r') RestartPlaylist();
- //
- // do some periodic updates
- EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- //
- // Here's where we do two things: switch patterns, and also set the
- // 'virtual timer' for how long until the NEXT pattern switch.
- //
- // Instead of EVERY_N_SECONDS(10) { nextPattern(); }, we use a special
- // variation that allows us to get at the pattern timer object itself,
- // and change the timer period every time we change the pattern.
- //
- // You could also do this with EVERY_N_MILLISECONDS_I and have the
- // times be expressed in milliseconds instead of seconds.
- {
- EVERY_N_SECONDS_I(patternTimer,gPlaylist[gCurrentTrackNumber].mTime) {
- nextPattern();
- patternTimer.setPeriod( gPlaylist[gCurrentTrackNumber].mTime);
- }
- // Here's where we handle restarting the playlist if the 'reset' flag
- // has been set. There are a few steps:
- if( gRestartPlaylistFlag ) {
- // Set the 'current pattern number' back to zero
- gCurrentTrackNumber = 0;
- // Set the playback duration for this patter to it's correct time
- patternTimer.setPeriod( gPlaylist[gCurrentTrackNumber].mTime);
- // Reset the pattern timer so that we start marking time from right now
- patternTimer.reset();
- // Finally, clear the gRestartPlaylistFlag flag
- gRestartPlaylistFlag = false;
- }
- }
- }
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
- void nextPattern()
- {
- // add one to the current pattern number
- gCurrentTrackNumber = gCurrentTrackNumber + 1;
- // If we've come to the end of the playlist, we can either
- // automatically restart it at the beginning, or just stay at the end.
- if( gCurrentTrackNumber == ARRAY_SIZE( gPlaylist) ) {
- if( gLoopPlaylist == true) {
- // restart at beginning
- gCurrentTrackNumber = 0;
- } else {
- // stay on the last track
- gCurrentTrackNumber--;
- }
- }
- }
- void RestartPlaylist()
- {
- gRestartPlaylistFlag = true;
- }
- //
- //**************************************************
- //*** BELOW IS WHERE THE INDIVIDUAL EFFECT LOOPS ***
- //**** ARE KEPT. THE VOID LOOP REFERS TO THEM *****
- //******** WHEN IT'S TIME TO PLAY THEM *************
- //**************************************************
- //
- //***************
- //*** RAINBOW ***
- //***************
- //
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow( leds, NUM_LEDS, gHue, 7);
- }
- //
- //****************************
- //*** RAINBOW WITH GLITTER ***
- //****************************
- //
- void rainbowWithGlitter()
- {
- // built-in FastLED rainbow, plus some random sparkly glitter
- rainbow();
- addGlitter(80);
- }
- //
- //*******************
- //*** ADD GLITTER ***
- //*******************
- //
- void addGlitter( fract8 chanceOfGlitter)
- {
- if( random8() < chanceOfGlitter) {
- leds[ random16(NUM_LEDS) ] += CRGB::White;
- }
- }
- //
- //****************
- //*** CONFETTI ***
- //****************
- //
- void confetti()
- {
- // random colored speckles that blink in and fade smoothly
- fadeToBlackBy( leds, NUM_LEDS, 10);
- int pos = random16(NUM_LEDS);
- leds[pos] += CHSV( gHue + random8(64), 200, 255);
- }
- //
- //***************
- //*** BPM ***
- //***************
- //
- void bpm()
- {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 62;
- CRGBPalette16 palette = PartyColors_p;
- uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
- for( int i = 0; i < NUM_LEDS; i++) { //9948
- leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
- }
- }
- //
- //***************
- //*** JUGGLE ***
- //***************
- //
- void juggle() {
- // eight colored dots, weaving in and out of sync with each other
- fadeToBlackBy( leds, NUM_LEDS, 20);
- byte dothue = 0;
- for( int i = 0; i < 8; i++) {
- leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
- dothue += 32;
- }
- }
- //
- //****************
- //*** APPLAUSE ***
- //****************
- //
- void applause()
- {
- static uint16_t lastPixel = 0;
- fadeToBlackBy( leds, NUM_LEDS, 32);
- leds[lastPixel] = CHSV(random8(HUE_BLUE,HUE_PURPLE),255,255);
- lastPixel = random16(NUM_LEDS);
- leds[lastPixel] = CRGB::White;
- }
- //
- //*********************
- //*** FADE TO BLACK ***
- //*********************
- //
- // An "animation" to just fade to black. Useful as the last track
- // in a non-looping performance-oriented playlist.
- void fadeToBlack()
- {
- fadeToBlackBy( leds, NUM_LEDS, 10);
- }
- //
- //***********************
- //*** ONE BY ONE FILL ***
- //***********************
- //
- void oneByOneFill()
- {
- //
- for(int i = 0; i < NUM_LEDS; i++) {
- // The above starts at the first LED, 0, and then counts up one every time it loops
- // as long as it's less than the total number of LEDs set in the initial parameters.
- leds[i] = CHSV( 0, 255, 255);
- // The above sets the current LED to red at full brightness, using Hue Saturation Value
- // **BUT** the FastLED.setBrightness(63) set in the void setup
- // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
- // Hue is the color, Saturation is the intensity, Value is the brightness
- // 0 brightness = black
- // Another method is below
- // leds[i] = CRGB::Red;
- FastLED.show(); // tells FastLED to display what you've asked for above
- // ***** MAKING IT A MOVING DOT *****
- // leds[i] = CHSV( 0, 0, 0);
- // if you uncomment the above line,it will turn off the current LED
- // above, but it won't happen until the next FastLED.show ();
- // It's turning itself off as you go along, so you'll only have a dot
- // moving down the strip.
- // Another method is below
- // leds[i] = CRGB::Black;
- // By commenting it, the strips will now fill with the color instead of a dot
- delay(3); // A VERY quick delay before the next step 3/1000 of a second
- // The way I understand it, because this portion is enclosed with its own
- // { and } this portion will keep repeating until "i" reaches the number
- // of LEDs we set in the initial parameters. Then it will go to the next
- // part between the next pair of { and }. I'm kind of guessing though.
- }
- // ***** FILLING THE STRIPS WITH BLUE *****
- for(int i = 0; i < NUM_LEDS; i++) {
- // The above starts at the first LED, 0, and then counts up one every time it loops
- // as long as it's less than the total number of LEDs set in the initial parameters.
- leds[i] = CHSV( 160, 255, 255);
- // The above sets the current LED to blue at full brightness, using Hue Saturation Value
- // **BUT** the FastLED.setBrightness(63) set in the void setup
- // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
- // Hue is the color, Saturation is the intensity, Value is the brightness
- // 0 brightness = black
- // Another method is below
- // leds[i] = CRGB::Blue;
- FastLED.show(); // tells FastLED to display what you've asked for above
- // ***** MAKING IT A MOVING DOT *****
- // leds[i] = CHSV( 0, 0, 0);
- // if you uncomment the above line,it will turn off the current LED
- // above, but it won't happen until the next FastLED.show ();
- // It's turning itself off as you go along, so you'll only have a dot
- // moving down the strip.
- // Another method is below
- // leds[i] = CRGB::Black;
- // By commenting it, the strips will now fill with the color instead of a dot
- delay(3); // A VERY quick delay before the next step 3/1000 of a second
- // The way I understand it, because this portion is enclosed with its own
- // { and } this portion will keep repeating until "i" reaches the number
- // of LEDs we set in the initial parameters. Then it will go to the next
- // part between the next pair of { and }. I'm kind of guessing though.
- }
- // ***** FILLING THE STRIPS WITH GREEN *****
- for(int i = 0; i < NUM_LEDS; i++) {
- // The above starts at the first LED, 0, and then counts up one every time it loops
- // as long as it's less than the total number of LEDs set in the initial parameters.
- leds[i] = CHSV( 96, 255, 255);
- // The above sets the current LED to green at full brightness, using Hue Saturation Value
- // **BUT** the FastLED.setBrightness(63) set in the void setup
- // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
- // Hue is the color, Saturation is the intensity, Value is the brightness
- // 0 brightness = black max brightness is 255
- // Another method is below
- // leds[i] = CRGB::Green;
- FastLED.show(); // tells FastLED to display what you've asked for above
- // ***** MAKING IT A MOVING DOT *****
- // leds[i] = CHSV( 0, 0, 0);
- // if you uncomment the above line,it will turn off the current LED
- // above, but it won't happen until the next FastLED.show ();
- // It's turning itself off as you go along, so you'll only have a dot
- // moving down the strip.
- // Another method is below
- // leds[i] = CRGB::Black;
- // By commenting it, the strips will fill with the color instead of a dot
- delay(3); // A VERY quick delay before the next step 3/1000 of a second
- // The way I understand it, because this portion is enclosed with its own
- // { and } this portion will keep repeating until "i" reaches the number
- // of LEDs we set in the initial parameters. Then it will go to the next
- // part between the next pair of { and }. I'm kind of guessing though.
- }
- // ***** FILLING THE STRIPS WITH WHITE *****
- // NOTE: FULL BRIGHT WHITE USES 60 MILLIAMPS
- // MULTIPLIED BY THE NUMBER OF LEDS YOU HAVE LIT!
- // MAKE SURE YOUR POWER SUPPLY IS SUFFICIENT
- // IF YOU RAISE THE FastLED.setBrightness() SETTING
- // MAKE SURE YOU LOWER THE "V" BELOW IF NEEDED
- for(int i = 0; i < NUM_LEDS; i++) {
- // The above starts at the first LED, 0, and then counts up one every time it loops
- // as long as it's less than the total number of LEDs set in the initial parameters.
- leds[i] = CHSV( 0, 0, 255);
- // The above sets the current LED to white at full brightness, using Hue Saturation Value
- // **BUT** the FastLED.setBrightness(63) set in the void setup
- // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
- // Hue is the color, Saturation is the intensity, Value is the brightness
- // For white, I believe you can use any Hue value as long as Saturation is 0
- // 0 brightness = black max brightness is 255
- // Another method is below
- // leds[i] = CRGB::White;
- FastLED.show(); // tells FastLED to display what you've asked for above
- // ***** MAKING IT A MOVING DOT *****
- // leds[i] = CHSV( 0, 0, 0);
- // if you uncomment the above line,it will turn off the current LED
- // above, but it won't happen until the next FastLED.show ();
- // It's turning itself off as you go along, so you'll only have a dot
- // moving down the strip.
- // Another method is below
- // leds[i] = CRGB::Black;
- // By commenting it, the strips will fill with the color instead of a dot
- delay(3); // A VERY quick delay before the next step 3/1000 of a second
- // The way I understand it, because this portion is enclosed with its own
- // { and } this portion will keep repeating until "i" reaches the number
- // of LEDs we set in the initial parameters. Then it will go to the next
- // part between the next pair of { and }. I'm kind of guessing though.
- }
- } // This designates the end of the void loop so the program
- // will now go to the start of loop again.
- //**************************************
- //**************************************
- //*** KEEP THIS AT THE END ***
- //**************************************
- //*** WHEN ADDING A NEW EFFECT LOOP ***
- //*** MAKE SURE YOU ALSO INCLUDE IT ***
- //*** ON THE VOID LIST AND PLAY LIST ***
- //*** NEAR THE START OF THE SKETCH ***
- //**************************************
- //**************************************
Advertisement
Add Comment
Please, Sign In to add comment