Advertisement
christian_suryanto

Fire2022

Nov 2nd, 2022 (edited)
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.30 KB | Source Code | 0 0
  1. #include <FastLED.h>
  2. //#include <ColorConverterLib.h>
  3.  
  4. #define NUM_LEDS 192
  5. #define kMatrixHeight 24
  6. #define kMatrixWidth 8
  7. #define currentBrightness 60
  8.  
  9. CRGB Leds1[NUM_LEDS];
  10. CRGB Leds2[NUM_LEDS];
  11. CRGB leds[NUM_LEDS];
  12.  
  13.  
  14. void setup()
  15. {
  16.   Serial.begin(115200);
  17.   FastLED.addLeds<WS2812B, 2, GRB>(leds, NUM_LEDS).setDither(0);
  18.   FastLED.setBrightness(currentBrightness);
  19.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 8000);
  20.   FastLED.show();
  21. }
  22.  
  23. #define FIXED_FIRE2_SCALE_FOR_Y 4  // 1-11 is scale for Y axis
  24.  
  25. int BlendAmount = 0;
  26. int BlendSteps = 5;
  27. bool Swap = false;
  28.  
  29. bool AutoPalette = true;
  30. uint8_t Scale;
  31. uint8_t Speed;
  32. uint8_t PaletteIndex;
  33.  
  34. uint8_t Speed1 = 150;       // 1-252 ...why is not 255?!
  35. uint8_t Scale1 = 9;         // 1-99 is palette and scale for X axis
  36. uint8_t PaletteIndex1;
  37. bool Loading1 = true;
  38. uint8_t DeltaValue1;
  39. uint8_t DeltaHue1;
  40. uint8_t Step1;
  41. uint8_t pcnt1;
  42. uint16_t ff_x1, ff_z1;
  43.  
  44. uint8_t Speed2 = 150;       // 1-252 ...why is not 255?!
  45. uint8_t Scale2 = 9;         // 1-99 is palette and scale for X axis
  46. uint8_t PaletteIndex2;
  47. bool Loading2 = true;
  48. uint8_t DeltaValue2;
  49. uint8_t DeltaHue2;
  50. uint8_t Step2;
  51. uint8_t pcnt2;
  52. uint16_t ff_x2, ff_z2;
  53.  
  54. extern const TProgmemRGBPalette16 WaterfallColors_p FL_PROGMEM = {0x000000, 0x060707, 0x101110, 0x151717, 0x1C1D22, 0x242A28, 0x363B3A, 0x313634, 0x505552, 0x6B6C70, 0x98A4A1, 0xC1C2C1, 0xCACECF, 0xCDDEDD, 0xDEDFE0, 0xB2BAB9};
  55. extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};             //* Orange
  56. extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};                             // пытаюсь сделать что-то более приличное
  57. extern const TProgmemRGBPalette16 Normalp FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};                      // пытаюсь сделать что-то более приличное
  58. extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};        //* Red
  59. extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};           //* Yellow
  60. extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};     //* Green
  61. extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};  //* Blue
  62. extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};        //* Indigo
  63. extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink}; //* Violet
  64.  
  65. const TProgmemRGBPalette16 *Palette[] =
  66. {
  67.     &PartyColors_p,
  68.     &OceanColors_p,
  69.     &LavaColors_p,
  70.     &HeatColors_p,
  71.     &WaterfallColors_p,
  72.     &CloudColors_p,
  73.     &ForestColors_p,
  74.     &RainbowColors_p,
  75.     &RainbowStripeColors_p,
  76.    
  77.     &WoodFireColors_p,
  78.     &NormalFire_p,
  79.     &Normalp,
  80.     &LithiumFireColors_p,
  81.     &SodiumFireColors_p,
  82.     &CopperFireColors_p,
  83.     &AlcoholFireColors_p,
  84.     &RubidiumFireColors_p,
  85.     &PotassiumFireColors_p
  86. };
  87.  
  88. const TProgmemRGBPalette16 *currentPalette1 = Palette[0];
  89. const TProgmemRGBPalette16 *currentPalette2 = Palette[0];
  90.  
  91.  
  92.  
  93. // FIRE2022 CODES //
  94. // Modified from //
  95. // ============= Эффект Огонь 2021 вариант 2 с фиксированным масштабом по оси Y ===============
  96. // (c) SottNick
  97. // На основе алгоритма https://editor.soulmatelights.com/gallery/546-fire
  98. // by Stepko
  99.  
  100. void ChangePalette()
  101. {
  102.   Swap = !Swap;
  103.   if (Swap)
  104.   {
  105.     Loading2 = true;
  106.   }
  107.   else
  108.   {
  109.     Loading1 = true;
  110.   }
  111. }
  112.  
  113. void makeFire2022(CRGB *LEDARRAY1)
  114. {
  115.   if (AutoPalette)
  116.   {
  117.     EVERY_N_SECONDS(10)
  118.     {
  119.       ChangePalette();
  120.     }
  121.   }
  122.  
  123.   runAll(LEDARRAY1);
  124. }
  125.  
  126. void runAll(CRGB *LEDARRAY20)
  127. {
  128.     EVERY_N_MILLIS(10)
  129.     {
  130.       blend(Leds1, Leds2, LEDARRAY20, NUM_LEDS, BlendAmount);
  131.  
  132.       if (Swap)
  133.       {
  134.         if (BlendAmount < 255) BlendAmount+=BlendSteps;                   // Blend 'up' to source 2
  135.       }
  136.       else
  137.       {
  138.         if (BlendAmount > 0) BlendAmount-=BlendSteps;                     // Blend 'down' to source 1
  139.       }
  140.     }
  141.    
  142.     draw1(Leds1);
  143.     draw2(Leds2);
  144. }
  145.  
  146. void draw1(CRGB *LEDARRAY21)
  147. {
  148.   if (Loading1)
  149.   {
  150.     random16_set_seed(8934);
  151.     random16_add_entropy(analogRead(32));
  152.     Scale1 = 1+random8(99);
  153.     Speed1 = 2+random8(169);
  154.  
  155.     if (Scale1 > 100U) Scale1 = 100U;
  156.     PaletteIndex1 = (uint8_t)(Scale1/100.0F*((sizeof(Palette)/sizeof(TProgmemRGBPalette16 *))-0.01F));
  157.     currentPalette1 = Palette[PaletteIndex1];
  158.    
  159.     Loading1 = false;
  160.     if (Scale1 > 100U) Scale1 = 100U;
  161.    
  162.     Step1 = map(Speed1*Speed1, 1U, 65025U, (FIXED_FIRE2_SCALE_FOR_Y-1U)/2U+1U, FIXED_FIRE2_SCALE_FOR_Y * 18U + 44);
  163.     DeltaValue1 = 0.7 * DeltaValue1 * DeltaValue1 + 31.3;
  164.     pcnt1 = map(Step1, 1U, 255U, 20U, 128U);
  165.     DeltaHue1 = 0.7 * FIXED_FIRE2_SCALE_FOR_Y * FIXED_FIRE2_SCALE_FOR_Y + 31.3;
  166.   }
  167.  
  168.   ff_x1 += Step1;
  169.   for (byte x = 0; x < kMatrixWidth; x++)
  170.   {
  171.     for (byte y = 0; y < kMatrixHeight; y++)
  172.     {
  173.       int16_t Bri1 = inoise8(x * DeltaValue1, (y * DeltaHue1) - ff_x1, ff_z1) - (y * (255 / kMatrixHeight));
  174.       byte Col1 = Bri1;
  175.       if (Bri1 < 0)
  176.         Bri1 = 0;
  177.       if (Bri1 != 0)
  178.         Bri1 = 256 - (Bri1 * 0.2);
  179.       nblend(LEDARRAY21[XY(x, y)], ColorFromPalette(*currentPalette1, Col1, Bri1), pcnt1);
  180.     }
  181.   }
  182.   if (!random8())
  183.     ff_z1++;
  184.   FastLED.show();
  185. }
  186.  
  187. void draw2(CRGB *LEDARRAY22)
  188. {
  189.   if (Loading2)
  190.   {
  191.     random16_set_seed(8934);
  192.     random16_add_entropy(analogRead(32));
  193.    
  194.     Scale2 = 1+random8(99);
  195.     Speed2 = 2+random8(169);
  196.  
  197.     if (Scale2 > 100U) Scale2 = 100U;
  198.     PaletteIndex2 = (uint8_t)(Scale2/100.0F*((sizeof(Palette)/sizeof(TProgmemRGBPalette16 *))-0.01F));
  199.     currentPalette2 = Palette[PaletteIndex2];
  200.  
  201.     Loading2 = false;
  202.     if (Scale2 > 100U) Scale2 = 100U;
  203.    
  204.     Step2 = map(Speed2*Speed2, 1U, 65025U, (FIXED_FIRE2_SCALE_FOR_Y-1U)/2U+1U, FIXED_FIRE2_SCALE_FOR_Y * 18U + 44);
  205.     DeltaValue2 = 0.7 * DeltaValue2 * DeltaValue2 + 31.3;
  206.     pcnt2 = map(Step2, 1U, 255U, 20U, 128U);
  207.     DeltaHue2 = 0.7 * FIXED_FIRE2_SCALE_FOR_Y * FIXED_FIRE2_SCALE_FOR_Y + 31.3;
  208.   }
  209.  
  210.   ff_x2 += Step2;
  211.   for (byte x = 0; x < kMatrixWidth; x++)
  212.   {
  213.     for (byte y = 0; y < kMatrixHeight; y++)
  214.     {
  215.       int16_t Bri2 = inoise8(x * DeltaValue2, (y * DeltaHue2) - ff_x2, ff_z2) - (y * (255 / kMatrixHeight));
  216.       byte Col2 = Bri2;
  217.       if (Bri2 < 0)
  218.         Bri2 = 0;
  219.       if (Bri2 != 0)
  220.         Bri2 = 256 - (Bri2 * 0.2);
  221.       nblend(LEDARRAY22[XY(x, y)], ColorFromPalette(*currentPalette2, Col2, Bri2), pcnt2);
  222.     }
  223.   }
  224.   if (!random8())
  225.     ff_z2++;
  226.   FastLED.show();
  227. }
  228.  
  229.  
  230. void loop()
  231. {
  232.   makeFire2022(leds);
  233. }
  234.  
  235.  
  236. uint16_t XY( uint8_t x, uint8_t y)
  237. {
  238.   if(y >= kMatrixHeight) { y = kMatrixHeight - 1; }
  239.   if(x >= kMatrixWidth) { x = kMatrixWidth - 1; }
  240.   uint16_t ret;
  241.  
  242.   ret = (x * kMatrixHeight) + y;
  243.   return ret;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement