Advertisement
kartonman

cmprehensive pixel sketch

Oct 17th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 5
  4. #define NUM_LEDS 16
  5. #define BRIGHTNESS 255
  6. #define LED_TYPE WS2812
  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. void setup() {
  38. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  39. FastLED.setBrightness( BRIGHTNESS );
  40.  
  41. currentPalette = RainbowColors_p;
  42. currentBlending = LINEARBLEND;
  43. }
  44.  
  45.  
  46. void loop()
  47. {
  48. ChangePalettePeriodically();
  49.  
  50. static uint8_t startIndex = 0;
  51. startIndex = startIndex + 1; /* motion speed */
  52.  
  53. FillLEDsFromPaletteColors( startIndex);
  54.  
  55. FastLED.show();
  56. FastLED.delay(1000 / UPDATES_PER_SECOND);
  57. }
  58.  
  59. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  60. {
  61. uint8_t brightness = 255;
  62.  
  63. for ( int i = 0; i < NUM_LEDS; i++) {
  64. leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  65. colorIndex += 3;
  66. }
  67. }
  68.  
  69.  
  70. // There are several different palettes of colors demonstrated here.
  71. //
  72. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  73. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  74. //
  75. // Additionally, you can manually define your own color palettes, or you can write
  76. // code that creates color palettes on the fly. All are shown here.
  77.  
  78. void ChangePalettePeriodically()
  79. {
  80. uint8_t secondHand = (millis() / 1000) % 60;
  81. static uint8_t lastSecond = 99;
  82.  
  83. if ( lastSecond != secondHand) {
  84. lastSecond = secondHand;
  85. if ( secondHand == 0) {
  86. currentPalette = RainbowColors_p;
  87. currentBlending = LINEARBLEND;
  88. }
  89. if ( secondHand == 10) {
  90. currentPalette = RainbowStripeColors_p;
  91. currentBlending = NOBLEND;
  92. }
  93. if ( secondHand == 15) {
  94. currentPalette = RainbowStripeColors_p;
  95. currentBlending = LINEARBLEND;
  96. }
  97. if ( secondHand == 20) {
  98. SetupPurpleAndGreenPalette();
  99. currentBlending = LINEARBLEND;
  100. }
  101. if ( secondHand == 25) {
  102. SetupTotallyRandomPalette();
  103. currentBlending = LINEARBLEND;
  104. }
  105. if ( secondHand == 30) {
  106. SetupBlackAndWhiteStripedPalette();
  107. currentBlending = NOBLEND;
  108. }
  109. if ( secondHand == 35) {
  110. SetupBlackAndWhiteStripedPalette();
  111. currentBlending = LINEARBLEND;
  112. }
  113. if ( secondHand == 40) {
  114. currentPalette = CloudColors_p;
  115. currentBlending = LINEARBLEND;
  116. }
  117. if ( secondHand == 45) {
  118. currentPalette = PartyColors_p;
  119. currentBlending = LINEARBLEND;
  120. }
  121. if ( secondHand == 50) {
  122. currentPalette = myRedWhiteBluePalette_p;
  123. currentBlending = NOBLEND;
  124. }
  125. if ( secondHand == 55) {
  126. currentPalette = myRedWhiteBluePalette_p;
  127. currentBlending = LINEARBLEND;
  128. }
  129. }
  130. }
  131.  
  132. // This function fills the palette with totally random colors.
  133. void SetupTotallyRandomPalette()
  134. {
  135. for ( int i = 0; i < 16; i++) {
  136. currentPalette[i] = CHSV( random8(), 255, random8());
  137. }
  138. }
  139.  
  140. // This function sets up a palette of black and white stripes,
  141. // using code. Since the palette is effectively an array of
  142. // sixteen CRGB colors, the various fill_* functions can be used
  143. // to set them up.
  144. void SetupBlackAndWhiteStripedPalette()
  145. {
  146. // 'black out' all 16 palette entries...
  147. fill_solid( currentPalette, 16, CRGB::Black);
  148. // and set every fourth one to white.
  149. currentPalette[0] = CRGB::White;
  150. currentPalette[4] = CRGB::White;
  151. currentPalette[8] = CRGB::White;
  152. currentPalette[12] = CRGB::White;
  153.  
  154. }
  155.  
  156. // This function sets up a palette of purple and green stripes.
  157. void SetupPurpleAndGreenPalette()
  158. {
  159. CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  160. CRGB green = CHSV( HUE_GREEN, 255, 255);
  161. CRGB black = CRGB::Black;
  162.  
  163. currentPalette = CRGBPalette16(
  164. green, green, black, black,
  165. purple, purple, black, black,
  166. green, green, black, black,
  167. purple, purple, black, black );
  168. }
  169.  
  170.  
  171. // This example shows how to set up a static color palette
  172. // which is stored in PROGMEM (flash), which is almost always more
  173. // plentiful than RAM. A static PROGMEM palette like this
  174. // takes up 64 bytes of flash.
  175. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  176. {
  177. CRGB::Red,
  178. CRGB::Gray, // 'white' is too bright compared to red and blue
  179. CRGB::Blue,
  180. CRGB::Black,
  181.  
  182. CRGB::Red,
  183. CRGB::Gray,
  184. CRGB::Blue,
  185. CRGB::Black,
  186.  
  187. CRGB::Red,
  188. CRGB::Red,
  189. CRGB::Gray,
  190. CRGB::Gray,
  191. CRGB::Blue,
  192. CRGB::Blue,
  193. CRGB::Black,
  194. CRGB::Black
  195. };
  196.  
  197.  
  198.  
  199. // Additional notes on FastLED compact palettes:
  200. //
  201. // Normally, in computer graphics, the palette (or "color lookup table")
  202. // has 256 entries, each containing a specific 24-bit RGB color. You can then
  203. // index into the color palette using a simple 8-bit (one byte) value.
  204. // A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
  205. // is quite possibly "too many" bytes.
  206. //
  207. // FastLED does offer traditional 256-element palettes, for setups that
  208. // can afford the 768-byte cost in RAM.
  209. //
  210. // However, FastLED also offers a compact alternative. FastLED offers
  211. // palettes that store 16 distinct entries, but can be accessed AS IF
  212. // they actually have 256 entries; this is accomplished by interpolating
  213. // between the 16 explicit entries to create fifteen intermediate palette
  214. // entries between each pair.
  215. //
  216. // So for example, if you set the first two explicit entries of a compact
  217. // palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
  218. // the first sixteen entries from the virtual palette (of 256), you'd get
  219. // Green, followed by a smooth gradient from green-to-blue, and then Blue.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement