Advertisement
Guest User

Attempt

a guest
Jul 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. FASTLED_USING_NAMESPACE
  4.  
  5. #define LED_PIN 5
  6. #define NUM_LEDS 94
  7. #define BRIGHTNESS 200
  8. #define LED_TYPE WS2812B
  9. #define COLOR_ORDER GRB
  10. CRGB leds[NUM_LEDS];
  11.  
  12. #define FRAMES_PER_SECOND 100
  13.  
  14. // This example shows several ways to set up and use 'palettes' of colors
  15. // with FastLED.
  16. //
  17. // These compact palettes provide an easy way to re-colorize your
  18. // animation on the fly, quickly, easily, and with low overhead.
  19. //
  20. // USING palettes is MUCH simpler in practice than in theory, so first just
  21. // run this sketch, and watch the pretty lights as you then read through
  22. // the code. Although this sketch has eight (or more) different color schemes,
  23. // the entire sketch compiles down to about 6.5K on AVR.
  24. //
  25. // FastLED provides a few pre-configured color palettes, and makes it
  26. // extremely easy to make up your own color schemes with palettes.
  27. //
  28. // Some notes on the more abstract 'theory and practice' of
  29. // FastLED compact palettes are at the bottom of this file.
  30.  
  31.  
  32.  
  33. CRGBPalette16 currentPalette;
  34. TBlendType currentBlending;
  35.  
  36. extern CRGBPalette16 myRedWhiteBluePalette;
  37. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  38.  
  39.  
  40. void setup() {
  41. delay( 3000 ); // power-up safety delay
  42. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  43. FastLED.setBrightness( BRIGHTNESS );
  44.  
  45. currentPalette = RainbowColors_p;
  46. currentBlending = LINEARBLEND;
  47. }
  48.  
  49.  
  50. // List of patterns to cycle through. Each is defined as a separate function below.
  51. typedef void (*SimplePatternList[])();
  52. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
  53.  
  54. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  55. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  56.  
  57. void loop()
  58. {
  59. gPatterns[gCurrentPatternNumber]();
  60. ChangePalettePeriodically();
  61.  
  62. static uint8_t startIndex = 0;
  63. startIndex = startIndex + 1; /* motion speed */
  64.  
  65. FillLEDsFromPaletteColors( startIndex);
  66.  
  67. FastLED.show();
  68. FastLED.delay(1000 / FRAMES_PER_SECOND);
  69.  
  70. // do some periodic updates
  71. EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  72. EVERY_N_SECONDS( 20 ) { nextPattern(); } // change patterns periodically
  73. }
  74.  
  75. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  76.  
  77. void nextPattern()
  78. {
  79. // add one to the current pattern number, and wrap around at the end
  80. gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  81. }
  82.  
  83. void rainbow()
  84. {
  85. // FastLED's built-in rainbow generator
  86. fill_rainbow( leds, NUM_LEDS, gHue, 5);
  87. }
  88.  
  89. void rainbowWithGlitter()
  90. {
  91. // built-in FastLED rainbow, plus some random sparkly glitter
  92. rainbow();
  93. addGlitter(80);
  94. }
  95.  
  96. void addGlitter( fract8 chanceOfGlitter)
  97. {
  98. if( random8() < chanceOfGlitter) {
  99. leds[ random16(NUM_LEDS) ] += CRGB::White;
  100. }
  101. }
  102.  
  103. void confetti()
  104. {
  105. // random colored speckles that blink in and fade smoothly
  106. fadeToBlackBy( leds, NUM_LEDS, 10);
  107. int pos = random16(NUM_LEDS);
  108. leds[pos] += CHSV( gHue + random8(64), 200, 255);
  109. }
  110.  
  111. void sinelon()
  112. {
  113. // a colored dot sweeping back and forth, with fading trails
  114. fadeToBlackBy( leds, NUM_LEDS, 20);
  115. int pos = beatsin16(13,0,NUM_LEDS);
  116. leds[pos] += CHSV( gHue, 255, 192);
  117. }
  118.  
  119. void bpm()
  120. {
  121. // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  122. uint8_t BeatsPerMinute = 62;
  123. CRGBPalette16 palette = PartyColors_p;
  124. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  125. for( int i = 0; i < NUM_LEDS; i++) { //9948
  126. leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  127. }
  128. }
  129.  
  130. void juggle()
  131. {
  132. // eight colored dots, weaving in and out of sync with each other
  133. fadeToBlackBy( leds, NUM_LEDS, 20);
  134. byte dothue = 0;
  135. for( int i = 0; i < 8; i++) {
  136. leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  137. dothue += 32;
  138. }
  139.  
  140. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  141. {
  142. }
  143. uint8_t brightness = 255;
  144.  
  145. for( int i = 0; i < NUM_LEDS; i++) {
  146. leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  147. colorIndex += 3;
  148. }
  149. }
  150.  
  151.  
  152. // There are several different palettes of colors demonstrated here.
  153. //
  154. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  155. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  156. //
  157. // Additionally, you can manually define your own color palettes, or you can write
  158. // code that creates color palettes on the fly. All are shown here.
  159.  
  160. void ChangePalettePeriodically()
  161. {
  162. uint8_t secondHand = (millis() / 1000) % 60;
  163. static uint8_t lastSecond = 99;
  164.  
  165. if( lastSecond != secondHand) {
  166. lastSecond = secondHand;
  167. if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
  168. if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
  169. if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
  170. if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
  171. if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
  172. if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
  173. if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
  174. if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
  175. if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
  176. if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
  177. if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
  178. }
  179. }
  180.  
  181. // This function fills the palette with totally random colors.
  182. void SetupTotallyRandomPalette()
  183. {
  184. for( int i = 0; i < 16; i++) {
  185. currentPalette[i] = CHSV( random8(), 255, random8());
  186. }
  187. }
  188.  
  189. // This function sets up a palette of black and white stripes,
  190. // using code. Since the palette is effectively an array of
  191. // sixteen CRGB colors, the various fill_* functions can be used
  192. // to set them up.
  193. void SetupBlackAndWhiteStripedPalette()
  194. {
  195. // 'black out' all 16 palette entries...
  196. fill_solid( currentPalette, 16, CRGB::Black);
  197. // and set every fourth one to white.
  198. currentPalette[0] = CRGB::White;
  199. currentPalette[4] = CRGB::White;
  200. currentPalette[8] = CRGB::White;
  201. currentPalette[12] = CRGB::White;
  202.  
  203. }
  204.  
  205. // This function sets up a palette of purple and green stripes.
  206. void SetupPurpleAndGreenPalette()
  207. {
  208. CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  209. CRGB green = CHSV( HUE_GREEN, 255, 255);
  210. CRGB black = CRGB::Black;
  211.  
  212. currentPalette = CRGBPalette16(
  213. green, green, black, black,
  214. purple, purple, black, black,
  215. green, green, black, black,
  216. purple, purple, black, black );
  217. }
  218.  
  219.  
  220. // This example shows how to set up a static color palette
  221. // which is stored in PROGMEM (flash), which is almost always more
  222. // plentiful than RAM. A static PROGMEM palette like this
  223. // takes up 64 bytes of flash.
  224. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  225. {
  226. CRGB::Red,
  227. CRGB::Gray, // 'white' is too bright compared to red and blue
  228. CRGB::Blue,
  229. CRGB::Black,
  230.  
  231. CRGB::Red,
  232. CRGB::Gray,
  233. CRGB::Blue,
  234. CRGB::Black,
  235.  
  236. CRGB::Red,
  237. CRGB::Red,
  238. CRGB::Gray,
  239. CRGB::Gray,
  240. CRGB::Blue,
  241. CRGB::Blue,
  242. CRGB::Black,
  243. CRGB::Black
  244. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement