PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Fire2012WithPalette.ino 6.1KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <FastLED.h>
  2. #define LED_PIN 5
  3. #define COLOR_ORDER GRB
  4. #define CHIPSET WS2811
  5. #define NUM_LEDS 30
  6. #define BRIGHTNESS 200
  7. #define FRAMES_PER_SECOND 60
  8. bool gReverseDirection = false;
  9. CRGB leds[NUM_LEDS];
  10. // Fire2012 with programmable Color Palette
  11. //
  12. // This code is the same fire simulation as the original "Fire2012",
  13. // but each heat cell's temperature is translated to color through a FastLED
  14. // programmable color palette, instead of through the "HeatColor(...)" function.
  15. //
  16. // Four different static color palettes are provided here, plus one dynamic one.
  17. //
  18. // The three static ones are:
  19. // 1. the FastLED built-in HeatColors_p -- this is the default, and it looks
  20. // pretty much exactly like the original Fire2012.
  21. //
  22. // To use any of the other palettes below, just "uncomment" the corresponding code.
  23. //
  24. // 2. a gradient from black to red to yellow to white, which is
  25. // visually similar to the HeatColors_p, and helps to illustrate
  26. // what the 'heat colors' palette is actually doing,
  27. // 3. a similar gradient, but in blue colors rather than red ones,
  28. // i.e. from black to blue to aqua to white, which results in
  29. // an "icy blue" fire effect,
  30. // 4. a simplified three-step gradient, from black to red to white, just to show
  31. // that these gradients need not have four components; two or
  32. // three are possible, too, even if they don't look quite as nice for fire.
  33. //
  34. // The dynamic palette shows how you can change the basic 'hue' of the
  35. // color palette every time through the loop, producing "rainbow fire".
  36. CRGBPalette16 gPal;
  37. void setup() {
  38. delay(3000); // sanity delay
  39. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  40. FastLED.setBrightness( BRIGHTNESS );
  41. // This first palette is the basic 'black body radiation' colors,
  42. // which run from black to red to bright yellow to white.
  43. gPal = HeatColors_p;
  44. // These are other ways to set up the color palette for the 'fire'.
  45. // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
  46. // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
  47. // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
  48. // gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
  49. // Third, here's a simpler, three-step gradient, from black to red to white
  50. // gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
  51. }
  52. void loop()
  53. {
  54. // Add entropy to random number generator; we use a lot of it.
  55. random16_add_entropy( random());
  56. // Fourth, the most sophisticated: this one sets up a new palette every
  57. // time through the loop, based on a hue that changes every time.
  58. // The palette is a gradient from black, to a dark color based on the hue,
  59. // to a light color based on the hue, to white.
  60. //
  61. // static uint8_t hue = 0;
  62. // hue++;
  63. // CRGB darkcolor = CHSV(hue,255,192); // pure hue, three-quarters brightness
  64. // CRGB lightcolor = CHSV(hue,128,255); // half 'whitened', full brightness
  65. // gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
  66. Fire2012WithPalette(); // run simulation frame, using palette colors
  67. FastLED.show(); // display this frame
  68. FastLED.delay(1000 / FRAMES_PER_SECOND);
  69. }
  70. // Fire2012 by Mark Kriegsman, July 2012
  71. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  72. ////
  73. // This basic one-dimensional 'fire' simulation works roughly as follows:
  74. // There's a underlying array of 'heat' cells, that model the temperature
  75. // at each point along the line. Every cycle through the simulation,
  76. // four steps are performed:
  77. // 1) All cells cool down a little bit, losing heat to the air
  78. // 2) The heat from each cell drifts 'up' and diffuses a little
  79. // 3) Sometimes randomly new 'sparks' of heat are added at the bottom
  80. // 4) The heat from each cell is rendered as a color into the leds array
  81. // The heat-to-color mapping uses a black-body radiation approximation.
  82. //
  83. // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
  84. //
  85. // This simulation scales it self a bit depending on NUM_LEDS; it should look
  86. // "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
  87. //
  88. // I recommend running this simulation at anywhere from 30-100 frames per second,
  89. // meaning an interframe delay of about 10-35 milliseconds.
  90. //
  91. // Looks best on a high-density LED setup (60+ pixels/meter).
  92. //
  93. //
  94. // There are two main parameters you can play with to control the look and
  95. // feel of your fire: COOLING (used in step 1 above), and SPARKING (used
  96. // in step 3 above).
  97. //
  98. // COOLING: How much does the air cool as it rises?
  99. // Less cooling = taller flames. More cooling = shorter flames.
  100. // Default 55, suggested range 20-100
  101. #define COOLING 55
  102. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  103. // Higher chance = more roaring fire. Lower chance = more flickery fire.
  104. // Default 120, suggested range 50-200.
  105. #define SPARKING 120
  106. void Fire2012WithPalette()
  107. {
  108. // Array of temperature readings at each simulation cell
  109. static byte heat[NUM_LEDS];
  110. // Step 1. Cool down every cell a little
  111. for( int i = 0; i < NUM_LEDS; i++) {
  112. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  113. }
  114. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  115. for( int k= NUM_LEDS - 1; k >= 2; k--) {
  116. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  117. }
  118. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  119. if( random8() < SPARKING ) {
  120. int y = random8(7);
  121. heat[y] = qadd8( heat[y], random8(160,255) );
  122. }
  123. // Step 4. Map from heat cells to LED colors
  124. for( int j = 0; j < NUM_LEDS; j++) {
  125. // Scale the heat value from 0-255 down to 0-240
  126. // for best results with color palettes.
  127. byte colorindex = scale8( heat[j], 240);
  128. CRGB color = ColorFromPalette( gPal, colorindex);
  129. int pixelnumber;
  130. if( gReverseDirection ) {
  131. pixelnumber = (NUM_LEDS-1) - j;
  132. } else {
  133. pixelnumber = j;
  134. }
  135. leds[pixelnumber] = color;
  136. }
  137. }