allanGEE

House LED Christmas Sketch 2016-11-24 CURRENT Works

Nov 24th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.97 KB | None | 0 0
  1. #include "FastLED.h"
  2. //
  3. //******************
  4. //*** WORKS ***
  5. //******************
  6. //
  7. // This sketch is based HEAVILY on the Timed Playlist
  8. // by Mark Kriegsman -- a key contributor to the
  9. // FastLED community on Google+
  10. // There are modifications made by me, so if things
  11. // don't run, it's my fault, not Mark's or FastLed.
  12.  
  13. // A "ResetPlaylist" method is provided so that the playlist can be
  14. // restarted from a custom external trigger, e.g., a button or event.
  15. // For demonstration purposes, the playlist is reset if the
  16. // sketch receives a letter "r" on the serial port.
  17. // -Mark Kriegsman, January 2015
  18. //
  19. //*****************************************************************
  20. //*** The following ***
  21. //*** VOID LIST ***
  22. //*** sometimes isn't needed, depending ***
  23. //*** on how things compile. I use it just to be safe. ***
  24. //*****************************************************************
  25. void nextPattern();
  26. void rainbow();
  27. void rainbowWithGlitter();
  28. void addGlitter( fract8 chanceOfGlitter); //NOT void addGlitter();
  29. //void confetti();
  30. void sinelon();
  31. void bpm();
  32. void juggle();
  33. void applause();
  34. void fadeToBlack();
  35. void oneByOneFill();
  36. //
  37. //
  38. //#define DATA_PIN 8
  39. // I skipped the above because lower down I define for
  40. // a bunch of data pins
  41. //
  42. //#define CLK_PIN 4
  43. // The above isn't needed for my WS2812B's
  44. //
  45. #define LED_TYPE WS2812B
  46. // The above states the type of LEDs I want to run
  47. //
  48. #define COLOR_ORDER GRB
  49. // Many strip types prefer RGB for the above.
  50. // If your colors aren't displaying properly
  51. // try changing the order
  52. //
  53. #define NUM_LEDS 300
  54. // The above notes how many LEDs you're controlling.
  55. // Since I'm using multiple strips of different lengths
  56. // I've set this number for the longest strip.
  57. //
  58. CRGB leds[NUM_LEDS];
  59. // One of these days I'll learn what the above does :)
  60. //
  61. #define FRAMES_PER_SECOND 120
  62. // Not sure what this does at this point
  63. // I'm sure it sets some parameter(s) for an effect(s) below
  64. //
  65. // List of patterns to cycle through. Each is defined as a separate function below.
  66. typedef void (*SimplePattern)();
  67. typedef SimplePattern SimplePatternList[];
  68. typedef struct { SimplePattern mPattern; uint16_t mTime; } PatternAndTime;
  69. typedef PatternAndTime PatternAndTimeList[];
  70. //
  71. // ********************
  72. //----------*** THE PLAYLIST ***----------
  73. // ********************
  74. //
  75. // The numbers after the effect name below are times are in seconds,
  76. // but could be changed to milliseconds if desired;
  77. // there's some discussion further below.
  78. //
  79. const PatternAndTimeList gPlaylist = {
  80. // { confetti, 120 },
  81. // I don't care for the above pattern so have commented it out
  82. // to remove it from the playlist until I can modify it.
  83. { juggle, 300 },
  84. { oneByOneFill, 300},
  85. { bpm, 300 },
  86. { rainbowWithGlitter, 300 },
  87. //{ applause, 120 }, // I decided to pull this one from the list as well
  88. { fadeToBlack, 3 } //NOTE, no comma on the last effect
  89. };
  90. //
  91. // If you want the playlist to loop forever, set the
  92. // bool function below to true. If you want the playlist to
  93. // play once, and then stay on the final pattern
  94. // until the playlist is reset, set it to false.
  95. bool gLoopPlaylist = true;
  96. //
  97. //******************
  98. //*** VOID SETUP ***
  99. //******************
  100. //
  101. void setup() {
  102. //
  103. delay(3000);
  104. // Above is a 3 second delay before anything starts
  105. // for recovery purposes
  106. //
  107. FastLED.clear();
  108. // Above turns all the LEDs "off" so there aren't
  109. // any lingering effects from previous sketches.
  110. //
  111. FastLED.setBrightness(127); // Can be 0 to 255
  112. // The above sets the overall brightness for the entire
  113. // sketch. For example, if it's set to 64, that means
  114. // everything later will be one-quarter as bright as it's
  115. // actually shown -- 64/255 = one-quarter. For
  116. // leds[i] = CHSV( 0, 255, 127); the actual brightness
  117. // would be one-quarter of 127, or about 32.
  118. //
  119. // The following lines tell FastLED about the
  120. // multiple strips I have. LED_TYPE, COLOR_ORDER, and NUM_LEDS
  121. // were set above.
  122. //
  123. FastLED.addLeds<LED_TYPE, 8, COLOR_ORDER>(leds, NUM_LEDS);
  124. // the 8 above refers to pin 8
  125. FastLED.addLeds<LED_TYPE, 9, COLOR_ORDER>(leds, NUM_LEDS);
  126. // the 8 above refers to pin 9
  127. FastLED.addLeds<LED_TYPE, 10, COLOR_ORDER>(leds, NUM_LEDS);
  128. // the 8 above refers to pin 10
  129. FastLED.addLeds<LED_TYPE, 11, COLOR_ORDER>(leds, NUM_LEDS);
  130. // the 8 above refers to pin 11
  131. FastLED.addLeds<LED_TYPE, 12, COLOR_ORDER>(leds, NUM_LEDS);
  132. // the 8 above refers to pin 12
  133. //
  134. RestartPlaylist();
  135. //
  136. Serial.begin(57600);
  137. // I believe the above sets up communication back to the
  138. // computer. I think it's only needed if you want to "reset"
  139. }
  140. //
  141. uint8_t gCurrentTrackNumber = 0; // Index number of which pattern is current
  142. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  143. bool gRestartPlaylistFlag = false;
  144. //
  145. //*****************
  146. //*** VOID LOOP ***
  147. //*****************
  148. //
  149. void loop()
  150. {
  151. // Call the current pattern function once, updating the 'leds' array
  152. gPlaylist[gCurrentTrackNumber].mPattern();
  153. // send the 'leds' array out to the actual LED strip
  154. //
  155. FastLED.show(); //Displays the LEDs you've called/set above
  156. //
  157. // insert a delay to keep the framerate modest
  158. // I don't know why the delay includes dividing by the frames instead of just a number.
  159. FastLED.delay(1000/FRAMES_PER_SECOND);
  160.  
  161. //
  162. // For demo purposes, restart the playlist any time we read
  163. // the letter "r" character from the serial port. Type "r" into
  164. // the Arduino serial monitor and press return to restart the playlist.
  165. // In practice, you could have this check a 'restart' button, or something similar.
  166. if( Serial.read() == 'r') RestartPlaylist();
  167. //
  168. // do some periodic updates
  169. EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  170. //
  171. // Here's where we do two things: switch patterns, and also set the
  172. // 'virtual timer' for how long until the NEXT pattern switch.
  173. //
  174. // Instead of EVERY_N_SECONDS(10) { nextPattern(); }, we use a special
  175. // variation that allows us to get at the pattern timer object itself,
  176. // and change the timer period every time we change the pattern.
  177. //
  178. // You could also do this with EVERY_N_MILLISECONDS_I and have the
  179. // times be expressed in milliseconds instead of seconds.
  180. {
  181. EVERY_N_SECONDS_I(patternTimer,gPlaylist[gCurrentTrackNumber].mTime) {
  182. nextPattern();
  183. patternTimer.setPeriod( gPlaylist[gCurrentTrackNumber].mTime);
  184. }
  185. // Here's where we handle restarting the playlist if the 'reset' flag
  186. // has been set. There are a few steps:
  187. if( gRestartPlaylistFlag ) {
  188. // Set the 'current pattern number' back to zero
  189. gCurrentTrackNumber = 0;
  190. // Set the playback duration for this patter to it's correct time
  191. patternTimer.setPeriod( gPlaylist[gCurrentTrackNumber].mTime);
  192. // Reset the pattern timer so that we start marking time from right now
  193. patternTimer.reset();
  194. // Finally, clear the gRestartPlaylistFlag flag
  195. gRestartPlaylistFlag = false;
  196. }
  197. }
  198. }
  199. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  200. void nextPattern()
  201. {
  202. // add one to the current pattern number
  203. gCurrentTrackNumber = gCurrentTrackNumber + 1;
  204. // If we've come to the end of the playlist, we can either
  205. // automatically restart it at the beginning, or just stay at the end.
  206. if( gCurrentTrackNumber == ARRAY_SIZE( gPlaylist) ) {
  207. if( gLoopPlaylist == true) {
  208. // restart at beginning
  209. gCurrentTrackNumber = 0;
  210. } else {
  211. // stay on the last track
  212. gCurrentTrackNumber--;
  213. }
  214. }
  215. }
  216. void RestartPlaylist()
  217. {
  218. gRestartPlaylistFlag = true;
  219. }
  220. //
  221. //**************************************************
  222. //*** BELOW IS WHERE THE INDIVIDUAL EFFECT LOOPS ***
  223. //**** ARE KEPT. THE VOID LOOP REFERS TO THEM *****
  224. //******** WHEN IT'S TIME TO PLAY THEM *************
  225. //**************************************************
  226. //
  227. //***************
  228. //*** RAINBOW ***
  229. //***************
  230. //
  231. void rainbow()
  232. {
  233. // FastLED's built-in rainbow generator
  234. fill_rainbow( leds, NUM_LEDS, gHue, 7);
  235. }
  236. //
  237. //****************************
  238. //*** RAINBOW WITH GLITTER ***
  239. //****************************
  240. //
  241. void rainbowWithGlitter()
  242. {
  243. // built-in FastLED rainbow, plus some random sparkly glitter
  244. rainbow();
  245. addGlitter(80);
  246. }
  247. //
  248. //*******************
  249. //*** ADD GLITTER ***
  250. //*******************
  251. //
  252. void addGlitter( fract8 chanceOfGlitter)
  253. {
  254. if( random8() < chanceOfGlitter) {
  255. leds[ random16(NUM_LEDS) ] += CRGB::White;
  256. }
  257. }
  258. //
  259. //****************
  260. //*** CONFETTI ***
  261. //****************
  262. //
  263. void confetti()
  264. {
  265. // random colored speckles that blink in and fade smoothly
  266. fadeToBlackBy( leds, NUM_LEDS, 10);
  267. int pos = random16(NUM_LEDS);
  268. leds[pos] += CHSV( gHue + random8(64), 200, 255);
  269. }
  270. //
  271. //***************
  272. //*** BPM ***
  273. //***************
  274. //
  275. void bpm()
  276. {
  277. // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  278. uint8_t BeatsPerMinute = 62;
  279. CRGBPalette16 palette = PartyColors_p;
  280. uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  281. for( int i = 0; i < NUM_LEDS; i++) { //9948
  282. leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  283. }
  284. }
  285. //
  286. //***************
  287. //*** JUGGLE ***
  288. //***************
  289. //
  290. void juggle() {
  291. // eight colored dots, weaving in and out of sync with each other
  292. fadeToBlackBy( leds, NUM_LEDS, 20);
  293. byte dothue = 0;
  294. for( int i = 0; i < 8; i++) {
  295. leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  296. dothue += 32;
  297. }
  298. }
  299. //
  300. //****************
  301. //*** APPLAUSE ***
  302. //****************
  303. //
  304. void applause()
  305. {
  306. static uint16_t lastPixel = 0;
  307. fadeToBlackBy( leds, NUM_LEDS, 32);
  308. leds[lastPixel] = CHSV(random8(HUE_BLUE,HUE_PURPLE),255,255);
  309. lastPixel = random16(NUM_LEDS);
  310. leds[lastPixel] = CRGB::White;
  311. }
  312. //
  313. //*********************
  314. //*** FADE TO BLACK ***
  315. //*********************
  316. //
  317. // An "animation" to just fade to black. Useful as the last track
  318. // in a non-looping performance-oriented playlist.
  319. void fadeToBlack()
  320. {
  321. fadeToBlackBy( leds, NUM_LEDS, 10);
  322. }
  323. //
  324. //***********************
  325. //*** ONE BY ONE FILL ***
  326. //***********************
  327. //
  328. void oneByOneFill()
  329. {
  330. //
  331. for(int i = 0; i < NUM_LEDS; i++) {
  332. // The above starts at the first LED, 0, and then counts up one every time it loops
  333. // as long as it's less than the total number of LEDs set in the initial parameters.
  334. leds[i] = CHSV( 0, 255, 255);
  335. // The above sets the current LED to red at full brightness, using Hue Saturation Value
  336. // **BUT** the FastLED.setBrightness(63) set in the void setup
  337. // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
  338. // Hue is the color, Saturation is the intensity, Value is the brightness
  339. // 0 brightness = black
  340. // Another method is below
  341. // leds[i] = CRGB::Red;
  342. FastLED.show(); // tells FastLED to display what you've asked for above
  343.  
  344. // ***** MAKING IT A MOVING DOT *****
  345. // leds[i] = CHSV( 0, 0, 0);
  346. // if you uncomment the above line,it will turn off the current LED
  347. // above, but it won't happen until the next FastLED.show ();
  348. // It's turning itself off as you go along, so you'll only have a dot
  349. // moving down the strip.
  350. // Another method is below
  351. // leds[i] = CRGB::Black;
  352. // By commenting it, the strips will now fill with the color instead of a dot
  353.  
  354. delay(3); // A VERY quick delay before the next step 3/1000 of a second
  355.  
  356. // The way I understand it, because this portion is enclosed with its own
  357. // { and } this portion will keep repeating until "i" reaches the number
  358. // of LEDs we set in the initial parameters. Then it will go to the next
  359. // part between the next pair of { and }. I'm kind of guessing though.
  360.  
  361. }
  362.  
  363. // ***** FILLING THE STRIPS WITH BLUE *****
  364.  
  365. for(int i = 0; i < NUM_LEDS; i++) {
  366. // The above starts at the first LED, 0, and then counts up one every time it loops
  367. // as long as it's less than the total number of LEDs set in the initial parameters.
  368. leds[i] = CHSV( 160, 255, 255);
  369. // The above sets the current LED to blue at full brightness, using Hue Saturation Value
  370. // **BUT** the FastLED.setBrightness(63) set in the void setup
  371. // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
  372. // Hue is the color, Saturation is the intensity, Value is the brightness
  373. // 0 brightness = black
  374. // Another method is below
  375. // leds[i] = CRGB::Blue;
  376. FastLED.show(); // tells FastLED to display what you've asked for above
  377.  
  378. // ***** MAKING IT A MOVING DOT *****
  379. // leds[i] = CHSV( 0, 0, 0);
  380. // if you uncomment the above line,it will turn off the current LED
  381. // above, but it won't happen until the next FastLED.show ();
  382. // It's turning itself off as you go along, so you'll only have a dot
  383. // moving down the strip.
  384. // Another method is below
  385. // leds[i] = CRGB::Black;
  386. // By commenting it, the strips will now fill with the color instead of a dot
  387.  
  388. delay(3); // A VERY quick delay before the next step 3/1000 of a second
  389.  
  390. // The way I understand it, because this portion is enclosed with its own
  391. // { and } this portion will keep repeating until "i" reaches the number
  392. // of LEDs we set in the initial parameters. Then it will go to the next
  393. // part between the next pair of { and }. I'm kind of guessing though.
  394.  
  395. }
  396.  
  397. // ***** FILLING THE STRIPS WITH GREEN *****
  398.  
  399. for(int i = 0; i < NUM_LEDS; i++) {
  400. // The above starts at the first LED, 0, and then counts up one every time it loops
  401. // as long as it's less than the total number of LEDs set in the initial parameters.
  402. leds[i] = CHSV( 96, 255, 255);
  403. // The above sets the current LED to green at full brightness, using Hue Saturation Value
  404. // **BUT** the FastLED.setBrightness(63) set in the void setup
  405. // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
  406. // Hue is the color, Saturation is the intensity, Value is the brightness
  407. // 0 brightness = black max brightness is 255
  408. // Another method is below
  409. // leds[i] = CRGB::Green;
  410. FastLED.show(); // tells FastLED to display what you've asked for above
  411.  
  412. // ***** MAKING IT A MOVING DOT *****
  413. // leds[i] = CHSV( 0, 0, 0);
  414. // if you uncomment the above line,it will turn off the current LED
  415. // above, but it won't happen until the next FastLED.show ();
  416. // It's turning itself off as you go along, so you'll only have a dot
  417. // moving down the strip.
  418. // Another method is below
  419. // leds[i] = CRGB::Black;
  420. // By commenting it, the strips will fill with the color instead of a dot
  421.  
  422. delay(3); // A VERY quick delay before the next step 3/1000 of a second
  423.  
  424. // The way I understand it, because this portion is enclosed with its own
  425. // { and } this portion will keep repeating until "i" reaches the number
  426. // of LEDs we set in the initial parameters. Then it will go to the next
  427. // part between the next pair of { and }. I'm kind of guessing though.
  428.  
  429. }
  430.  
  431. // ***** FILLING THE STRIPS WITH WHITE *****
  432. // NOTE: FULL BRIGHT WHITE USES 60 MILLIAMPS
  433. // MULTIPLIED BY THE NUMBER OF LEDS YOU HAVE LIT!
  434. // MAKE SURE YOUR POWER SUPPLY IS SUFFICIENT
  435. // IF YOU RAISE THE FastLED.setBrightness() SETTING
  436. // MAKE SURE YOU LOWER THE "V" BELOW IF NEEDED
  437.  
  438. for(int i = 0; i < NUM_LEDS; i++) {
  439. // The above starts at the first LED, 0, and then counts up one every time it loops
  440. // as long as it's less than the total number of LEDs set in the initial parameters.
  441. leds[i] = CHSV( 0, 0, 255);
  442. // The above sets the current LED to white at full brightness, using Hue Saturation Value
  443. // **BUT** the FastLED.setBrightness(63) set in the void setup
  444. // makes it display at one-quarter brightness, or leds[i] = CHSV( 0, 255, 63);
  445. // Hue is the color, Saturation is the intensity, Value is the brightness
  446. // For white, I believe you can use any Hue value as long as Saturation is 0
  447. // 0 brightness = black max brightness is 255
  448. // Another method is below
  449. // leds[i] = CRGB::White;
  450. FastLED.show(); // tells FastLED to display what you've asked for above
  451.  
  452. // ***** MAKING IT A MOVING DOT *****
  453. // leds[i] = CHSV( 0, 0, 0);
  454. // if you uncomment the above line,it will turn off the current LED
  455. // above, but it won't happen until the next FastLED.show ();
  456. // It's turning itself off as you go along, so you'll only have a dot
  457. // moving down the strip.
  458. // Another method is below
  459. // leds[i] = CRGB::Black;
  460. // By commenting it, the strips will fill with the color instead of a dot
  461.  
  462. delay(3); // A VERY quick delay before the next step 3/1000 of a second
  463.  
  464. // The way I understand it, because this portion is enclosed with its own
  465. // { and } this portion will keep repeating until "i" reaches the number
  466. // of LEDs we set in the initial parameters. Then it will go to the next
  467. // part between the next pair of { and }. I'm kind of guessing though.
  468.  
  469. }
  470.  
  471.  
  472. } // This designates the end of the void loop so the program
  473. // will now go to the start of loop again.
  474.  
  475. //**************************************
  476. //**************************************
  477. //*** KEEP THIS AT THE END ***
  478. //**************************************
  479. //*** WHEN ADDING A NEW EFFECT LOOP ***
  480. //*** MAKE SURE YOU ALSO INCLUDE IT ***
  481. //*** ON THE VOID LIST AND PLAY LIST ***
  482. //*** NEAR THE START OF THE SKETCH ***
  483. //**************************************
  484. //**************************************
Advertisement
Add Comment
Please, Sign In to add comment