Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Note this is a multi-tab Arduino sketch
- // Main tab
- ///////////////////
- /*
- Anbis Candy Slide 2021 V3
- Based on Halloween Fun and Time Performance by CD77
- 10/25/21:
- - Replaced FireWithPalette with Glitter
- - Made Glitter into its own function that is called from Time_Performance
- - CODE IS DONE!!!
- CD77_Halloween_Fun_2019 by Chemdoc77
- Based by code from different sources.
- See each tab for the sources.
- https://github.com/chemdoc77/CD77_FastLED/tree/master/CD77_Halloween_Fun_2019
- */
- #include <FastLED.h>
- #define LED_PIN A0
- #define CHIPSET WS2812
- #define NUM_LEDS 120 // 60 per strand
- #define BRIGHTNESS 225
- //Button Setup
- #include <JC_Button.h>
- const byte BUTTON_PIN(8); // connect a button switch from this pin to ground
- Button myBtn(BUTTON_PIN); // define the button
- // YX5300 MP3 player
- #include "SerialMP3Player.h"
- #define TX 7
- #define RX A3
- SerialMP3Player mp3(RX,TX);
- //Time Performance code
- uint32_t gTimeCodeBase = 0;
- uint32_t gTimeCode = 0;
- uint32_t gLastTimeCodeDoneAt = 0;
- uint32_t gLastTimeCodeDoneFrom = 0;
- //=================
- // Strand info
- CRGB rawleds[NUM_LEDS];
- CRGBSet leds(rawleds, NUM_LEDS);
- // Real size of arrays
- CRGBSet BoxAndRays(leds(0,59)); // 60 pixels used for box and rays of light.
- CRGBSet Staff(leds(60,105)); // 35 pixels for the staff
- CRGBSet Gap(leds(106,116)); // 10 unused pixels from top of staff to Eye
- CRGBSet Eye(leds(117,119)); // 2-3 pixels for eye
- // Test size of arrays
- //CRGBSet BoxAndRays(leds(0,9)); // 60 pixels used for box and rays of light.
- //CRGBSet Staff(leds(10,19)); // 35 pixels for the staff
- //CRGBSet Gap(leds(20,23)); // 10 unused pixels from top of staff to Eye
- //CRGBSet Eye(leds(24,26)); // 2-3 pixels for eye
- struct CRGB * ledarray[] ={BoxAndRays, Staff, Gap, Eye}; // An array of the CRGBSet arrays
- // Real size of array of arrays
- uint8_t sizearray[]= {60,45,10,3}; // size of the above arrays
- // Test size of array of arrays
- //uint8_t sizearray[]= {10,10,4,3}; // size of the above arrays
- //Fire2012withPalette stuff
- CRGBPalette16 gPal;
- #define FRAMES_PER_SECOND 60
- bool gReverseDirection = false;
- //========================
- #include "Halloween_chase.h"
- #include "Fire2012withPalette.h"
- #include "Time_performance.h"
- void setup() {
- // YX5300 MP3 player setup
- mp3.begin(9600); // start mp3-communication
- delay(500); // wait for init
- mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card
- delay(500); // wait for init
- mp3.setVol(15); // Set volume of playback, 0 - 30.
- delay(500); // sanity delay
- FastLED.addLeds<CHIPSET, LED_PIN, GRB>(leds, NUM_LEDS);
- FastLED.setBrightness( BRIGHTNESS );
- //Fire2012withPalette stuff
- gPal = HeatColors_p;
- FastLED.setMaxPowerInVoltsAndMilliamps(5,7500);
- set_max_power_indicator_LED(13);
- fill_solid(leds, NUM_LEDS, CRGB::Black);
- FastLED.show();
- // JCButton setup
- myBtn.begin(); // initialize the button object
- //Time Performance code
- RestartPerformance();
- }
- //============================================
- void loop() {
- //Time Performance code
- gTimeCode = millis() - gTimeCodeBase;
- Performance1();
- myBtn.read(); // read the button
- if (myBtn.wasReleased()) RestartPerformance();
- }
- // Fire2012withPalette tab
- //////////////
- #ifndef Fire2012withPalette.h
- #define Fire2012withPalette.h
- /*
- The following code is a slight modification of Mark Kriegsman's Fire2012WithPalette that can be found at:
- https://github.com/FastLED/FastLED/blob/master/examples/Fire2012WithPalette/Fire2012WithPalette.ino
- */
- // These are other ways to set up the color palette for the 'fire'.
- // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
- // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
- // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
- // gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
- // Third, here's a simpler, three-step gradient, from black to red to white
- // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
- // COOLING: How much does the air cool as it rises?
- // Less cooling = taller flames. More cooling = shorter flames.
- // Default 55, suggested range 20-100
- //#define COOLING 75
- // SPARKING: What chance (out of 255) is there that a new spark will be lit?
- // Higher chance = more roaring fire. Lower chance = more flickery fire.
- // Default 120, suggested range 50-200.
- //#define SPARKING 80
- void Fire2012WithPalette(uint8_t COOLING, uint8_t SPARKING )
- {
- // Array of temperature readings at each simulation cell
- static byte heat[NUM_LEDS];
- // Step 1. Cool down every cell a little
- for( int i = 0; i < NUM_LEDS; i++) {
- heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
- }
- // Step 2. Heat from each cell drifts 'up' and diffuses a little
- for( int k= NUM_LEDS - 1; k >= 2; k--) {
- heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
- }
- // Step 3. Randomly ignite new 'sparks' of heat near the bottom
- if( random8() < SPARKING ) {
- int y = random8(7);
- heat[y] = qadd8( heat[y], random8(160,255) );
- }
- // Step 4. Map from heat cells to LED colors
- for( int j = 0; j < NUM_LEDS; j++) {
- // Scale the heat value from 0-255 down to 0-240
- // for best results with color palettes.
- byte colorindex = scale8( heat[j], 240);
- CRGB color = ColorFromPalette( gPal, colorindex);
- int pixelnumber;
- if( gReverseDirection ) {
- pixelnumber = (NUM_LEDS-1) - j;
- } else {
- pixelnumber = j;
- }
- leds[pixelnumber] = color;
- }
- }
- void Anubis_Fire2012WithPalette(uint8_t COOLING, uint8_t SPARKING, uint8_t sizearea, uint8_t ledarrayb)
- {
- // Array of temperature readings at each simulation cell
- byte heat[sizearray[sizearea]];
- // Step 1. Cool down every cell a little
- for( int i = 0; i < sizearray[sizearea]; i++) {
- heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / sizearray[sizearea]) + 2));
- }
- // Step 2. Heat from each cell drifts 'up' and diffuses a little
- for( int k= sizearray[sizearea] - 1; k >= 2; k--) {
- heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
- }
- // Step 3. Randomly ignite new 'sparks' of heat near the bottom
- if( random8() < SPARKING ) {
- int y = random8(7);
- heat[y] = qadd8( heat[y], random8(160,255) );
- }
- // Step 4. Map from heat cells to LED colors
- for( int j = 0; j < sizearray[sizearea]; j++) {
- // Scale the heat value from 0-255 down to 0-240
- // for best results with color palettes.
- byte colorindex = scale8( heat[j], 240);
- CRGB color = ColorFromPalette( gPal, colorindex);
- int pixelnumber;
- if( gReverseDirection ) {
- pixelnumber = (sizearray[sizearea]-1) - j;
- } else {
- pixelnumber = j;
- }
- ledarray[ledarrayb] [pixelnumber] = color;
- }
- }
- #endif
- // Halloween Chase tab
- //////////////////
- /* Created by Chemdoc77 based on code by Zaphod Beeblewurdle in:
- https://plus.google.com/107029944060954069417/posts/6r7QakiLLvm
- and based on code by Jason Coon’s Color Pulse in:
- https://plus.google.com/+JasonCoon1/posts/gpN7pEqRQUe
- */
- #ifndef Halloween_chase.h
- #define Halloween_chase.h
- int leds_done = 0;
- uint16_t x=0;
- uint16_t wait = 800;
- uint16_t i=0;
- CRGB Halloween_color = CRGB::Red;
- int m_red; // RGB components of the color
- int m_green;
- int m_blue;
- //======================================
- void CD77_Halloween_Colors (uint16_t wait_cc){
- /* Based on code in This sketch is based on the following sketch from Adafruit:
- *
- https://learn.adafruit.com/random-spooky-led-eyes/assembly?view=all
- Random Eyes sketch for WS2801 pixels
- W. Earl 10/16/11
- For Adafruit Industries
- */
- EVERY_N_MILLIS(wait_cc){
- // Pick a random color - skew toward red/orange/yellow part of the spectrum for extra creepyness
- m_red = random8(150, 255);
- m_blue = 0;
- m_green = random8(100);
- int r = map(m_red, 0, 255, 0, BRIGHTNESS);
- int g = map(m_green, 0, 255, 0, BRIGHTNESS);
- int b = map(m_blue, 0, 255, 0, BRIGHTNESS);
- Halloween_color = CRGB( r, g, b);
- }
- }
- //================================================================
- void CD77_Chase_Halloween_random(uint16_t wait1, uint16_t wait2,uint8_t dots ) {
- //shift pixels
- for(int i = NUM_LEDS - 1; i >0; i--) {
- leds[i] = leds[i-1];
- }
- //reset?
- EVERY_N_MILLIS_I( Dot_time, 500) {
- // This initally defaults to 20 seconds, but then will change the run
- // period to a new random number of seconds from 10 and 30 seconds.
- // You can name "timingObj" whatever you want.
- Dot_time.setPeriod( random16(wait1,wait2) );
- leds_done = 0;
- }
- if(leds_done <dots) {
- leds[0] = Halloween_color;
- leds_done = leds_done + 1;
- //i=i+1; if (i>=3){i=0;}
- } else {
- leds[0] = CRGB::Black;
- }
- }
- //======================
- void CD77_Chase_Halloween_fixed(uint16_t wait1,uint8_t dots ) {
- //shift pixels
- for(int i = NUM_LEDS - 1; i >0; i--) {
- leds[i] = leds[i-1];
- }
- //reset?
- EVERY_N_MILLIS_I( Dot_time, 500) {
- // This initally defaults to 20 seconds, but then will change the run
- // period to a new random number of seconds from 10 and 30 seconds.
- // You can name "timingObj" whatever you want.
- Dot_time.setPeriod(wait1);
- leds_done = 0;
- }
- if(leds_done <dots) {
- leds[0] = Halloween_color;
- leds_done = leds_done + 1;
- //i=i+1; if (i>=3){i=0;}
- } else {
- leds[0] = CRGB::Black;
- }
- }
- //================
- void CD77_Chase_Halloween_fixed_color(CRGB color1, uint16_t wait1,uint8_t dots ) {
- //shift pixels
- for(int i = NUM_LEDS - 1; i >0; i--) {
- leds[i] = leds[i-1];
- }
- //reset?
- EVERY_N_MILLIS_I( Dot_time, 500) {
- // This initally defaults to 20 seconds, but then will change the run
- // period to a new random number of seconds from 10 and 30 seconds.
- // You can name "timingObj" whatever you want.
- Dot_time.setPeriod(wait1);
- leds_done = 0;
- }
- if(leds_done <dots) {
- leds[0] = color1;
- leds_done = leds_done + 1;
- //i=i+1; if (i>=3){i=0;}
- } else {
- leds[0] = CRGB::Black;
- }
- }
- void Anubis_fixed_color(CRGB color1, uint16_t wait1,uint8_t dots, uint8_t sizearea, uint8_t
- ledarrayb) {
- //shift pixels
- for(int i = sizearray[sizearea]- 1; i >0; i--) {
- ledarray[ledarrayb] [i] = ledarray[ledarrayb] [i-1];
- }
- //reset?
- EVERY_N_MILLIS_I( Dot_time, 500) {
- // This initially defaults to 20 seconds, but then will change the run
- // period to a new random number of seconds from 10 and 30 seconds.
- // You can name "timingObj" whatever you want.
- Dot_time.setPeriod(wait1);
- leds_done = 0;
- }
- if(leds_done <dots) {
- ledarray[ledarrayb] [0] = color1;
- leds_done = leds_done + 1;
- //i=i+1; if (i>=3){i=0;}
- }
- else
- {ledarray[ledarrayb] [0] = CRGB::Black;
- }
- }
- //void Glitter (){
- // fadeToBlackBy( ledarray[0], 60, 10); // array you're dimming, number of LEDs in the array, amount you would like to fade them (lower = slower)
- // fract8 chanceOfGlitter = 80;
- //
- // if( random8() < chanceOfGlitter) {
- // leds[ random16(60) ] += CRGB::Gold;} // Glitter colour fixed
- //}
- void GlitterArray (uint8_t ledarrayb, uint8_t sizeareaZ, int fadespeed){
- 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)
- fract8 chanceOfGlitter = 80;
- if( random8() < chanceOfGlitter) {
- ledarray[ledarrayb][ random16(sizearray[sizeareaZ]) ] += CRGB::Gold;} // Glitter colour fixed
- }
- #endif
- // Time Performance tab
- /////////////
- #ifndef Time_performance.h
- #define Time_performance.h
- /* This code is based on the Time Performance sketch by Mark Kriegsman of FastLED at:
- https://gist.github.com/kriegsman/a916be18d32ec675fea8
- */
- #define TC(HOURS,MINUTES,SECONDS) \
- ((uint32_t)(((uint32_t)((HOURS)*(uint32_t)(3600000))) + \
- ((uint32_t)((MINUTES)*(uint32_t)(60000))) + \
- ((uint32_t)((SECONDS)*(uint32_t)(1000)))))
- #define AT(HOURS,MINUTES,SECONDS) if( atTC(TC(HOURS,MINUTES,SECONDS)) )
- #define FROM(HOURS,MINUTES,SECONDS) if( fromTC(TC(HOURS,MINUTES,SECONDS)) )
- static bool atTC( uint32_t tc)
- {
- bool maybe = false;
- if( gTimeCode >= tc) {
- if( gLastTimeCodeDoneAt < tc) {
- maybe = true;
- gLastTimeCodeDoneAt = tc;
- }
- }
- return maybe;
- }
- static bool fromTC( uint32_t tc)
- {
- bool maybe = false;
- if( gTimeCode >= tc) {
- if( gLastTimeCodeDoneFrom <= tc) {
- maybe = true;
- gLastTimeCodeDoneFrom = tc;
- }
- }
- return maybe;
- }
- void RestartPerformance()
- {
- gLastTimeCodeDoneAt = 0;
- gLastTimeCodeDoneFrom = 0;
- gTimeCodeBase = millis();
- }
- //note: add random16_add_entropy( random(0,65535)); and FastLED.delay(1000 / FRAMES_PER_SECOND); for Fire2012 entries
- void Performance1() // Using this for Anubis
- {
- AT(0,0,00.100) { fill_solid( ledarray[3], 3, CRGB::Red); FastLED.show();} // Turn on eye
- AT(0,0,01.400) { mp3.play(001); }
- 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
- FROM(0,0,06.400) { fill_solid( ledarray[1], 40, CRGB::Green); FastLED.show();} // Turn on entire staff
- FROM(0,0,6.500) {FastLED.setBrightness(BRIGHTNESS); GlitterArray(0,0, 10); FastLED.delay(1 / FRAMES_PER_SECOND);}
- FROM(0,0,17.000) {FastLED.clear();FastLED.show();}
- }
- #endif
Add Comment
Please, Sign In to add comment