Guest User

Untitled

a guest
Apr 23rd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define DATA_PIN 13
  4. #define CLOCK_PIN 14
  5. #define COLOR_ORDER BGR
  6. #define CHIPSET APA102
  7. #define NUM_LEDS 50
  8.  
  9. #define BRIGHTNESS 200
  10. #define FRAMES_PER_SECOND 60
  11.  
  12. CRGB leds[NUM_LEDS];
  13.  
  14. void setup() {
  15. delay(3000); // sanity delay
  16. FastLED.addLeds<CHIPSET, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  17. FastLED.setBrightness( BRIGHTNESS );
  18. }
  19.  
  20. void loop()
  21. {
  22. // Add entropy to random number generator; we use a lot of it.
  23. // random16_add_entropy( random());
  24.  
  25. Fire2012(); // run simulation frame
  26. FastLED.show(); // display this frame
  27.  
  28. #if defined(FASTLED_VERSION) && (FASTLED_VERSION >= 2001000)
  29. FastLED.delay(1000 / FRAMES_PER_SECOND);
  30. #else
  31. delay(1000 / FRAMES_PER_SECOND);
  32. #endif 
  33. }
  34.  
  35.  
  36. // Fire2012 by Mark Kriegsman, July 2012
  37. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  38. //
  39. // This basic one-dimensional 'fire' simulation works roughly as follows:
  40. // There's a underlying array of 'heat' cells, that model the temperature
  41. // at each point along the line. Every cycle through the simulation,
  42. // four steps are performed:
  43. // 1) All cells cool down a little bit, losing heat to the air
  44. // 2) The heat from each cell drifts 'up' and diffuses a little
  45. // 3) Sometimes randomly new 'sparks' of heat are added at the bottom
  46. // 4) The heat from each cell is rendered as a color into the leds array
  47. // The heat-to-color mapping uses a black-body radiation approximation.
  48. //
  49. // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
  50. //
  51. // This simulation scales it self a bit depending on NUM_LEDS; it should look
  52. // "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
  53. //
  54. // I recommend running this simulation at anywhere from 30-100 frames per second,
  55. // meaning an interframe delay of about 10-35 milliseconds.
  56. //
  57. //
  58. // There are two main parameters you can play with to control the look and
  59. // feel of your fire: COOLING (used in step 1 above), and SPARKING (used
  60. // in step 3 above).
  61. //
  62. // COOLING: How much does the air cool as it rises?
  63. // Less cooling = taller flames. More cooling = shorter flames.
  64. // Default 55, suggested range 20-100
  65. #define COOLING 55
  66.  
  67. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  68. // Higher chance = more roaring fire. Lower chance = more flickery fire.
  69. // Default 120, suggested range 50-200.
  70. #define SPARKING 120
  71.  
  72.  
  73. void Fire2012()
  74. {
  75. // Array of temperature readings at each simulation cell
  76. static byte heat[NUM_LEDS];
  77.  
  78. // Step 1. Cool down every cell a little
  79. for( int i = 0; i < NUM_LEDS; i++) {
  80. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  81. }
  82.  
  83. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  84. for( int k= NUM_LEDS - 3; k > 0; k--) {
  85. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  86. }
  87.  
  88. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  89. if( random8() < SPARKING ) {
  90. int y = random8(7);
  91. heat[y] = qadd8( heat[y], random8(160,255) );
  92. }
  93.  
  94. // Step 4. Map from heat cells to LED colors
  95. for( int j = 0; j < NUM_LEDS; j++) {
  96. leds[j] = HeatColor( heat[j]);
  97. }
  98. }
  99.  
  100.  
  101.  
  102. // CRGB HeatColor( uint8_t temperature)
  103. // [to be included in the forthcoming FastLED v2.1]
  104. //
  105. // Approximates a 'black body radiation' spectrum for
  106. // a given 'heat' level. This is useful for animations of 'fire'.
  107. // Heat is specified as an arbitrary scale from 0 (cool) to 255 (hot).
  108. // This is NOT a chromatically correct 'black body radiation'
  109. // spectrum, but it's surprisingly close, and it's extremely fast and small.
  110. //
  111. // On AVR/Arduino, this typically takes around 70 bytes of program memory,
  112. // versus 768 bytes for a full 256-entry RGB lookup table.
  113.  
  114. CRGB HeatColor( uint8_t temperature)
  115. {
  116. CRGB heatcolor;
  117.  
  118. // Scale 'heat' down from 0-255 to 0-191,
  119. // which can then be easily divided into three
  120. // equal 'thirds' of 64 units each.
  121. uint8_t t192 = scale8_video( temperature, 192);
  122.  
  123. // calculate a value that ramps up from
  124. // zero to 255 in each 'third' of the scale.
  125. uint8_t heatramp = t192 & 0x3F; // 0..63
  126. heatramp <<= 2; // scale up to 0..252
  127.  
  128. // now figure out which third of the spectrum we're in:
  129. if( t192 & 0x80) {
  130. // we're in the hottest third
  131. heatcolor.r = 255; // full red
  132. heatcolor.g = 255; // full green
  133. heatcolor.b = heatramp; // ramp up blue
  134.  
  135. } else if( t192 & 0x40 ) {
  136. // we're in the middle third
  137. heatcolor.r = 255; // full red
  138. heatcolor.g = heatramp; // ramp up green
  139. heatcolor.b = 0; // no blue
  140.  
  141. } else {
  142. // we're in the coolest third
  143. heatcolor.r = heatramp; // ramp up red
  144. heatcolor.g = 0; // no green
  145. heatcolor.b = 0; // no blue
  146. }
  147.  
  148. return heatcolor;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment