Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////
- // .____ ___________________ //
- // | | \_ _____/\______ \ //
- // | | | __)_ | | \ //
- // | |___ | \ | ` \ //
- // |_______ \/_______ //_______ / //
- // \/ \/ \/ //
- // __________ __ __ //
- // \______ \_____ ____ | | _____________ ____ | | __ //
- // | | _/\__ \ _/ ___\| |/ /\____ \__ \ _/ ___\| |/ / //
- // | | \ / __ \\ \___| < | |_> > __ \\ \___| < //
- // |______ /(____ /\___ >__|_ \| __(____ /\___ >__|_ \ //
- // \/ \/ \/ \/|__| \/ \/ \/ //
- /////////////////////////////////////////////////////////////////
- // v1.0 Button >> FASTLED3.1 //
- /////////////////////////////////////////////////////////////////
- #include <FastLED.h> //Include FastLED Library
- FASTLED_USING_NAMESPACE
- //Warn me if FASTLED != 3.1 or later
- #if FASTLED_VERSION < 3001000
- #error "Requires FastLED 3.1 or later; check github for latest code."
- #endif
- //LED SETUP
- #define LED_PIN 11 //Data pin
- #define BUTTON_PIN 13 //Button pin
- #define LED_TYPE WS2811 //LED Type (Note: WS2811 driver used for WS2812B)
- #define COLOR_ORDER RGB //LED Color Order
- #define NUM_LEDS 50 //Number of LEDs
- CRGB leds[NUM_LEDS]; //Name of LED Array
- //#define BRIGHTNESS 96 //Define global brightness
- #define FRAMES_PER_SECOND 120 //Number of frames per second
- //LED Color Palette & Blending
- CRGBPalette16 currentPalette; //Color Palette
- TBlendType currentBlending; //Color Blending
- //BUTTON SETUP
- byte prevKeyState = HIGH;
- //MODE VARIABLES
- int ledMode = 0; //FIRST ACTIVE MODE
- int BRIGHTNESS = 192; //0-255. Lower number saves battery life, higher number is screamingly bright
- int SATURATION = 255; //0 is white (no color) and 255 is fully saturated with color
- int HUE = 0; //0-255, around the color wheel
- int STEPS = 4; //Wider or narrower bands of color
- int SPEEDO = 10; //The speed of the animation
- bool colorORblack=true; // Container for blink function
- unsigned long lastUpdate=0; //container for removing delay();
- //SETUP
- void setup() {
- delay(3000); // Three second power-up safety delay
- pinMode(BUTTON_PIN, INPUT_PULLUP); //Initialize button
- //FASTLED
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- FastLED.setBrightness(BRIGHTNESS);
- currentBlending = LINEARBLEND;
- }
- //uint8_t ledMode = 0; // Index number of which pattern is current
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- ///////////////////////////////////////////////
- // __ __ _ _ //
- // | \/ |__ _(_)_ _ | | ___ ___ _ __ //
- // | |\/| / _` | | ' \ | |__/ _ \/ _ \ '_ \ //
- // |_| |_\__,_|_|_||_| |____\___/\___/ .__/ //
- // |_| //
- ///////////////////////////////////////////////
- #define NUM_MODES 12 //Max number of effects
- void loop() {
- switch (ledMode) {
- case 0: BRIGHTNESS=192; rainbow(); break; //Rainbow Colors
- case 1: BRIGHTNESS=192; RainbowStripe(); break; //Rainbow Stripe
- case 2: BRIGHTNESS=240; rainbowWithGlitter(); break; //Rainbow with Glitter
- case 3: BRIGHTNESS=240; confetti(); break; //Confetti
- case 4: BRIGHTNESS=240; sinelon(); break; //Colored dot
- case 5: BRIGHTNESS=192; bpm(); break; //BPM
- case 6: BRIGHTNESS=192; juggle(); break; //Eight colored dots
- case 7: BRIGHTNESS=128; PartyColors(); break; //Party Colors
- case 8: BRIGHTNESS=128; HeatColors(); break; //Heat Colors
- case 9: BRIGHTNESS=128; Ocean(); break; //Ocean Colors
- case 10: BRIGHTNESS=128; Forest(); break; //Forrest Colors
- case 11: BRIGHTNESS=128; Cloud(); break; //Cloud Colors
- case 12: BRIGHTNESS=240; blink(CRGB::Orange, CRGB::Black, 1000); break; //Blink Caution
- }
- //BUTTON MANAGEMENT
- byte currKeyState = digitalRead(BUTTON_PIN);
- if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
- keyRelease();
- }
- prevKeyState = currKeyState;
- // send the 'leds' array out to the actual LED strip
- FastLED.show();
- // insert a delay to keep the framerate modest
- FastLED.delay(1000/FRAMES_PER_SECOND);
- // do some periodic updates
- EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- }
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
- //////////////////////////////////////////////////////////////
- // _ _ _ _ ___ _ _ //
- // | | (_)__ _| |_| |_ | _ \__ _| |_| |_ ___ _ _ _ _ ___ //
- // | |__| / _` | ' \ _| | _/ _` | _| _/ -_) '_| ' \(_-< //
- // |____|_\__, |_||_\__| |_| \__,_|\__|\__\___|_| |_||_/__/ //
- // |___/ //
- //////////////////////////////////////////////////////////////
- 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 sinelon() {
- // a colored dot sweeping back and forth, with fading trails
- fadeToBlackBy( leds, NUM_LEDS, 20);
- int pos = beatsin16(13,0,NUM_LEDS);
- leds[pos] += CHSV( gHue, 255, 192);
- }
- void bpm() {
- // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
- uint8_t BeatsPerMinute = 64;
- 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;
- }
- }
- void RainbowStripe() {
- //Rainbow Stripe effect
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = RainbowStripeColors_p;
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- FillLEDsFromPaletteColors( startIndex);
- FastLED.show();
- FastLED.delay(SPEEDO);
- }
- void PartyColors() {
- //Party colors
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = PartyColors_p;
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- FillLEDsFromPaletteColors( startIndex);
- FastLED.show();
- FastLED.delay(SPEEDO);
- }
- void HeatColors() {
- //Heat colors
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = HeatColors_p;
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- FillLEDsFromPaletteColors( startIndex);
- FastLED.show();
- FastLED.delay(SPEEDO);
- }
- void Ocean() {
- //Ocean colors
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = OceanColors_p;
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- FillLEDsFromPaletteColors( startIndex);
- FastLED.show();
- FastLED.delay(SPEEDO);
- }
- void Cloud() {
- //Cloud colors
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = CloudColors_p;
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- FillLEDsFromPaletteColors( startIndex);
- FastLED.show();
- FastLED.delay(SPEEDO);
- }
- void Forest() {
- //Forrest colors
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = ForestColors_p;
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- FillLEDsFromPaletteColors( startIndex);
- FastLED.show();
- FastLED.delay(SPEEDO);
- }
- //Fill colors from palette
- void FillLEDsFromPaletteColors( uint8_t colorIndex) {
- for( int i = 0; i < NUM_LEDS; i++) {
- leds[i] = ColorFromPalette( currentPalette, colorIndex, BRIGHTNESS, currentBlending);
- colorIndex += STEPS;
- }
- }
- //function made to take in 2 colors, and their respective wait times
- void blink(CRGB color1, CRGB color2, long delTime)
- {
- //if colorORblack==true, update the array with that color and show it
- if(colorORblack)
- {
- fill_solid(leds,NUM_LEDS, color1);
- FastLED.show();
- }
- //else, if it's not, update it with the second color and show
- else
- {
- fill_solid(leds,NUM_LEDS,color2);
- FastLED.show();
- }
- //blink without delay example
- if(millis()-lastUpdate>delTime)
- {
- //switch the current color if we've overrun the timer
- colorORblack=!colorORblack;
- //update the counter
- lastUpdate=millis();
- }
- }
- //////////////////////////////////
- // ___ _ _ //
- // | _ )_ _| |_| |_ ___ _ _ //
- // | _ \ || | _| _/ _ \ ' \ //
- // |___/\_,_|\__|\__\___/_||_| //
- //////////////////////////////////
- void shortKeyPress() {
- Serial.println("short");
- ledMode++;
- if (ledMode > NUM_MODES){
- ledMode=0; }
- }
- // called when key goes from pressed to not pressed
- void keyRelease() {
- Serial.println("key release");
- shortKeyPress();
- }
- //END DAMNIT!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement