Guest User

ColorPaletteGlitch with Modified FastLED example

a guest
Dec 1st, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 23
  4. #define NUM_LEDS 300
  5. #define BRIGHTNESS 255
  6. #define LED_TYPE WS2811
  7. #define COLOR_ORDER GRB
  8. CRGB leds[NUM_LEDS];
  9.  
  10. #define UPDATES_PER_SECOND 50
  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 myWhitePalette;
  35. extern const TProgmemPalette16 myWhitePalette_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 = CloudColors_p;
  44. currentBlending = NOBLEND;
  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. for( int i = 0; i <= NUM_LEDS/2; i++) {
  70. leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  71. leds[NUM_LEDS-i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  72. colorIndex += 3;
  73.  
  74. }
  75. }
  76.  
  77.  
  78. // There are several different palettes of colors demonstrated here.
  79. //
  80. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  81. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  82. //
  83. // Additionally, you can manually define your own color palettes, or you can write
  84. // code that creates color palettes on the fly. All are shown here.
  85.  
  86. void ChangePalettePeriodically()
  87. {
  88. uint8_t secondHand = (millis() / 1000) % 60;
  89. static uint8_t lastSecond = 99;
  90.  
  91. if( lastSecond != secondHand) {
  92. lastSecond = secondHand;
  93. if( secondHand == 0) { currentPalette = CloudColors_p; currentBlending = BLEND; }
  94. if( secondHand == 20) { SetupRedAndGreenPalette(); currentBlending = NOBLEND; }
  95. if( secondHand == 40) { currentPalette = myWhitePalette_p; currentBlending = BLEND; }
  96. }
  97. }
  98.  
  99. // This function fills the palette with totally random colors.
  100. void SetupTotallyRandomPalette()
  101. {
  102. for( int i = 0; i < 16; i++) {
  103. currentPalette[i] = CHSV( random8(), 255, random8());
  104. }
  105. }
  106.  
  107. // This function sets up a palette of black and white stripes,
  108. // using code. Since the palette is effectively an array of
  109. // sixteen CRGB colors, the various fill_* functions can be used
  110. // to set them up.
  111. void SetupBlackAndWhiteStripedPalette()
  112. {
  113. // 'black out' all 16 palette entries...
  114. fill_solid( currentPalette, 16, CRGB::Black);
  115. // and set every fourth one to white.
  116. currentPalette[0] = CRGB::White;
  117. currentPalette[4] = CRGB::White;
  118. currentPalette[8] = CRGB::White;
  119. currentPalette[12] = CRGB::White;
  120.  
  121. }
  122.  
  123. // This function sets up a palette of purple and green stripes.
  124. void SetupRedAndGreenPalette()
  125. {
  126. CRGB red = CHSV( HUE_RED, 255, 255);
  127. CRGB green = CHSV( HUE_GREEN, 255, 255);
  128. CRGB black = CRGB::Black;
  129.  
  130. currentPalette = CRGBPalette16(
  131. green, green, red, red,
  132. green, green, red, red,
  133. green, green, red, red,
  134. green, green, red, red );
  135. }
  136.  
  137.  
  138. // This example shows how to set up a static color palette
  139. // which is stored in PROGMEM (flash), which is almost always more
  140. // plentiful than RAM. A static PROGMEM palette like this
  141. // takes up 64 bytes of flash.
  142. const TProgmemPalette16 myWhitePalette_p PROGMEM =
  143. {
  144. CRGB::White,
  145. CRGB::White, // 'white' is too bright compared to red and blue
  146. CRGB::White,
  147. CRGB::White,
  148.  
  149. CRGB::Gray,
  150. CRGB::Gray,
  151. CRGB::Gray,
  152. CRGB::Gray,
  153.  
  154. CRGB::White,
  155. CRGB::White, // 'white' is too bright compared to red and blue
  156. CRGB::White,
  157. CRGB::White,
  158.  
  159. CRGB::Gray,
  160. CRGB::Gray,
  161. CRGB::Gray,
  162. CRGB::Gray
  163. };
  164.  
  165.  
  166.  
  167. // Additionl notes on FastLED compact palettes:
  168. //
  169. // Normally, in computer graphics, the palette (or "color lookup table")
  170. // has 256 entries, each containing a specific 24-bit RGB color. You can then
  171. // index into the color palette using a simple 8-bit (one byte) value.
  172. // A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
  173. // is quite possibly "too many" bytes.
  174. //
  175. // FastLED does offer traditional 256-element palettes, for setups that
  176. // can afford the 768-byte cost in RAM.
  177. //
  178. // However, FastLED also offers a compact alternative. FastLED offers
  179. // palettes that store 16 distinct entries, but can be accessed AS IF
  180. // they actually have 256 entries; this is accomplished by interpolating
  181. // between the 16 explicit entries to create fifteen intermediate palette
  182. // entries between each pair.
  183. //
  184. // So for example, if you set the first two explicit entries of a compact
  185. // palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
  186. // the first sixteen entries from the virtual palette (of 256), you'd get
  187. // Green, followed by a smooth gradient from green-to-blue, and then Blue.
Advertisement
Add Comment
Please, Sign In to add comment