computermuseo

fire led strip ws2812b arduino

Mar 23rd, 2017
1,563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4. #define COLOR_ORDER GRB
  5. #define CHIPSET WS2811
  6. #define NUM_LEDS 30
  7.  
  8. #define BRIGHTNESS 200
  9. #define FRAMES_PER_SECOND 60
  10.  
  11. bool gReverseDirection = false;
  12.  
  13. CRGB leds[NUM_LEDS];
  14.  
  15. void setup() {
  16. delay(3000); // sanity delay
  17. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  18. FastLED.setBrightness( BRIGHTNESS );
  19. }
  20.  
  21. void loop()
  22. {
  23. // Add entropy to random number generator; we use a lot of it.
  24. // random16_add_entropy( random());
  25.  
  26. Fire2012(); // run simulation frame
  27.  
  28. FastLED.show(); // display this frame
  29. FastLED.delay(1000 / FRAMES_PER_SECOND);
  30. }
  31.  
  32.  
  33. // Fire2012 by Mark Kriegsman, July 2012
  34. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  35. ////
  36. // This basic one-dimensional 'fire' simulation works roughly as follows:
  37. // There's a underlying array of 'heat' cells, that model the temperature
  38. // at each point along the line. Every cycle through the simulation,
  39. // four steps are performed:
  40. // 1) All cells cool down a little bit, losing heat to the air
  41. // 2) The heat from each cell drifts 'up' and diffuses a little
  42. // 3) Sometimes randomly new 'sparks' of heat are added at the bottom
  43. // 4) The heat from each cell is rendered as a color into the leds array
  44. // The heat-to-color mapping uses a black-body radiation approximation.
  45. //
  46. // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
  47. //
  48. // This simulation scales it self a bit depending on NUM_LEDS; it should look
  49. // "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
  50. //
  51. // I recommend running this simulation at anywhere from 30-100 frames per second,
  52. // meaning an interframe delay of about 10-35 milliseconds.
  53. //
  54. // Looks best on a high-density LED setup (60+ pixels/meter).
  55. //
  56. //
  57. // There are two main parameters you can play with to control the look and
  58. // feel of your fire: COOLING (used in step 1 above), and SPARKING (used
  59. // in step 3 above).
  60. //
  61. // COOLING: How much does the air cool as it rises?
  62. // Less cooling = taller flames. More cooling = shorter flames.
  63. // Default 50, suggested range 20-100
  64. #define COOLING 55
  65.  
  66. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  67. // Higher chance = more roaring fire. Lower chance = more flickery fire.
  68. // Default 120, suggested range 50-200.
  69. #define SPARKING 120
  70.  
  71.  
  72. void Fire2012()
  73. {
  74. // Array of temperature readings at each simulation cell
  75. static byte heat[NUM_LEDS];
  76.  
  77. // Step 1. Cool down every cell a little
  78. for( int i = 0; i < NUM_LEDS; i++) {
  79. heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  80. }
  81.  
  82. // Step 2. Heat from each cell drifts 'up' and diffuses a little
  83. for( int k= NUM_LEDS - 1; k >= 2; k--) {
  84. heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  85. }
  86.  
  87. // Step 3. Randomly ignite new 'sparks' of heat near the bottom
  88. if( random8() < SPARKING ) {
  89. int y = random8(7);
  90. heat[y] = qadd8( heat[y], random8(160,255) );
  91. }
  92.  
  93. // Step 4. Map from heat cells to LED colors
  94. for( int j = 0; j < NUM_LEDS; j++) {
  95. CRGB color = HeatColor( heat[j]);
  96. int pixelnumber;
  97. if( gReverseDirection ) {
  98. pixelnumber = (NUM_LEDS-1) - j;
  99. } else {
  100. pixelnumber = j;
  101. }
  102. leds[pixelnumber] = color;
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment