allanGEE

FastLED Timed Playlist 2016-11-17

Nov 17th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1. #include "FastLED.h"
  2. // This sketch is based HEAVILY on the Timed Playlist
  3. // by Mark Kriegsman -- a key contributor to the
  4. // FastLED community on Google+
  5. // There are modifications made by me, so if things
  6. // don't run, it's my fault, not Mark's or FastLed.
  7.  
  8. // A "ResetPlaylist" method is provided so that the playlist can be
  9. // restarted from a custom external trigger, e.g., a button or event.
  10. // For demonstration purposes, the playlist is reset if the
  11. // sketch receives a letter "r" on the serial port.
  12. // -Mark Kriegsman, January 2015
  13.  
  14. // The following void list sometimes isn't needed, depending
  15. // on how things compile. I use it just to be safe.
  16. void nextPattern();
  17. void rainbow();
  18. void rainbowWithGlitter();
  19. void addGlitter( fract8 chanceOfGlitter); //NOT void addGlitter();
  20. void confetti();
  21. void sinelon();
  22. void bpm();
  23. void juggle();
  24. void applause();
  25. void fadeToBlack();
  26. void fadingColors();
  27. //
  28. //
  29. //#define DATA_PIN 8
  30. // I skipped the above because lower down I define for
  31. // a bunch of data pins
  32. //#define CLK_PIN 4
  33. //The above isn't needed for my WS2812b's
  34. #define LED_TYPE WS2812B
  35. #define COLOR_ORDER GRB
  36. // Many strip types prefer RGB for the above.
  37. // If your colors aren't displaying properly
  38. // try changing the order
  39. //
  40. //
  41. #define NUM_LEDS 100
  42. // The above notes how many LEDs you're controlling.
  43. // Since I'm using multiple strips of different lengths
  44. // I've set this number for the longest strip.
  45. //
  46. CRGB leds[NUM_LEDS];
  47. // One of these days I'll learn what the above does :)
  48. //
  49. #define FRAMES_PER_SECOND 120
  50. // Not sure what this does at this point
  51. // I'm sure it sets some parameter(s) for an effect(s) below
  52. //
  53. // List of patterns to cycle through. Each is defined as a separate function below.
  54. typedef void (*SimplePattern)();
  55. typedef SimplePattern SimplePatternList[];
  56. typedef struct { SimplePattern mPattern; uint16_t mTime; } PatternAndTime;
  57. typedef PatternAndTime PatternAndTimeList[];
  58. //
  59. // The numbers after the effect name below are times are in seconds,
  60. // but could be changed to milliseconds if desired;
  61. // there's some discussion further below.
  62. //
  63. const PatternAndTimeList gPlaylist = {
  64. { confetti, 300 },
  65. { fadeToBlack, 1 },
  66. { fadingColors, 300 },
  67. { juggle, 300 },
  68. { bpm, 300 },
  69. { rainbowWithGlitter, 300 },
  70. { juggle, 300 },
  71. { applause, 300 },
  72. { fadeToBlack, 3 }
  73. };
  74. //
  75. // If you want the playlist to loop forever, set the
  76. // bool function below to true. If you want the playlist to
  77. // play once, and then stay on the final pattern
  78. // until the playlist is reset, set it to false.
  79. bool gLoopPlaylist = true;
  80. //
  81. void setup() {
  82. //
  83. delay(3000); // 3 second delay for recovery
  84. //
  85. FastLED.setBrightness(64); // Can be 0 to 255
  86. // The above sets the overall brightness for the entire
  87. // sketch. For example, if it's set to 64, that means
  88. // everything later will be one-quarter as bright as it's
  89. // actually shown -- 64/255 = one-quarter. For
  90. // leds[i] = CHSV( 0, 255, 127); the actual brightness
  91. // would be one-quarter of 127, or about 32.
  92. //
  93. // The following lines tell FastLED about the
  94. // multiple strips I have. LED_TYPE, COLOR_ORDER, and NUM_LEDS
  95. // were set above.
  96. //
  97. FastLED.addLeds<LED_TYPE, 8, COLOR_ORDER>(leds, NUM_LEDS);
  98. // the 8 above refers to pin 8
  99. FastLED.addLeds<LED_TYPE, 9, COLOR_ORDER>(leds, NUM_LEDS);
  100. // the 8 above refers to pin 9
  101. FastLED.addLeds<LED_TYPE, 10, COLOR_ORDER>(leds, NUM_LEDS);
  102. // the 8 above refers to pin 10
  103. FastLED.addLeds<LED_TYPE, 11, COLOR_ORDER>(leds, NUM_LEDS);
  104. // the 8 above refers to pin 11
  105. FastLED.addLeds<LED_TYPE, 12, COLOR_ORDER>(leds, NUM_LEDS);
  106. // the 8 above refers to pin 12
  107. //
  108. RestartPlaylist();
  109. //
  110. Serial.begin(57600);
  111. // I believe the above sets up communication back to the
  112. // computer. I think it's only needed if you want to "reset"
  113. }
  114. //
  115. uint8_t gCurrentTrackNumber = 0; // Index number of which pattern is current
  116. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  117. bool gRestartPlaylistFlag = false;
  118. void loop()
  119. {
  120. // Call the current pattern function once, updating the 'leds' array
  121. gPlaylist[gCurrentTrackNumber].mPattern();
  122. // send the 'leds' array out to the actual LED strip
  123. //
  124. FastLED.show(); Displays the LEDs you've called/set above
  125. //
  126. // insert a delay to keep the framerate modest
  127. FastLED.delay(1000/FRAMES_PER_SECOND);
  128. //
  129. // For demo purposes, restart the playlist any time we read
  130. // the letter "r" character from the serial port. Type "r" into
  131. // the Arduino serial monitor and press return to restart the playlist.
  132. // In practice, you could have this check a 'restart' button, or something similar.
  133. if( Serial.read() == 'r') RestartPlaylist();
  134. //
  135. // do some periodic updates
  136. EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  137. //
  138. // Here's where we do two things: switch patterns, and also set the
  139. // 'virtual timer' for how long until the NEXT pattern switch.
  140. //
  141. // Instead of EVERY_N_SECONDS(10) { nextPattern(); }, we use a special
  142. // variation that allows us to get at the pattern timer object itself,
  143. // and change the timer period every time we change the pattern.
  144. //
  145. // You could also do this with EVERY_N_MILLISECONDS_I and have the
  146. // times be expressed in milliseconds instead of seconds.
  147. {
  148. EVERY_N_SECONDS_I(patternTimer,gPlaylist[gCurrentTrackNumber].mTime) {
  149. nextPattern();
  150. patternTimer.setPeriod( gPlaylist[gCurrentTrackNumber].mTime);
  151. }
  152. // Here's where we handle restarting the playlist if the 'reset' flag
  153. // has been set. There are a few steps:
  154. if( gRestartPlaylistFlag ) {
  155. // Set the 'current pattern number' back to zero
  156. gCurrentTrackNumber = 0;
  157. // Set the playback duration for this patter to it's correct time
  158. patternTimer.setPeriod( gPlaylist[gCurrentTrackNumber].mTime);
  159. // Reset the pattern timer so that we start marking time from right now
  160. patternTimer.reset();
  161. // Finally, clear the gRestartPlaylistFlag flag
  162. gRestartPlaylistFlag = false;
  163. }
  164. }
  165. }
  166. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  167. void nextPattern()
  168. {
  169. // add one to the current pattern number
  170. gCurrentTrackNumber = gCurrentTrackNumber + 1;
  171. // If we've come to the end of the playlist, we can either
  172. // automatically restart it at the beginning, or just stay at the end.
  173. if( gCurrentTrackNumber == ARRAY_SIZE( gPlaylist) ) {
  174. if( gLoopPlaylist == true) {
  175. // restart at beginning
  176. gCurrentTrackNumber = 0;
  177. } else {
  178. // stay on the last track
  179. gCurrentTrackNumber--;
  180. }
  181. }
  182. }
  183. void RestartPlaylist()
  184. {
  185. gRestartPlaylistFlag = true;
  186. }
  187. void rainbow()
  188. {
  189. // FastLED's built-in rainbow generator
  190. fill_rainbow( leds, NUM_LEDS, gHue, 7);
  191. }
  192. void rainbowWithGlitter()
  193. {
  194. // built-in FastLED rainbow, plus some random sparkly glitter
  195. rainbow();
  196. addGlitter(80);
  197. }
  198. void addGlitter( fract8 chanceOfGlitter)
  199. {
  200. if( random8() < chanceOfGlitter) {
  201. leds[ random16(NUM_LEDS) ] += CRGB::White;
  202. }
  203. }
  204. void confetti()
  205. {
  206. // random colored speckles that blink in and fade smoothly
  207. fadeToBlackBy( leds, NUM_LEDS, 10);
  208. int pos = random16(NUM_LEDS);
  209. leds[pos] += CHSV( gHue + random8(64), 200, 255);
  210. }
  211. void bpm()
  212. {
  213. // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  214. uint8_t BeatsPerMinute = 62;
  215. CRGBPalette16 palette = PartyColors_p;
  216. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  217. for( int i = 0; i < NUM_LEDS; i++) { //9948
  218. leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  219. }
  220. }
  221. void juggle() {
  222. // eight colored dots, weaving in and out of sync with each other
  223. fadeToBlackBy( leds, NUM_LEDS, 20);
  224. byte dothue = 0;
  225. for( int i = 0; i < 8; i++) {
  226. leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  227. dothue += 32;
  228. }
  229. }
  230. // An animation to play while the crowd goes wild after the big performance
  231. void applause()
  232. {
  233. static uint16_t lastPixel = 0;
  234. fadeToBlackBy( leds, NUM_LEDS, 32);
  235. leds[lastPixel] = CHSV(random8(HUE_BLUE,HUE_PURPLE),255,255);
  236. lastPixel = random16(NUM_LEDS);
  237. leds[lastPixel] = CRGB::White;
  238. }
  239. // An "animation" to just fade to black. Useful as the last track
  240. // in a non-looping performance-oriented playlist.
  241. void fadeToBlack()
  242. {
  243. fadeToBlackBy( leds, NUM_LEDS, 10);
  244. }
  245.  
  246. void fadingColors() {
  247.  
  248. for(int j = 0; j < 3; j++ ) { // This is increasing the case numbers 0 to 2
  249. // FADING IN
  250. for(int k = 0; k < 254; k++) { // This is INcreasing k from 0 to 255
  251. switch(j)
  252. {
  253. case 0: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  254. leds[i] = CHSV( 0, 255, k); break; //should fade in red
  255. case 1: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  256. leds[i] = CHSV( 96, 255, k); break; //should fade in green
  257. case 2: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  258. leds[i] = CHSV( 160, 255, k); break; //should fade in blue
  259. }
  260. FastLED.show(); // Tells FastLED to display the LEDS
  261. delay(15); // Determines the speed of the fade
  262. }
  263. // HOLD COLOR
  264. for (int z=0; z < 2000; z++) { //4000 is about 10 seconds
  265. switch(j)
  266. {
  267. case 0: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  268. leds[i] = CHSV( 0, 255, 255); break; //should fade in red
  269.  
  270. case 1: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  271. leds[i] = CHSV( 96, 255, 255); break; //should fade in green
  272.  
  273. case 2: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  274. leds[i] = CHSV( 160, 255, 255); break; //should fade in blue
  275.  
  276. }
  277. FastLED.show(); // Tells FastLED to display the LEDS
  278. //delay(3); // Determines the speed of the fade
  279. }
  280.  
  281. // FADING OUT
  282. for(int k = 255; k >= 0; k--) { // This is DEcreasing k from 255 to 0
  283. switch(j)
  284. {
  285. case 0: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  286. leds[i] = CHSV( 0, 255, k); break; //should fade out red
  287. case 1: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  288. leds[i] = CHSV( 96, 255, k); break; //should fade out green
  289. case 2: for(int i = 0; i < NUM_LEDS; i++ ) // says how many LEDs to turn on, 0 to NUM_LEDS;
  290. leds[i] = CHSV( 160, 255, k); break; //should fade out blue
  291. }
  292. FastLED.show(); // Tells FastLED to display the LEDS
  293. delay(15); // Determines the speed of the fade
  294. }
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment