prouton

Snowflake Master ver 1.0

Dec 16th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.75 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define NUM_STRIPS 2
  4. #define NUM_SPOKES 6
  5. #define NUM_LEDS_PER_STRIPE 15
  6. #define NUM_LEDS_PER_SPOKE 25
  7. #define TTL_LEDS NUM_SPOKES * NUM_LEDS_PER_SPOKE
  8. // #define TTL_LEDS 50
  9. CRGB leds[NUM_SPOKES][NUM_LEDS_PER_SPOKE];
  10.  
  11. #define NUM_LEDS_MAIN 15
  12. #define ARM1_OFFSET 6
  13. #define ARM1_LENGTH 2
  14. #define ARM2_OFFSET 12
  15. #define ARM2_LENGTH 3
  16.  
  17. // Create array values for lighting arm spots identically to stripe spots.
  18. // If arms are made longer, additional lines must be coded to fill them.
  19.  
  20. unsigned char armLedEquivs[5][3] = {{ARM1_OFFSET, NUM_LEDS_MAIN + ARM2_LENGTH * 2, NUM_LEDS_MAIN + ARM2_LENGTH * 2 + ARM1_LENGTH},
  21. {ARM1_OFFSET + 1, NUM_LEDS_MAIN + ARM2_LENGTH * 2 + 1, NUM_LEDS_MAIN + ARM2_LENGTH * 2 + ARM1_LENGTH + 1},
  22. {ARM2_OFFSET, NUM_LEDS_MAIN, NUM_LEDS_MAIN + ARM2_LENGTH},
  23. {ARM2_OFFSET + 1, NUM_LEDS_MAIN + 1, NUM_LEDS_MAIN + ARM2_LENGTH + 1},
  24. {ARM2_OFFSET + 2, NUM_LEDS_MAIN + 2, NUM_LEDS_MAIN + ARM2_LENGTH + 2}};
  25.  
  26. #define SWT_BRIGHTNESS 220
  27. #define SWT_DENSITY 220
  28. #define RM_BRIGHTNESS 200
  29.  
  30.  
  31. unsigned long startTime;
  32. unsigned long endTime;
  33. uint8_t lastSelection = 255; // last effect used
  34. uint8_t currSelection = 16; // next effect to use
  35.  
  36. CRGBPalette16 currentPalette;
  37. // TBlendType currentBlending;
  38. // currentBlending = BLEND;
  39. CRGB red = CHSV( HUE_RED, 255, 255 );
  40. CRGB green = CHSV( HUE_GREEN, 255, 255 );
  41. CRGB blue = CHSV( HUE_BLUE, 255, 255 );
  42. CRGB yellow = CHSV( HUE_YELLOW, 255, 255 );
  43. CRGB purple = CHSV( HUE_PURPLE, 255, 255 );
  44. CRGB colorArray[] = {red, green, blue, yellow, purple, CRGB::White};
  45.  
  46.  
  47.  
  48. void setup() {
  49. FastLED.addLeds<WS2812B, 12, GRB>(leds[0], NUM_LEDS_PER_SPOKE * NUM_SPOKES).setCorrection( TypicalLEDStrip );
  50. set_max_power_in_volts_and_milliamps(5, 11000); // FastLED 2.1 Power management set at 5V, 11A
  51.  
  52. BlankTheSnowflake();
  53.  
  54. }
  55.  
  56.  
  57. void loop() {
  58.  
  59. while( currSelection != lastSelection ) {
  60. lastSelection = currSelection;
  61. switch( currSelection ) {
  62. case 0:
  63. RotateThroughColorWheel( 60, 48, 200 );
  64. break;
  65. case 1:
  66. RainbowMarquee( 30, 0, 64, 20, 1, 40 ); // pulsing primary spots
  67. break;
  68. case 2:
  69. GrowAndCycle( 30, 3 );
  70. break;
  71. case 3:
  72. GrowFadeCycle( 30, 5 );
  73. break;
  74. case 4:
  75. RainbowMarquee( 30, 0, 20, 10, 0, 40 ); // smooth tie-dye effect
  76. break;
  77. case 5:
  78. SoftWarmTwinkles( 60 );
  79. break;
  80. case 6:
  81. PopFade( 15 ); // multi-color fast blink spots
  82. break;
  83. case 7:
  84. Sparkle( 15, CRGB::White, 5, 6 ); // all white, dense fast blink spots
  85. break;
  86. case 8:
  87. Sparkle( 15, CRGB::Blue, 3, 4 ); // all blue, medium density fast blink spots
  88. break;
  89. case 9:
  90. Sparkle( 15, CRGB::Red, 3, 4 ); // all red, medium density fast blink spots
  91. break;
  92. case 10:
  93. Sparkle( 15, CRGB::Green, 3, 4 ); // all green, medium density fast blink spots
  94. break;
  95. case 11:
  96. Sparkle( 15, CRGB::Purple, 3, 4 ); // all purple, medium density fast blink spots
  97. break;
  98. case 12:
  99. RedGreenRandom( 30 );
  100. break;
  101. case 13:
  102. MultiMeteor( 30, false );
  103. break;
  104. case 14:
  105. MultiMeteor( 30, true ); // true = green background with only red meteors
  106. break;
  107. case 15:
  108. SpiralOut( 60 );
  109. break;
  110. case 16:
  111. SpiralIn( 45 );
  112. break;
  113. }
  114. FadeTheSnowflake();
  115. BlankTheSnowflake();
  116. FastLED.setBrightness( 255 );
  117. }
  118. currSelection = random8( 17 );
  119. }
  120.  
  121. void SpiralIn(unsigned long duration) {
  122.  
  123. CRGB holdStripe[NUM_LEDS_PER_SPOKE];
  124.  
  125. for( int y = 0; y < NUM_LEDS_PER_STRIPE; y++ ) {
  126. for( int x = 0; x < NUM_SPOKES; x++ ) {
  127. leds[x][y] = colorArray[(x + y)%6];
  128. }
  129. }
  130. for( int x = 0; x < NUM_SPOKES; x++ ) {
  131. setArmsToStripe( x );
  132. }
  133.  
  134. startTime = millis();
  135. endTime = (duration * 1000) + startTime;
  136.  
  137. while( millis() < endTime ) {
  138. memcpy8( &holdStripe[0], &leds[0][0], NUM_LEDS_PER_SPOKE * 3);
  139. for( int i = 1; i < NUM_SPOKES; i++ ) {
  140. memcpy8( &leds[i - 1][0], &leds[i][0], NUM_LEDS_PER_SPOKE * 3);
  141. }
  142. memcpy8( &leds[NUM_SPOKES - 1][0], &holdStripe[0], NUM_LEDS_PER_SPOKE * 3 );
  143.  
  144. FastLED.show();
  145. delay(100);
  146. }
  147. }
  148.  
  149. void SpiralOut(unsigned long duration) {
  150.  
  151. uint8_t cycle = 0;
  152. CRGB holdStripe[NUM_LEDS_PER_STRIPE];
  153. currentPalette = RainbowColors_p;
  154.  
  155. startTime = millis();
  156. endTime = (duration * 1000) + startTime;
  157.  
  158. while( millis() < endTime ) {
  159. memcpy8( &holdStripe[0], &leds[0][0], NUM_LEDS_PER_STRIPE * 3);
  160. for( int i = 1; i < NUM_SPOKES; i++ ) {
  161. memcpy8( &leds[i - 1][0], &leds[i][0], NUM_LEDS_PER_STRIPE * 3);
  162. }
  163. memcpy8( &leds[NUM_SPOKES - 1][1], &holdStripe[0], (NUM_LEDS_PER_STRIPE - 1) * 3 );
  164. leds[NUM_SPOKES - 1][0] = ColorFromPalette( currentPalette, cycle, 220, NOBLEND);
  165.  
  166. FastLED.show();
  167. delay(10);
  168. cycle++;
  169. }
  170. }
  171.  
  172. void MultiMeteor(unsigned long duration, boolean doRedGreen) {
  173.  
  174. uint8_t hue[] = {32, 128, 224, 64, 160, 192};
  175. int idex[] = {255, -29, -10, -17, -3, -21};
  176.  
  177. startTime = millis();
  178. endTime = (duration * 1000) + startTime;
  179.  
  180. while( millis() < endTime ) {
  181. for( int i = 0; i < NUM_SPOKES; i++ ) {
  182. // slide all the pixels in the stripe down one
  183. memmove8( &leds[i][1], &leds[i][0], (NUM_LEDS_PER_STRIPE - 1) * 3 );
  184.  
  185. // increment the meteor display frame
  186. idex[i]++;
  187. // make sure we don't drift into space
  188. if( idex[i] > 29 ) {
  189. idex[i] = 0;
  190. // cycle through hues in each successive meteor tail
  191. hue[i] += 32;
  192. }
  193.  
  194. if( doRedGreen ) {
  195. hue[i] = 32;
  196. }
  197.  
  198. if( idex[i] > -1 ) {
  199. // this switch controls the actual meteor animation, i.e., what gets placed in the
  200. // first position and then subsequently gets moved down the strip by the memmove above
  201. switch ( idex[i] ) {
  202. case 0:
  203. leds[i][0] = CRGB(200,200,200);
  204. break;
  205. case 1:
  206. leds[i][0] = CHSV((hue[i] - 20), 255, 210);
  207. break;
  208. case 2:
  209. leds[i][0] = CHSV((hue[i] - 22), 255, 180);
  210. break;
  211. case 3:
  212. leds[i][0] = CHSV((hue[i] - 23), 255, 150);
  213. break;
  214. case 4:
  215. leds[i][0] = CHSV((hue[i] - 24), 255, 110);
  216. break;
  217. case 5:
  218. leds[i][0] = CHSV((hue[i] - 25), 255, 90);
  219. break;
  220. case 6:
  221. leds[i][0] = CHSV((hue[i] - 26), 160, 60);
  222. break;
  223. case 7:
  224. leds[i][0] = CHSV((hue[i] - 27), 140, 40);
  225. break;
  226. case 8:
  227. leds[i][0] = CHSV((hue[i] - 28), 120, 20);
  228. break;
  229. case 9:
  230. leds[i][0] = CHSV((hue[i] - 29), 100, 20);
  231. break;
  232. default:
  233. if( doRedGreen ) {
  234. leds[i][0] = green;
  235. } else {
  236. leds[i][0] = CRGB::Black;
  237. }
  238. }
  239. }
  240. setArmsToStripe( i );
  241. }
  242.  
  243. // show the blinky
  244. FastLED.show();
  245. // control the animation speed/frame rate
  246. delay(30);
  247. }
  248. }
  249.  
  250. void RedGreenRandom(unsigned long duration) {
  251.  
  252. const CRGB colors[] = { red, green, CRGB::Black };
  253.  
  254. startTime = millis();
  255. endTime = (duration * 1000) + startTime;
  256.  
  257. while( millis() < endTime ) {
  258. for( int i = 0; i < 3; i++ ) {
  259. *((CRGB *)leds + random8( TTL_LEDS )) = colors[i];
  260. }
  261.  
  262. FastLED.show();
  263. delay( 30 );
  264. }
  265. }
  266.  
  267. void Sparkle(unsigned long duration, CRGB color, uint8_t turnOn, uint8_t turnOff) {
  268.  
  269. startTime = millis();
  270. endTime = (duration * 1000) + startTime;
  271.  
  272. while( millis() < endTime ) {
  273. for( int i = 0; i < turnOn; i++ ) {
  274. *((CRGB *)leds + random8( TTL_LEDS )) = color;
  275. }
  276.  
  277. FastLED.show();
  278.  
  279. for( int i = 0; i < turnOff; i++ ) {
  280. *((CRGB *)leds + random8( TTL_LEDS )) = CRGB::Black;
  281. }
  282. }
  283. }
  284.  
  285. void RainbowMarquee(unsigned long duration, uint8_t thishue, uint8_t deltahue,
  286. int8_t thisrot, bool thisdir, uint8_t thisdelay) {
  287. // adapted from code by Andrew Tuline
  288. // Oct, 2014
  289.  
  290. FastLED.setBrightness( RM_BRIGHTNESS );
  291.  
  292. startTime = millis();
  293. endTime = (duration * 1000) + startTime;
  294.  
  295. while( millis() < endTime ) {
  296. if ( thisdir == 0 ) thishue += thisrot; else thishue-= thisrot; // I could use signed math, but 'thisdir' works with other routines.
  297. fill_rainbow( &leds[0][0], TTL_LEDS, thishue, deltahue ); // I don't change deltahue on the fly as it's too fast near the end of the strip.
  298. show_at_max_brightness_for_power(); // Power managed display of LED's.
  299. delay_at_max_brightness_for_power( 2.5*thisdelay ); // Power managed FastLED delay.
  300. }
  301. }
  302.  
  303. void SoftWarmTwinkles(unsigned long duration) {
  304. // By: Mark Kriegsman (modified)
  305. // www.fastled.io
  306. // Date: December 2014
  307.  
  308. // For this animation to work, the Red component of this
  309. // color MUST be nonzero, AND must be an EVEN number.
  310. const CRGB lightcolor(8,7,1);
  311. BlankTheSnowflake();
  312. FastLED.setBrightness(SWT_BRIGHTNESS);
  313.  
  314. uint8_t spoke;
  315. uint8_t pixel;
  316.  
  317. startTime = millis();
  318. endTime = (duration * 1000) + startTime;
  319.  
  320. while( millis() < endTime ) {
  321. for( int y = 0; y < NUM_LEDS_PER_SPOKE; y++) {
  322. for( int x = 0; x < NUM_SPOKES; x++) {
  323. if( !leds[x][y]) continue; // skip black pixels
  324. if( leds[x][y].r & 1) { // is red odd?
  325. leds[x][y] -= lightcolor; // darken if red is odd
  326. } else {
  327. leds[x][y] += lightcolor; // brighten if red is even
  328. }
  329. }
  330. }
  331.  
  332. // Randomly choose a pixel, and if it's black, 'bump' it up a little.
  333. // Since it will now have an EVEN red component, it will start getting
  334. // brighter over time.
  335. for( int a = 0; a < 5; a++) {
  336. if( random8() < SWT_DENSITY) {
  337. int j = random16(TTL_LEDS);
  338. spoke = (j)/NUM_LEDS_PER_SPOKE;
  339. pixel = (j)%NUM_LEDS_PER_SPOKE;
  340. if( !leds[spoke][pixel] ) leds[spoke][pixel] = lightcolor;
  341. }
  342. }
  343.  
  344. FastLED.show();
  345. FastLED.delay(20);
  346. }
  347. }
  348.  
  349. void PopFade(unsigned long duration) {
  350. // By: Andrew Tuline (modified)
  351. // www.tuline.com
  352. // Date: Sep, 2014
  353.  
  354. int ranamount = 150; // The higher the number, lowers the chance for a pixel to light up.
  355. uint8_t thisdelay = 50; // Standard delay value.
  356. bool boolcolours = 0; // True = random, False = array of colours (up to 10)
  357. uint8_t numcolours = 4; // Number of colours in the array to display.
  358. unsigned long colours[10] = {0xff0000, 0x00ff00, 0x0000ff, 0xffffff}; // Just assign the first 3 of these selectable colours.
  359. uint8_t maxbar = 1; // Maximum # of pixels to turn on in a row
  360. uint8_t fadeval = 224; // Fade rate
  361. unsigned long thiscolour;
  362.  
  363. uint8_t spoke;
  364. uint8_t pixel;
  365.  
  366. // thisdelay = 50; colours[0] = 0xffffff; numcolours=1; boolcolours=0; maxbar = 1;
  367.  
  368. startTime = millis();
  369. endTime = (duration * 1000) + startTime;
  370.  
  371. while( millis() < endTime ) {
  372. int idex = random16( 0, ranamount );
  373. if ( idex < TTL_LEDS ) { // Only the lowest probability twinkles will do.
  374. boolcolours ? thiscolour = random(0, 0xffffff) : thiscolour = colours[random16(0, numcolours)];
  375. int barlen = random16( 1,maxbar );
  376. for ( int i = 0; i < barlen; i++ )
  377. if ( idex+i < TTL_LEDS ) {
  378. spoke = (idex+i)/NUM_LEDS_PER_SPOKE;
  379. pixel = (idex+i)%NUM_LEDS_PER_SPOKE;
  380. leds[spoke][pixel] = thiscolour; // Make sure we don't overshoot the array.
  381. }
  382. }
  383. for ( spoke = 0; spoke < NUM_SPOKES; spoke++) {
  384. for ( pixel = 0; pixel < NUM_LEDS_PER_SPOKE; pixel++ ) {
  385. leds[spoke][pixel].nscale8(fadeval); // Go through the array and reduce each RGB value by a percentage.
  386. }
  387. }
  388.  
  389. FastLED.show();
  390. }
  391. }
  392.  
  393. // Starting at the center, fill the leds outward, cycling through up to five colors
  394. void GrowFadeCycle(unsigned long duration, int nbrColors) {
  395.  
  396. uint8_t currColor = 0;
  397.  
  398. startTime = millis();
  399. endTime = (duration * 1000) + startTime;
  400.  
  401. while( millis() < endTime ) {
  402. GrowSimultaneous( colorArray[currColor], 100 );
  403. currColor++;
  404. currColor = currColor%nbrColors;
  405. delay(600);
  406. FadeTheSnowflake();
  407. }
  408. }
  409.  
  410. // Starting at the center, fill the leds outward, cycling through up to five colors
  411. void GrowAndCycle(unsigned long duration, int nbrColors) {
  412.  
  413. uint8_t currColor = 0;
  414.  
  415. startTime = millis();
  416. endTime = (duration * 1000) + startTime;
  417.  
  418. while( millis() < endTime ) {
  419. GrowSimultaneous( colorArray[currColor], 100 );
  420. currColor++;
  421. currColor = currColor%nbrColors;
  422. delay(300);
  423. }
  424. }
  425.  
  426. void GrowSimultaneous(CRGB color, int pause) {
  427.  
  428. // This outer loop will cycle through each led in the spoke
  429. for( int y = 0; y < NUM_LEDS_PER_STRIPE; y++ ) {
  430. // This inner loop will cycle through each spoke
  431. for( int x = 0; x < NUM_SPOKES; x++ ) {
  432. leds[x][y] = color;
  433. setArmsToStripe(x);
  434. }
  435. FastLED.show();
  436. delay(pause);
  437. }
  438. }
  439.  
  440. void UngrowSimultaneous(CRGB color, int pause) {
  441.  
  442. // This outer loop will cycle through each led in the spoke
  443. for( int y = NUM_LEDS_PER_STRIPE - 1; y > - 1; y-- ) {
  444. // This inner loop will cycle through each spoke
  445. for( int x = 0; x < NUM_SPOKES; x++ ) {
  446. leds[x][y] = color;
  447. setArmsToStripe(x);
  448. }
  449. FastLED.show();
  450. delay(pause);
  451. }
  452. }
  453.  
  454. // This function cycles through the color wheel palette.
  455. // "duration" specifies number of seconds to run the routine
  456. // "simul_spokes" is either 16, 32 or 64 in order to keep
  457. // 3, 2 or 1 consecutive spokes the same color
  458. void RotateThroughColorWheel(unsigned long duration, int simul_spokes, int brightness) {
  459.  
  460. CRGB spoke_color[7];
  461.  
  462. SetupColorWheelPalette();
  463.  
  464. uint8_t pri_clr_idx = 0;
  465. startTime = millis();
  466. endTime = (duration * 1000) + startTime;
  467.  
  468. while( millis() < endTime ) {
  469. for( int a = 0; a < NUM_SPOKES; a++ ) {
  470. for( int c = 0; c < 7; c++ ) {
  471. spoke_color[c] = ColorFromPalette( currentPalette, pri_clr_idx + c + (simul_spokes * a), brightness, BLEND);
  472. }
  473. for( int b = 0; b < NUM_LEDS_PER_STRIPE; b++ ) {
  474. leds[a][b] = spoke_color[3];
  475. }
  476. // for( int d = 0; d < 5; d++ ) {
  477. // armLedEquivs
  478. leds[a][17] = spoke_color[0];
  479. leds[a][16] = spoke_color[1];
  480. leds[a][15] = spoke_color[2];
  481. leds[a][18] = spoke_color[4];
  482. leds[a][19] = spoke_color[5];
  483. leds[a][20] = spoke_color[6];
  484. leds[a][22] = spoke_color[1];
  485. leds[a][21] = spoke_color[2];
  486. leds[a][23] = spoke_color[4];
  487. leds[a][24] = spoke_color[5];
  488. // }
  489. }
  490.  
  491. FastLED.show();
  492. pri_clr_idx++;
  493. delay(100);
  494. }
  495. }
  496.  
  497. // This function sets up the colorwheel palette.
  498. void SetupColorWheelPalette() {
  499.  
  500. currentPalette = CRGBPalette16(
  501. red, red, red, blue,
  502. blue, blue, blue, yellow,
  503. yellow, yellow, yellow, green,
  504. green, green, green, red );
  505. }
  506.  
  507. // Turn off all lights in the snowflake immediately
  508. void BlankTheSnowflake() {
  509.  
  510. FastLED.clear();
  511. }
  512.  
  513. // Turn off all lights in the snowflake gradually
  514. void FadeTheSnowflake() {
  515. for( int a = 0; a < 30; a++ ) {
  516. // fadeToBlackBy( &leds[0][0], NUM_LEDS_PER_SPOKE * NUM_SPOKES, 40 );
  517. fadeToBlackBy( &leds[0][0], TTL_LEDS, 40 );
  518. FastLED.show();
  519. delay(40);
  520. }
  521. }
  522.  
  523. // Set the side arm spots to the same value as the stripe spot at that distance
  524. // using the table established above.
  525. void setArmsToStripe(int x) {
  526. for(unsigned char b = 0; b < 5; b++) {
  527. leds[x][armLedEquivs[b][1]] = leds[x][armLedEquivs[b][0]];
  528. leds[x][armLedEquivs[b][2]] = leds[x][armLedEquivs[b][0]];
  529. }
  530. }
Advertisement
Add Comment
Please, Sign In to add comment