Advertisement
liuk90

Untitled

Mar 18th, 2020
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.74 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN     5
  4. #define NUM_LEDS    50
  5. #define BRIGHTNESS  64
  6. #define LED_TYPE    WS2811
  7. #define COLOR_ORDER GRB
  8. CRGB leds[NUM_LEDS];
  9.  
  10. #define UPDATES_PER_SECOND 100
  11.  
  12. // This example shows several ways to set up and use 'palettes' of colors
  13. // with FastLED.
  14. //
  15. // These compact palettes provide an easy way to re-colorize your
  16. // animation on the fly, quickly, easily, and with low overhead.
  17. //
  18. // USING palettes is MUCH simpler in practice than in theory, so first just
  19. // run this sketch, and watch the pretty lights as you then read through
  20. // the code.  Although this sketch has eight (or more) different color schemes,
  21. // the entire sketch compiles down to about 6.5K on AVR.
  22. //
  23. // FastLED provides a few pre-configured color palettes, and makes it
  24. // extremely easy to make up your own color schemes with palettes.
  25. //
  26. // Some notes on the more abstract 'theory and practice' of
  27. // FastLED compact palettes are at the bottom of this file.
  28.  
  29.  
  30.  
  31. CRGBPalette16 currentPalette;
  32. TBlendType    currentBlending;
  33.  
  34. extern CRGBPalette16 myRedWhiteBluePalette;
  35. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  36.  
  37.  
  38. void setup() {
  39.     delay( 3000 ); // power-up safety delay
  40.     FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  41.     FastLED.setBrightness(  BRIGHTNESS );
  42.    
  43.     currentPalette = RainbowColors_p;
  44.     currentBlending = LINEARBLEND;
  45. }
  46.  
  47.  
  48. void loop()
  49. {
  50.     ChangePalettePeriodically();
  51.    
  52.     static uint8_t startIndex = 0;
  53.     startIndex = startIndex + 1; /* motion speed */
  54.    
  55.     FillLEDsFromPaletteColors( startIndex);
  56.    
  57.     FastLED.show();
  58.     FastLED.delay(1000 / UPDATES_PER_SECOND);
  59. }
  60.  
  61. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  62. {
  63.     uint8_t brightness = 255;
  64.    
  65.     for( int i = 0; i < NUM_LEDS; i++) {
  66.         leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  67.         colorIndex += 3;
  68.     }
  69. }
  70.  
  71.  
  72. // There are several different palettes of colors demonstrated here.
  73. //
  74. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  75. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  76. //
  77. // Additionally, you can manually define your own color palettes, or you can write
  78. // code that creates color palettes on the fly.  All are shown here.
  79.  
  80. void ChangePalettePeriodically()
  81. {
  82.     uint8_t secondHand = (millis() / 1000) % 60;
  83.     static uint8_t lastSecond = 99;
  84.    
  85.     if( lastSecond != secondHand) {
  86.         lastSecond = secondHand;
  87.         if( secondHand ==  0)  { currentPalette = RainbowColors_p;         currentBlending = LINEARBLEND; }
  88.         if( secondHand == 10)  { currentPalette = RainbowStripeColors_p;   currentBlending = NOBLEND;  }
  89.         if( secondHand == 15)  { currentPalette = RainbowStripeColors_p;   currentBlending = LINEARBLEND; }
  90.         if( secondHand == 20)  { SetupPurpleAndGreenPalette();             currentBlending = LINEARBLEND; }
  91.         if( secondHand == 25)  { SetupTotallyRandomPalette();              currentBlending = LINEARBLEND; }
  92.         if( secondHand == 30)  { SetupBlackAndWhiteStripedPalette();       currentBlending = NOBLEND; }
  93.         if( secondHand == 35)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
  94.         if( secondHand == 40)  { currentPalette = CloudColors_p;           currentBlending = LINEARBLEND; }
  95.         if( secondHand == 45)  { currentPalette = PartyColors_p;           currentBlending = LINEARBLEND; }
  96.         if( secondHand == 50)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND;  }
  97.         if( secondHand == 55)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
  98.     }
  99. }
  100.  
  101. // This function fills the palette with totally random colors.
  102. void SetupTotallyRandomPalette()
  103. {
  104.     for( int i = 0; i < 16; i++) {
  105.         currentPalette[i] = CHSV( random8(), 255, random8());
  106.     }
  107. }
  108.  
  109. // This function sets up a palette of black and white stripes,
  110. // using code.  Since the palette is effectively an array of
  111. // sixteen CRGB colors, the various fill_* functions can be used
  112. // to set them up.
  113. void SetupBlackAndWhiteStripedPalette()
  114. {
  115.     // 'black out' all 16 palette entries...
  116.     fill_solid( currentPalette, 16, CRGB::Black);
  117.     // and set every fourth one to white.
  118.     currentPalette[0] = CRGB::White;
  119.     currentPalette[4] = CRGB::White;
  120.     currentPalette[8] = CRGB::White;
  121.     currentPalette[12] = CRGB::White;
  122.    
  123. }
  124.  
  125. // This function sets up a palette of purple and green stripes.
  126. void SetupPurpleAndGreenPalette()
  127. {
  128.     CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  129.     CRGB green  = CHSV( HUE_GREEN, 255, 255);
  130.     CRGB black  = CRGB::Black;
  131.    
  132.     currentPalette = CRGBPalette16(
  133.                                    green,  green,  black,  black,
  134.                                    purple, purple, black,  black,
  135.                                    green,  green,  black,  black,
  136.                                    purple, purple, black,  black );
  137. }
  138.  
  139.  
  140. // This example shows how to set up a static color palette
  141. // which is stored in PROGMEM (flash), which is almost always more
  142. // plentiful than RAM.  A static PROGMEM palette like this
  143. // takes up 64 bytes of flash.
  144. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  145. {
  146.     CRGB::Red,
  147.     CRGB::Gray, // 'white' is too bright compared to red and blue
  148.     CRGB::Blue,
  149.     CRGB::Black,
  150.    
  151.     CRGB::Red,
  152.     CRGB::Gray,
  153.     CRGB::Blue,
  154.     CRGB::Black,
  155.    
  156.     CRGB::Red,
  157.     CRGB::Red,
  158.     CRGB::Gray,
  159.     CRGB::Gray,
  160.     CRGB::Blue,
  161.     CRGB::Blue,
  162.     CRGB::Black,
  163.     CRGB::Black
  164. };
  165.  
  166. // Additional notes on FastLED compact palettes:
  167. //
  168. // Normally, in computer graphics, the palette (or "color lookup table")
  169. // has 256 entries, each containing a specific 24-bit RGB color.  You can then
  170. // index into the color palette using a simple 8-bit (one byte) value.
  171. // A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
  172. // is quite possibly "too many" bytes.
  173. //
  174. // FastLED does offer traditional 256-element palettes, for setups that
  175. // can afford the 768-byte cost in RAM.
  176. //
  177. // However, FastLED also offers a compact alternative.  FastLED offers
  178. // palettes that store 16 distinct entries, but can be accessed AS IF
  179. // they actually have 256 entries; this is accomplished by interpolating
  180. // between the 16 explicit entries to create fifteen intermediate palette
  181. // entries between each pair.
  182. //
  183. // So for example, if you set the first two explicit entries of a compact
  184. // palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
  185. // the first sixteen entries from the virtual palette (of 256), you'd get
  186. // Green, followed by a smooth gradient from green-to-blue, and then Blue.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement