Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h"
- // 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 fadingColors();
- //
- //
- //#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
- #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 100
- // 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 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, 300 },
- { fadeToBlack, 1 },
- { fadingColors, 300 },
- { juggle, 300 },
- { bpm, 300 },
- { rainbowWithGlitter, 300 },
- { juggle, 300 },
- { applause, 300 },
- { fadeToBlack, 3 }
- };
- //
- // 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() {
- //
- delay(3000); // 3 second delay for recovery
- //
- FastLED.setBrightness(64); // 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()
- {
- // 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
- 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;
- }
- void rainbow()
- {
- // FastLED's built-in rainbow generator
- fill_rainbow( leds, NUM_LEDS, gHue, 7);
- }
- void rainbowWithGlitter()
- {
- // built-in FastLED rainbow, plus some random sparkly glitter
- rainbow();
- addGlitter(80);
- }
- void addGlitter( fract8 chanceOfGlitter)
- {
- if( random8() < chanceOfGlitter) {
- leds[ random16(NUM_LEDS) ] += CRGB::White;
- }
- }
- 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);
- }
- 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));
- }
- }
- 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;
- }
- }
- // An animation to play while the crowd goes wild after the big performance
- 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;
- }
- // 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);
- }
- void fadingColors() {
- for(int j = 0; j < 3; j++ ) { // This is increasing the case numbers 0 to 2
- // FADING IN
- for(int k = 0; k < 254; k++) { // This is INcreasing k from 0 to 255
- switch(j)
- {
- case 0: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 0, 255, k); break; //should fade in red
- case 1: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 96, 255, k); break; //should fade in green
- case 2: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 160, 255, k); break; //should fade in blue
- }
- FastLED.show(); // Tells FastLED to display the LEDS
- delay(15); // Determines the speed of the fade
- }
- // HOLD COLOR
- for (int z=0; z < 2000; z++) { //4000 is about 10 seconds
- switch(j)
- {
- case 0: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 0, 255, 255); break; //should fade in red
- case 1: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 96, 255, 255); break; //should fade in green
- case 2: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 160, 255, 255); break; //should fade in blue
- }
- FastLED.show(); // Tells FastLED to display the LEDS
- //delay(3); // Determines the speed of the fade
- }
- // FADING OUT
- for(int k = 255; k >= 0; k--) { // This is DEcreasing k from 255 to 0
- switch(j)
- {
- case 0: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 0, 255, k); break; //should fade out red
- case 1: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 96, 255, k); break; //should fade out green
- case 2: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
- leds[i] = CHSV( 160, 255, k); break; //should fade out blue
- }
- FastLED.show(); // Tells FastLED to display the LEDS
- delay(15); // Determines the speed of the fade
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment