Advertisement
Guest User

Arduino WS2811 with USB Battery Pack

a guest
Jul 14th, 2015
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.70 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////
  2. // .____     ___________________                               //
  3. // |    |    \_   _____/\______ \                              //
  4. // |    |     |    __)_  |    |  \                             //
  5. // |    |___  |        \ |    `   \                            //
  6. // |_______ \/_______  //_______  /                            //
  7. //         \/        \/         \/                             //
  8. // __________                __                         __     //
  9. // \______   \_____    ____ |  | _____________    ____ |  | __ //
  10. //  |    |  _/\__  \ _/ ___\|  |/ /\____ \__  \ _/ ___\|  |/ / //
  11. //  |    |   \ / __ \\  \___|    < |  |_> > __ \\  \___|    <  //
  12. //  |______  /(____  /\___  >__|_ \|   __(____  /\___  >__|_ \ //
  13. //         \/      \/     \/     \/|__|       \/     \/     \/ //
  14. /////////////////////////////////////////////////////////////////
  15. // v1.0 Button >> FASTLED3.1                        //
  16. /////////////////////////////////////////////////////////////////
  17. #include <FastLED.h> //Include FastLED Library
  18.  
  19. FASTLED_USING_NAMESPACE
  20.  
  21. //Warn me if FASTLED != 3.1 or later
  22. #if FASTLED_VERSION < 3001000
  23. #error "Requires FastLED 3.1 or later; check github for latest code."
  24. #endif
  25.  
  26. //LED SETUP
  27. #define LED_PIN 11 //Data pin
  28. #define BUTTON_PIN 13 //Button pin
  29. #define LED_TYPE    WS2811 //LED Type (Note: WS2811 driver used for WS2812B)
  30. #define COLOR_ORDER RGB //LED Color Order
  31. #define NUM_LEDS 50 //Number of LEDs
  32. CRGB leds[NUM_LEDS]; //Name of LED Array
  33.  
  34. //#define BRIGHTNESS          96 //Define global brightness
  35. #define FRAMES_PER_SECOND  120 //Number of frames per second
  36.  
  37. //LED Color Palette & Blending
  38. CRGBPalette16 currentPalette; //Color Palette
  39. TBlendType    currentBlending; //Color Blending
  40.  
  41. //BUTTON SETUP
  42. byte prevKeyState = HIGH;        
  43.  
  44. //MODE VARIABLES
  45. int ledMode = 0; //FIRST ACTIVE MODE
  46. int BRIGHTNESS = 192; //0-255.  Lower number saves battery life, higher number is screamingly bright
  47. int SATURATION = 255; //0 is white (no color) and 255 is fully saturated with color
  48. int HUE = 0; //0-255, around the color wheel
  49. int STEPS = 4; //Wider or narrower bands of color
  50. int SPEEDO = 10; //The speed of the animation
  51.  
  52. bool colorORblack=true; // Container for blink function
  53. unsigned long lastUpdate=0; //container for removing delay();
  54.  
  55. //SETUP
  56. void setup() {
  57.   delay(3000); // Three second power-up safety delay
  58.   pinMode(BUTTON_PIN, INPUT_PULLUP); //Initialize button
  59.  
  60.   //FASTLED
  61.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  62.   FastLED.setBrightness(BRIGHTNESS);
  63.   currentBlending = LINEARBLEND;
  64.   }
  65.  
  66. //uint8_t ledMode = 0; // Index number of which pattern is current
  67. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  68.  
  69. ///////////////////////////////////////////////
  70. //  __  __      _        _                   //
  71. // |  \/  |__ _(_)_ _   | |   ___  ___ _ __  //
  72. // | |\/| / _` | | ' \  | |__/ _ \/ _ \ '_ \ //
  73. // |_|  |_\__,_|_|_||_| |____\___/\___/ .__/ //
  74. //                                    |_|    //
  75. ///////////////////////////////////////////////
  76. #define NUM_MODES 12 //Max number of effects
  77.  
  78. void loop() {
  79.  
  80.   switch (ledMode) {
  81.        case 0: BRIGHTNESS=192; rainbow(); break; //Rainbow Colors
  82.        case 1: BRIGHTNESS=192; RainbowStripe(); break; //Rainbow Stripe
  83.        case 2: BRIGHTNESS=240; rainbowWithGlitter(); break; //Rainbow with Glitter
  84.        case 3: BRIGHTNESS=240; confetti(); break; //Confetti
  85.        case 4: BRIGHTNESS=240; sinelon(); break; //Colored dot
  86.        case 5: BRIGHTNESS=192; bpm(); break; //BPM
  87.        case 6: BRIGHTNESS=192; juggle(); break; //Eight colored dots
  88.        case 7: BRIGHTNESS=128; PartyColors(); break; //Party Colors
  89.        case 8: BRIGHTNESS=128; HeatColors(); break; //Heat Colors
  90.        case 9: BRIGHTNESS=128; Ocean(); break; //Ocean Colors
  91.        case 10: BRIGHTNESS=128; Forest(); break; //Forrest Colors
  92.        case 11: BRIGHTNESS=128; Cloud(); break; //Cloud Colors
  93.        case 12: BRIGHTNESS=240; blink(CRGB::Orange, CRGB::Black, 1000); break; //Blink Caution
  94.     }  
  95.  
  96. //BUTTON MANAGEMENT
  97.   byte currKeyState = digitalRead(BUTTON_PIN);  
  98.     if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
  99.        keyRelease();
  100.   }
  101.  
  102.   prevKeyState = currKeyState;  
  103.  
  104.   // send the 'leds' array out to the actual LED strip
  105.   FastLED.show();  
  106.   // insert a delay to keep the framerate modest
  107.   FastLED.delay(1000/FRAMES_PER_SECOND);
  108.  
  109.   // do some periodic updates
  110.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  111. }
  112.  
  113. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  114.  
  115. //////////////////////////////////////////////////////////////
  116. //  _    _      _   _     ___      _   _                    //
  117. // | |  (_)__ _| |_| |_  | _ \__ _| |_| |_ ___ _ _ _ _  ___ //
  118. // | |__| / _` | ' \  _| |  _/ _` |  _|  _/ -_) '_| ' \(_-< //
  119. // |____|_\__, |_||_\__| |_| \__,_|\__|\__\___|_| |_||_/__/ //
  120. //         |___/                                            //
  121. //////////////////////////////////////////////////////////////
  122. void rainbow() {
  123.   // FastLED's built-in rainbow generator
  124.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  125. }
  126.  
  127. void rainbowWithGlitter() {
  128.   // built-in FastLED rainbow, plus some random sparkly glitter
  129.   rainbow();
  130.   addGlitter(80);
  131. }
  132.  
  133. void addGlitter( fract8 chanceOfGlitter) {
  134.   if( random8() < chanceOfGlitter) {
  135.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  136.   }
  137. }
  138.  
  139. void confetti() {
  140.   // random colored speckles that blink in and fade smoothly
  141.   fadeToBlackBy( leds, NUM_LEDS, 10);
  142.   int pos = random16(NUM_LEDS);
  143.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  144. }
  145.  
  146. void sinelon() {
  147.   // a colored dot sweeping back and forth, with fading trails
  148.   fadeToBlackBy( leds, NUM_LEDS, 20);
  149.   int pos = beatsin16(13,0,NUM_LEDS);
  150.   leds[pos] += CHSV( gHue, 255, 192);
  151. }
  152.  
  153. void bpm() {
  154.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  155.   uint8_t BeatsPerMinute = 64;
  156.   CRGBPalette16 palette = PartyColors_p;
  157.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  158.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  159.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  160.   }
  161. }
  162.  
  163. void juggle() {
  164.   // eight colored dots, weaving in and out of sync with each other
  165.   fadeToBlackBy( leds, NUM_LEDS, 20);
  166.   byte dothue = 0;
  167.   for( int i = 0; i < 8; i++) {
  168.     leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  169.     dothue += 32;
  170.   }
  171. }
  172.  
  173. void RainbowStripe() {
  174.   //Rainbow Stripe effect
  175.   FastLED.setBrightness(  BRIGHTNESS );
  176.   currentPalette = RainbowStripeColors_p;
  177.  
  178.   static uint8_t startIndex = 0;
  179.   startIndex = startIndex + 1;
  180.  
  181.   FillLEDsFromPaletteColors( startIndex);
  182.    
  183.   FastLED.show();
  184.   FastLED.delay(SPEEDO);  
  185. }
  186.  
  187. void PartyColors() {
  188.   //Party colors
  189.   FastLED.setBrightness(  BRIGHTNESS );
  190.   currentPalette = PartyColors_p;
  191.  
  192.   static uint8_t startIndex = 0;
  193.   startIndex = startIndex + 1;
  194.  
  195.   FillLEDsFromPaletteColors( startIndex);
  196.    
  197.   FastLED.show();
  198.   FastLED.delay(SPEEDO);  
  199. }
  200.  
  201. void HeatColors() {
  202.   //Heat colors
  203.   FastLED.setBrightness(  BRIGHTNESS );
  204.   currentPalette = HeatColors_p;
  205.  
  206.   static uint8_t startIndex = 0;
  207.   startIndex = startIndex + 1;
  208.  
  209.   FillLEDsFromPaletteColors( startIndex);
  210.    
  211.   FastLED.show();
  212.   FastLED.delay(SPEEDO);  
  213. }
  214.  
  215. void Ocean() {
  216.   //Ocean colors
  217.   FastLED.setBrightness(  BRIGHTNESS );
  218.   currentPalette = OceanColors_p;
  219.  
  220.   static uint8_t startIndex = 0;
  221.   startIndex = startIndex + 1;
  222.  
  223.   FillLEDsFromPaletteColors( startIndex);
  224.    
  225.   FastLED.show();
  226.   FastLED.delay(SPEEDO);  
  227. }
  228.  
  229. void Cloud() {
  230.   //Cloud colors
  231.   FastLED.setBrightness(  BRIGHTNESS );
  232.   currentPalette = CloudColors_p;
  233.  
  234.   static uint8_t startIndex = 0;
  235.   startIndex = startIndex + 1;
  236.  
  237.   FillLEDsFromPaletteColors( startIndex);
  238.    
  239.   FastLED.show();
  240.   FastLED.delay(SPEEDO);  
  241. }
  242.  
  243. void Forest() {
  244.   //Forrest colors
  245.   FastLED.setBrightness(  BRIGHTNESS );
  246.   currentPalette = ForestColors_p;
  247.  
  248.   static uint8_t startIndex = 0;
  249.   startIndex = startIndex + 1;
  250.  
  251.   FillLEDsFromPaletteColors( startIndex);
  252.    
  253.   FastLED.show();
  254.   FastLED.delay(SPEEDO);  
  255. }
  256.  
  257. //Fill colors from palette
  258. void FillLEDsFromPaletteColors( uint8_t colorIndex) {
  259.   for( int i = 0; i < NUM_LEDS; i++) {
  260.     leds[i] = ColorFromPalette( currentPalette, colorIndex, BRIGHTNESS, currentBlending);
  261.     colorIndex += STEPS;
  262.   }
  263. }
  264.  
  265. //function made to take in 2 colors, and their respective wait times
  266. void blink(CRGB color1, CRGB color2, long delTime)
  267. {
  268.         //if colorORblack==true, update the array with that color and show it
  269.         if(colorORblack)
  270.         {
  271.                 fill_solid(leds,NUM_LEDS, color1);
  272.                 FastLED.show();
  273.         }
  274.         //else, if it's not, update it with the second color and show
  275.         else
  276.         {
  277.                 fill_solid(leds,NUM_LEDS,color2);
  278.                 FastLED.show();
  279.         }
  280.        
  281.         //blink without delay example
  282.         if(millis()-lastUpdate>delTime)
  283.         {
  284.                 //switch the current color if we've overrun the timer
  285.                 colorORblack=!colorORblack;
  286.                 //update the counter
  287.                 lastUpdate=millis();
  288.         }
  289. }
  290.  
  291. //////////////////////////////////
  292. //   ___      _   _             //
  293. //  | _ )_  _| |_| |_ ___ _ _   //
  294. //  | _ \ || |  _|  _/ _ \ ' \  //
  295. //  |___/\_,_|\__|\__\___/_||_| //
  296. //////////////////////////////////
  297. void shortKeyPress() {
  298.     Serial.println("short");
  299.     ledMode++;
  300.     if (ledMode > NUM_MODES){
  301.     ledMode=0; }
  302. }
  303.  
  304. // called when key goes from pressed to not pressed
  305. void keyRelease() {
  306.     Serial.println("key release");
  307.         shortKeyPress();
  308. }
  309. //END DAMNIT!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement