Advertisement
GyroGearloose

Untitled

Jan 31st, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. /*
  2.  * Demo Code for FastLED and Fire2012 Animation to make it use separate actions on different pins
  3.  * with different number of LEDs if necessary.
  4.  * original sketch:
  5.  * Created: 3/17/2015 2:57:40 PM
  6.  * Final mods: March 26 2015ish
  7.  * Author: Dave Hrynkiw, Solarbotics Ltd.
  8.  * Special thx to K. Lonney for assistance
  9.  * modified by J. Bruegl, May 2015
  10.  */
  11.  
  12. //* Adding FIRE animation trial
  13. #include <FastLED.h>
  14. #define BRIGHTNESS 64 //200
  15. #define FRAMES_PER_SECOND 40
  16. #define SPARKING 18         // lower values calm fire down
  17. #define COOLING 50
  18.  
  19. //ZONE 1 DEFINITIONS
  20. #define Z1_DATA_PIN 5       // Declare Zone 1 data pin
  21. #define Z1_CLOCK_PIN        // Declare Zone 1 clock pin
  22. #define Z1_NUM_LEDS 44      // Declare Zone 1 number of LEDs
  23. CRGB Z1[Z1_NUM_LEDS];       // Declare Zone 1 FastLED data array
  24. byte Z1_Heat[Z1_NUM_LEDS];  // Delcare Zone 1 Heat map data array
  25.    
  26. //ZONE 2 DEFINITIONS
  27. #define Z2_DATA_PIN 6       // Declare Zone 2 data pin
  28. #define Z2_CLOCK_PIN        // Declare Zone 2 clock pin
  29. #define Z2_NUM_LEDS 44      // Declare Zone 2 number of LEDs
  30. CRGB Z2[Z2_NUM_LEDS];       // Declare Zone 2 FastLED data array
  31. byte Z2_Heat[Z2_NUM_LEDS];  // Delcare Zone 2 Heat map data array
  32.  
  33. CRGBPalette16 gPal;     // Set pallet array
  34.  
  35. void setup()
  36. {        
  37.     delay(2000);    //DON'T IGNORE THIS. Yes, it makes a difference, especially on a Teensy 3.1
  38.     FastLED.addLeds<NEOPIXEL, Z1_DATA_PIN>(Z1, 0, Z1_NUM_LEDS);     //Z1 Initialize the FastLED functions
  39.     FastLED.addLeds<NEOPIXEL, Z2_DATA_PIN>(Z2, 0, Z2_NUM_LEDS);     //Z2 Initialize the FastLED functions
  40. //  FastLED.addLeds<APA102, Z1_DATA_PIN, Z1_CLOCK_PIN>(Z1, 0, Z1_NUM_LEDS);     //Z1 Initialize the FastLED functions
  41. //  FastLED.addLeds<APA102, Z2_DATA_PIN, Z2_CLOCK_PIN>(Z2, 0, Z2_NUM_LEDS);     //Z2 Initialize the FastLED functions
  42.  
  43.     FastLED.setBrightness(64);                                  //Set global maximum brightness
  44.       // This first palette is the basic 'black body radiation' colors,
  45.       // which run from black to red to bright yellow to white.
  46.       gPal = HeatColors_p;
  47.    
  48.       // These are other ways to set up the color palette for the 'fire'.
  49.       // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
  50.       //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
  51.      
  52.       // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
  53.       //   gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  54.      
  55.       // Third, here's a simpler, three-step gradient, from black to red to white
  56.       //  gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
  57.  
  58.           // Davetest for a green pallet.
  59.           //   gPal = CRGBPalette16( CRGB::Black, CRGB::Green, CRGB::YellowGreen, CRGB::White);
  60.          
  61. }
  62.  
  63. void loop() {
  64.  
  65.   // Add entropy to random number generator; we use a lot of it.
  66.     random16_add_entropy( random());
  67.   // ----------------------------------------------------------------------------
  68.   // This one sets up a new palette every time through the loop,
  69.   // based on a hue that changes every time.
  70.   // The palette is a gradient from black, to a dark color based on the hue,
  71.   // to a light color based on the hue, to white.
  72.   //
  73.  /* start extra  
  74.      static uint8_t hue = 0;
  75.      hue++;
  76.      CRGB darkcolor  = CHSV(hue,255,192); // pure hue, three-quarters brightness
  77.      CRGB lightcolor = CHSV(hue,128,255); // half 'whitened', full brightness
  78.      gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
  79. // end extra --------------------------------------------------------------------*/
  80.  
  81.     Fire2012(Z1_Heat, Z1, Z1_NUM_LEDS);     // Call the Fire subroutine, passing the Heatmap,
  82.     Fire2012(Z2_Heat, Z2, Z2_NUM_LEDS);             // the Zone, and Number of LEDs
  83.     FastLED.show();                 // display the frame
  84.     FastLED.delay(1000 / FRAMES_PER_SECOND);    // Slow that sucka down (esp. on a Teensy)
  85. }
  86.  
  87. void Fire2012(byte *heat, CRGB *leds, int number_leds)  //   Might not need the "*" in front of the arrays...
  88. {
  89.     // Step 1.  Cool down every cell a little
  90.     for( int i = 0; i < number_leds; i++) {
  91.         heat[i] = qsub8(heat[i],  random8(0, ((COOLING * 10) / number_leds) + 2));
  92.     }
  93.    
  94.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  95.     for( byte k= number_leds - 1; k >= 2; k--) {
  96.         heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  97.     }
  98.    
  99.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  100.     if( random8() < SPARKING ) {
  101.         int y = random8(7);
  102.         heat[y] = qadd8( heat[y], random8(160,255) );
  103.     }
  104.  
  105.     // Step 4.  Map from heat cells to LED colors
  106.     for( int j = 0; j < number_leds; j++) {
  107.         //leds[j] = HeatColor(heat[j]);
  108. //      byte colorindex = scale8( heat[number_leds-j], 240); // this makes the thing upside down (!)
  109.         byte colorindex = scale8( heat[j], 240);
  110.         leds[j] = ColorFromPalette( gPal, colorindex);
  111.  
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement