BurningWreck

Anubis Candy Wall 2021

Nov 10th, 2021
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.60 KB | None | 0 0
  1. // Note this is a multi-tab Arduino sketch
  2.  
  3. // Main tab
  4. ///////////////////
  5. /*
  6. Anbis Candy Slide 2021 V3
  7. Based on Halloween Fun and Time Performance by CD77
  8.  
  9. 10/25/21:
  10. - Replaced FireWithPalette with Glitter
  11. - Made Glitter into its own function that is called from Time_Performance
  12. - CODE IS DONE!!!
  13.  
  14. CD77_Halloween_Fun_2019 by Chemdoc77
  15. Based by code from different sources.
  16. See each tab for the sources.
  17. https://github.com/chemdoc77/CD77_FastLED/tree/master/CD77_Halloween_Fun_2019
  18. */
  19.  
  20. #include <FastLED.h>
  21.  
  22. #define LED_PIN A0
  23. #define CHIPSET WS2812
  24. #define NUM_LEDS 120 // 60 per strand
  25. #define BRIGHTNESS 225
  26.  
  27. //Button Setup
  28. #include <JC_Button.h>
  29. const byte BUTTON_PIN(8); // connect a button switch from this pin to ground
  30. Button myBtn(BUTTON_PIN); // define the button
  31.  
  32. // YX5300 MP3 player
  33. #include "SerialMP3Player.h"
  34. #define TX 7
  35. #define RX A3
  36. SerialMP3Player mp3(RX,TX);
  37.  
  38. //Time Performance code
  39.  
  40. uint32_t gTimeCodeBase = 0;
  41. uint32_t gTimeCode = 0;
  42. uint32_t gLastTimeCodeDoneAt = 0;
  43. uint32_t gLastTimeCodeDoneFrom = 0;
  44. //=================
  45.  
  46. // Strand info
  47. CRGB rawleds[NUM_LEDS];
  48. CRGBSet leds(rawleds, NUM_LEDS);
  49. // Real size of arrays
  50. CRGBSet BoxAndRays(leds(0,59)); // 60 pixels used for box and rays of light.
  51. CRGBSet Staff(leds(60,105)); // 35 pixels for the staff
  52. CRGBSet Gap(leds(106,116)); // 10 unused pixels from top of staff to Eye
  53. CRGBSet Eye(leds(117,119)); // 2-3 pixels for eye
  54.  
  55. // Test size of arrays
  56. //CRGBSet BoxAndRays(leds(0,9)); // 60 pixels used for box and rays of light.
  57. //CRGBSet Staff(leds(10,19)); // 35 pixels for the staff
  58. //CRGBSet Gap(leds(20,23)); // 10 unused pixels from top of staff to Eye
  59. //CRGBSet Eye(leds(24,26)); // 2-3 pixels for eye
  60.  
  61. struct CRGB * ledarray[] ={BoxAndRays, Staff, Gap, Eye}; // An array of the CRGBSet arrays
  62.  
  63. // Real size of array of arrays
  64. uint8_t sizearray[]= {60,45,10,3}; // size of the above arrays
  65.  
  66. // Test size of array of arrays
  67. //uint8_t sizearray[]= {10,10,4,3}; // size of the above arrays
  68.  
  69. //Fire2012withPalette stuff
  70. CRGBPalette16 gPal;
  71. #define FRAMES_PER_SECOND 60
  72.  
  73. bool gReverseDirection = false;
  74.  
  75.  
  76. //========================
  77.  
  78. #include "Halloween_chase.h"
  79. #include "Fire2012withPalette.h"
  80. #include "Time_performance.h"
  81.  
  82. void setup() {
  83.  
  84. // YX5300 MP3 player setup
  85. mp3.begin(9600); // start mp3-communication
  86. delay(500); // wait for init
  87. mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card
  88. delay(500); // wait for init
  89. mp3.setVol(15); // Set volume of playback, 0 - 30.
  90.  
  91. delay(500); // sanity delay
  92. FastLED.addLeds<CHIPSET, LED_PIN, GRB>(leds, NUM_LEDS);
  93. FastLED.setBrightness( BRIGHTNESS );
  94.  
  95. //Fire2012withPalette stuff
  96. gPal = HeatColors_p;
  97.  
  98.  
  99. FastLED.setMaxPowerInVoltsAndMilliamps(5,7500);
  100. set_max_power_indicator_LED(13);
  101. fill_solid(leds, NUM_LEDS, CRGB::Black);
  102. FastLED.show();
  103.  
  104. // JCButton setup
  105. myBtn.begin(); // initialize the button object
  106.  
  107. //Time Performance code
  108. RestartPerformance();
  109.  
  110. }
  111.  
  112. //============================================
  113. void loop() {
  114.  
  115. //Time Performance code
  116. gTimeCode = millis() - gTimeCodeBase;
  117. Performance1();
  118.  
  119. myBtn.read(); // read the button
  120. if (myBtn.wasReleased()) RestartPerformance();
  121.  
  122. }
  123.  
  124. // Fire2012withPalette tab
  125. //////////////
  126. #ifndef Fire2012withPalette.h
  127. #define Fire2012withPalette.h
  128.  
  129. /*
  130. The following code is a slight modification of Mark Kriegsman's Fire2012WithPalette that can be found at:
  131. https://github.com/FastLED/FastLED/blob/master/examples/Fire2012WithPalette/Fire2012WithPalette.ino
  132.  
  133. */
  134. // These are other ways to set up the color palette for the 'fire'.
  135. // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
  136. // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
  137.  
  138. // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
  139. // gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
  140.  
  141. // Third, here's a simpler, three-step gradient, from black to red to white
  142. // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
  143.  
  144. // COOLING: How much does the air cool as it rises?
  145. // Less cooling = taller flames. More cooling = shorter flames.
  146. // Default 55, suggested range 20-100
  147. //#define COOLING 75
  148.  
  149. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  150. // Higher chance = more roaring fire. Lower chance = more flickery fire.
  151. // Default 120, suggested range 50-200.
  152. //#define SPARKING 80
  153.  
  154. void Fire2012WithPalette(uint8_t COOLING, uint8_t SPARKING )
  155. {
  156. // Array of temperature readings at each simulation cell
  157. static byte heat[NUM_LEDS];
  158.  
  159. // Step 1. Cool down every cell a little
  160. for( int i = 0; i < NUM_LEDS; i++) {
  161. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  162. }
  163.  
  164. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  165. for( int k= NUM_LEDS - 1; k >= 2; k--) {
  166. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  167. }
  168.  
  169. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  170. if( random8() < SPARKING ) {
  171. int y = random8(7);
  172. heat[y] = qadd8( heat[y], random8(160,255) );
  173. }
  174.  
  175. // Step 4. Map from heat cells to LED colors
  176. for( int j = 0; j < NUM_LEDS; j++) {
  177. // Scale the heat value from 0-255 down to 0-240
  178. // for best results with color palettes.
  179. byte colorindex = scale8( heat[j], 240);
  180. CRGB color = ColorFromPalette( gPal, colorindex);
  181. int pixelnumber;
  182. if( gReverseDirection ) {
  183. pixelnumber = (NUM_LEDS-1) - j;
  184. } else {
  185. pixelnumber = j;
  186. }
  187. leds[pixelnumber] = color;
  188. }
  189. }
  190.  
  191. void Anubis_Fire2012WithPalette(uint8_t COOLING, uint8_t SPARKING, uint8_t sizearea, uint8_t ledarrayb)
  192. {
  193. // Array of temperature readings at each simulation cell
  194. byte heat[sizearray[sizearea]];
  195.  
  196. // Step 1. Cool down every cell a little
  197. for( int i = 0; i < sizearray[sizearea]; i++) {
  198. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / sizearray[sizearea]) + 2));
  199. }
  200.  
  201. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  202. for( int k= sizearray[sizearea] - 1; k >= 2; k--) {
  203. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  204. }
  205.  
  206. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  207. if( random8() < SPARKING ) {
  208. int y = random8(7);
  209. heat[y] = qadd8( heat[y], random8(160,255) );
  210. }
  211.  
  212. // Step 4. Map from heat cells to LED colors
  213. for( int j = 0; j < sizearray[sizearea]; j++) {
  214. // Scale the heat value from 0-255 down to 0-240
  215. // for best results with color palettes.
  216. byte colorindex = scale8( heat[j], 240);
  217. CRGB color = ColorFromPalette( gPal, colorindex);
  218. int pixelnumber;
  219. if( gReverseDirection ) {
  220. pixelnumber = (sizearray[sizearea]-1) - j;
  221. } else {
  222. pixelnumber = j;
  223. }
  224. ledarray[ledarrayb] [pixelnumber] = color;
  225. }
  226. }
  227. #endif
  228.  
  229. // Halloween Chase tab
  230. //////////////////
  231.  
  232. /* Created by Chemdoc77 based on code by Zaphod Beeblewurdle in:
  233. https://plus.google.com/107029944060954069417/posts/6r7QakiLLvm
  234. and based on code by Jason Coon’s Color Pulse in:
  235. https://plus.google.com/+JasonCoon1/posts/gpN7pEqRQUe
  236. */
  237.  
  238.  
  239. #ifndef Halloween_chase.h
  240. #define Halloween_chase.h
  241.  
  242. int leds_done = 0;
  243. uint16_t x=0;
  244. uint16_t wait = 800;
  245. uint16_t i=0;
  246. CRGB Halloween_color = CRGB::Red;
  247.  
  248.  
  249. int m_red; // RGB components of the color
  250. int m_green;
  251. int m_blue;
  252.  
  253.  
  254. //======================================
  255.  
  256. void CD77_Halloween_Colors (uint16_t wait_cc){
  257.  
  258. /* Based on code in This sketch is based on the following sketch from Adafruit:
  259. *
  260. https://learn.adafruit.com/random-spooky-led-eyes/assembly?view=all
  261.  
  262. Random Eyes sketch for WS2801 pixels
  263. W. Earl 10/16/11
  264. For Adafruit Industries
  265. */
  266. EVERY_N_MILLIS(wait_cc){
  267. // Pick a random color - skew toward red/orange/yellow part of the spectrum for extra creepyness
  268. m_red = random8(150, 255);
  269. m_blue = 0;
  270. m_green = random8(100);
  271.  
  272. int r = map(m_red, 0, 255, 0, BRIGHTNESS);
  273. int g = map(m_green, 0, 255, 0, BRIGHTNESS);
  274. int b = map(m_blue, 0, 255, 0, BRIGHTNESS);
  275.  
  276. Halloween_color = CRGB( r, g, b);
  277.  
  278. }
  279. }
  280.  
  281. //================================================================
  282.  
  283. void CD77_Chase_Halloween_random(uint16_t wait1, uint16_t wait2,uint8_t dots ) {
  284. //shift pixels
  285. for(int i = NUM_LEDS - 1; i >0; i--) {
  286. leds[i] = leds[i-1];
  287. }
  288.  
  289. //reset?
  290. EVERY_N_MILLIS_I( Dot_time, 500) {
  291. // This initally defaults to 20 seconds, but then will change the run
  292. // period to a new random number of seconds from 10 and 30 seconds.
  293. // You can name "timingObj" whatever you want.
  294. Dot_time.setPeriod( random16(wait1,wait2) );
  295. leds_done = 0;
  296. }
  297.  
  298. if(leds_done <dots) {
  299.  
  300. leds[0] = Halloween_color;
  301. leds_done = leds_done + 1;
  302. //i=i+1; if (i>=3){i=0;}
  303. } else {
  304. leds[0] = CRGB::Black;
  305. }
  306. }
  307.  
  308.  
  309. //======================
  310.  
  311. void CD77_Chase_Halloween_fixed(uint16_t wait1,uint8_t dots ) {
  312. //shift pixels
  313. for(int i = NUM_LEDS - 1; i >0; i--) {
  314. leds[i] = leds[i-1];
  315. }
  316.  
  317. //reset?
  318. EVERY_N_MILLIS_I( Dot_time, 500) {
  319. // This initally defaults to 20 seconds, but then will change the run
  320. // period to a new random number of seconds from 10 and 30 seconds.
  321. // You can name "timingObj" whatever you want.
  322. Dot_time.setPeriod(wait1);
  323. leds_done = 0;
  324. }
  325.  
  326. if(leds_done <dots) {
  327.  
  328. leds[0] = Halloween_color;
  329. leds_done = leds_done + 1;
  330. //i=i+1; if (i>=3){i=0;}
  331. } else {
  332. leds[0] = CRGB::Black;
  333. }
  334. }
  335.  
  336. //================
  337. void CD77_Chase_Halloween_fixed_color(CRGB color1, uint16_t wait1,uint8_t dots ) {
  338. //shift pixels
  339. for(int i = NUM_LEDS - 1; i >0; i--) {
  340. leds[i] = leds[i-1];
  341. }
  342.  
  343. //reset?
  344. EVERY_N_MILLIS_I( Dot_time, 500) {
  345. // This initally defaults to 20 seconds, but then will change the run
  346. // period to a new random number of seconds from 10 and 30 seconds.
  347. // You can name "timingObj" whatever you want.
  348. Dot_time.setPeriod(wait1);
  349. leds_done = 0;
  350. }
  351.  
  352. if(leds_done <dots) {
  353.  
  354. leds[0] = color1;
  355. leds_done = leds_done + 1;
  356. //i=i+1; if (i>=3){i=0;}
  357. } else {
  358. leds[0] = CRGB::Black;
  359. }
  360. }
  361.  
  362.  
  363. void Anubis_fixed_color(CRGB color1, uint16_t wait1,uint8_t dots, uint8_t sizearea, uint8_t
  364. ledarrayb) {
  365.  
  366. //shift pixels
  367. for(int i = sizearray[sizearea]- 1; i >0; i--) {
  368. ledarray[ledarrayb] [i] = ledarray[ledarrayb] [i-1];
  369. }
  370.  
  371. //reset?
  372. EVERY_N_MILLIS_I( Dot_time, 500) {
  373. // This initially defaults to 20 seconds, but then will change the run
  374. // period to a new random number of seconds from 10 and 30 seconds.
  375. // You can name "timingObj" whatever you want.
  376. Dot_time.setPeriod(wait1);
  377. leds_done = 0;
  378. }
  379.  
  380. if(leds_done <dots) {
  381.  
  382. ledarray[ledarrayb] [0] = color1;
  383. leds_done = leds_done + 1;
  384. //i=i+1; if (i>=3){i=0;}
  385. }
  386. else
  387. {ledarray[ledarrayb] [0] = CRGB::Black;
  388. }
  389. }
  390.  
  391. //void Glitter (){
  392. // fadeToBlackBy( ledarray[0], 60, 10); // array you're dimming, number of LEDs in the array, amount you would like to fade them (lower = slower)
  393. // fract8 chanceOfGlitter = 80;
  394. //
  395. // if( random8() < chanceOfGlitter) {
  396. // leds[ random16(60) ] += CRGB::Gold;} // Glitter colour fixed
  397. //}
  398.  
  399. void GlitterArray (uint8_t ledarrayb, uint8_t sizeareaZ, int fadespeed){
  400. fadeToBlackBy( ledarray[ledarrayb], (sizearray[sizeareaZ]), fadespeed); // array you're dimming, number of LEDs in the array, amount you would like to fade them (lower = slower)
  401. fract8 chanceOfGlitter = 80;
  402.  
  403. if( random8() < chanceOfGlitter) {
  404. ledarray[ledarrayb][ random16(sizearray[sizeareaZ]) ] += CRGB::Gold;} // Glitter colour fixed
  405. }
  406.  
  407. #endif
  408.  
  409. // Time Performance tab
  410. /////////////
  411. #ifndef Time_performance.h
  412. #define Time_performance.h
  413.  
  414. /* This code is based on the Time Performance sketch by Mark Kriegsman of FastLED at:
  415. https://gist.github.com/kriegsman/a916be18d32ec675fea8
  416. */
  417.  
  418. #define TC(HOURS,MINUTES,SECONDS) \
  419. ((uint32_t)(((uint32_t)((HOURS)*(uint32_t)(3600000))) + \
  420. ((uint32_t)((MINUTES)*(uint32_t)(60000))) + \
  421. ((uint32_t)((SECONDS)*(uint32_t)(1000)))))
  422.  
  423.  
  424. #define AT(HOURS,MINUTES,SECONDS) if( atTC(TC(HOURS,MINUTES,SECONDS)) )
  425. #define FROM(HOURS,MINUTES,SECONDS) if( fromTC(TC(HOURS,MINUTES,SECONDS)) )
  426.  
  427. static bool atTC( uint32_t tc)
  428. {
  429. bool maybe = false;
  430. if( gTimeCode >= tc) {
  431. if( gLastTimeCodeDoneAt < tc) {
  432. maybe = true;
  433. gLastTimeCodeDoneAt = tc;
  434. }
  435. }
  436. return maybe;
  437. }
  438.  
  439. static bool fromTC( uint32_t tc)
  440. {
  441. bool maybe = false;
  442. if( gTimeCode >= tc) {
  443. if( gLastTimeCodeDoneFrom <= tc) {
  444. maybe = true;
  445. gLastTimeCodeDoneFrom = tc;
  446. }
  447. }
  448. return maybe;
  449. }
  450.  
  451. void RestartPerformance()
  452. {
  453. gLastTimeCodeDoneAt = 0;
  454. gLastTimeCodeDoneFrom = 0;
  455. gTimeCodeBase = millis();
  456. }
  457.  
  458. //note: add random16_add_entropy( random(0,65535)); and FastLED.delay(1000 / FRAMES_PER_SECOND); for Fire2012 entries
  459.  
  460.  
  461. void Performance1() // Using this for Anubis
  462. {
  463. AT(0,0,00.100) { fill_solid( ledarray[3], 3, CRGB::Red); FastLED.show();} // Turn on eye
  464. AT(0,0,01.400) { mp3.play(001); }
  465. FROM(0,0,01.500) { Anubis_fixed_color(CRGB::Green, 700, 8, 1, 1); FastLED.delay(20);} // In Halloween_chase.h. Conflicts with MP3 player if not enough pixels in strand
  466. FROM(0,0,06.400) { fill_solid( ledarray[1], 40, CRGB::Green); FastLED.show();} // Turn on entire staff
  467. FROM(0,0,6.500) {FastLED.setBrightness(BRIGHTNESS); GlitterArray(0,0, 10); FastLED.delay(1 / FRAMES_PER_SECOND);}
  468. FROM(0,0,17.000) {FastLED.clear();FastLED.show();}
  469. }
  470. #endif
Add Comment
Please, Sign In to add comment