Advertisement
pleasedontcode

"Fire Simulation" rev_01

Jul 9th, 2024
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Fire Simulation"
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-07-09 15:46:57
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a red fire simulation with WS2812B LEDs */
  21.     /* using the FastLED library. Connect the LED data */
  22.     /* pin to pin 2. Develop setup and loop functions to */
  23.     /* initialize and continuously update the LED states. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <FastLED.h>  //https://github.com/FastLED/FastLED
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void Fire2012(void);
  33.  
  34. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  35. const uint8_t WS2812B_WS2812B_DIN_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  38. /***** used to store raw data *****/
  39. bool WS2812B_WS2812B_DIN_PIN_D2_rawData = 0;
  40.  
  41. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  42. /***** used to store data after characteristic curve transformation *****/
  43. float WS2812B_WS2812B_DIN_PIN_D2_phyData = 0.0;
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. #define NUM_LEDS 30
  47. #define LED_PIN  WS2812B_WS2812B_DIN_PIN_D2
  48. #define COLOR_ORDER GRB
  49. #define CHIPSET WS2812B
  50. #define BRIGHTNESS 200
  51. #define FRAMES_PER_SECOND 60
  52.  
  53. #define COOLING  55
  54. #define SPARKING 120
  55.  
  56. CRGB leds[NUM_LEDS];
  57.  
  58. void setup(void) {
  59.   // put your setup code here, to run once:
  60.   delay(3000); // sanity delay
  61.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  62.   FastLED.setBrightness(BRIGHTNESS);
  63.   pinMode(WS2812B_WS2812B_DIN_PIN_D2, OUTPUT);
  64. }
  65.  
  66. void loop(void) {
  67.   // put your main code here, to run repeatedly:
  68.   Fire2012(); // Fire simulation
  69.   FastLED.show();
  70.   FastLED.delay(1000 / FRAMES_PER_SECOND);
  71. }
  72.  
  73. void Fire2012() {
  74.   static uint8_t heat[NUM_LEDS];
  75.  
  76.   // Step 1. Cool down every cell a little
  77.   for (int i = 0; i < NUM_LEDS; i++) {
  78.     heat[i] = qsub8(heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  79.   }
  80.  
  81.   // Step 2. Heat from each cell drifts 'up' and diffuses a little
  82.   for (int k = NUM_LEDS - 1; k >= 2; k--) {
  83.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  84.   }
  85.  
  86.   // Step 3. Randomly ignite new 'sparks' near the bottom
  87.   if (random8() < SPARKING) {
  88.     int y = random8(7);
  89.     heat[y] = qadd8(heat[y], random8(160, 255));
  90.   }
  91.  
  92.   // Step 4. Convert heat to LED colors
  93.   for (int j = 0; j < NUM_LEDS; j++) {
  94.     leds[j] = HeatColor(heat[j]);
  95.   }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement