Advertisement
nathancarter

Hades flaming helmet

Oct 3rd, 2022
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 KB | Source Code | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <FastLED.h>
  3. #include <JC_Button.h>
  4.  
  5. #define COLOR_ORDER GRB
  6. #define CHIPSET     WS2812
  7.  
  8. #define FIRE_TOTAL_LEDS 46
  9. #define FIRE_PIN 14
  10. #define FIRE_BRIGHTNESS_NOMINAL 16
  11. #define FIRE_BRIGHTNESS_MAX 255
  12. #define FIRE_SPEED 75// ms between fire refresh rate, higher is slower
  13. #define COOLING 70
  14. #define SPARKING 270
  15. // #define FIRE_ROW_LEDS 11
  16. // #define FIRE_ROW_COUNT 5
  17.  
  18. #define BRIGHTBUTTON_PIN 16
  19. #define BRIGHT_DELAY_LONG 60000 // how many ms to stay in bright mode after bright button is pressed
  20. #define BRIGHT_DELAY_SHORT 10000 // how many ms to stay in bright mode after color button is pressed
  21.  
  22. #define COLORBUTTON_PIN 17
  23.  
  24. Adafruit_NeoPixel FireStrip = Adafruit_NeoPixel(FIRE_TOTAL_LEDS, FIRE_PIN, NEO_GRB + NEO_KHZ800);
  25.  
  26. Button BrightButton(BRIGHTBUTTON_PIN);       // define the button that increases brightness
  27. Button ColorButton(COLORBUTTON_PIN);       // define the button that toggles color
  28.  
  29. CRGBPalette16 gPal;     // Palette color for flames
  30.  
  31.  
  32. unsigned long TimerMaster = 0; // master clock timer
  33. unsigned long TimerFire = 0; // fire update timer
  34. unsigned long TimerBright = 0; // bright-mode timer
  35.  
  36. int FireBrightness = FIRE_BRIGHTNESS_NOMINAL; // brightness value to be changed on button press
  37.  
  38. bool FireFade = 0; // to fade fire from MAX to NOMINAL
  39. byte heat[FIRE_TOTAL_LEDS]; // global array of heat values for all five rows
  40.  
  41. bool ColorToggle = 0; // remember the current flame color 0=blue 1=red
  42.  
  43. void setup()
  44. {
  45.   delay(1500); // sanity delay
  46.   gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  47.   //gPal = HeatColors_p;
  48.   FireStrip.begin(); // initialize fire strip
  49.   FireStrip.setBrightness(FireBrightness);
  50.   BrightButton.begin();
  51.   ColorButton.begin();
  52. }
  53.  
  54. void loop()
  55. {
  56.   TimerMaster = millis();
  57.  
  58.   BrightButton.read();
  59.   if (BrightButton.wasPressed())
  60.   {
  61.     // If it's not already bright, crank up the brightness for a certain amount of time
  62.     if (TimerMaster >= TimerBright)
  63.     {
  64.       FireBrightness = FIRE_BRIGHTNESS_MAX;
  65.       FireFade = 0;
  66.       FireStrip.show();
  67.       TimerBright = (TimerMaster + BRIGHT_DELAY_LONG);
  68.     }
  69.     // Otherwise, if it's already bright, go ahead and start fading
  70.     else
  71.     {
  72.       TimerBright = TimerMaster;
  73.     }
  74.   }
  75.  
  76.   ColorButton.read();
  77.   if (ColorButton.wasPressed())
  78.   {
  79.     // If the button was pressed, toggle the flame color
  80.     if (ColorToggle == 0)
  81.     {
  82.       gPal = HeatColors_p;
  83.     }
  84.     else
  85.     {
  86.       gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  87.     }
  88.     ColorToggle = !ColorToggle;
  89.     // and crank up the brightness
  90.     FireBrightness = FIRE_BRIGHTNESS_MAX;
  91.     FireFade = 0;
  92.  
  93.     FireStrip.show();
  94.     TimerBright = (TimerMaster + BRIGHT_DELAY_SHORT);
  95.   }
  96.  
  97.   if (TimerMaster >= TimerBright)  // If the brightness timer has aged out
  98.   {
  99.     // Turn on toggle to slowly fade Fire brightness to nominal value
  100.     FireFade = 1;
  101.     //FireBrightness = FIRE_BRIGHTNESS_NOMINAL;
  102.   }
  103.  
  104.  
  105.   if (TimerMaster >= TimerFire)
  106.   {
  107.     if (FireFade == 1 && FireBrightness > FIRE_BRIGHTNESS_NOMINAL)
  108.     {
  109.       FireBrightness = FireBrightness -2;
  110.       if (FireBrightness <= FIRE_BRIGHTNESS_NOMINAL)
  111.       {
  112.         FireBrightness = FIRE_BRIGHTNESS_NOMINAL;
  113.         FireFade = 0;
  114.         // Always reset the color to blue when brightness fade completes
  115.         gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  116.         ColorToggle = 0;
  117.       }
  118.     }
  119.  
  120.     FireStrip.setBrightness(FireBrightness);
  121.     Fire2012(0, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  122.     Fire2012(6, 5, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  123.     Fire2012(11, 5, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  124.     Fire2012(16, 5, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  125.     Fire2012(21, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  126.     Fire2012(27, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  127.     Fire2012(33, 6, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  128.     Fire2012(39, 7, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  129.     TimerFire = (TimerMaster + FIRE_SPEED);
  130.     FireStrip.show();
  131.   }
  132. }
  133.  
  134.  
  135.  
  136. void Fire2012(int LED_START_NUM, int FIRE_ROW_LEDS, bool gReverseDirection, int cool, int spark)
  137. {
  138.   // Step 1.  Cool down every cell a little
  139.   for ( int i = LED_START_NUM; i < (FIRE_ROW_LEDS + LED_START_NUM); i++)
  140.   {
  141.     heat[i] = qsub8( heat[i],  random8(0, ((cool * 10) / FIRE_ROW_LEDS) + 2));
  142.   }
  143.  
  144.   // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  145.   for ( int k = (FIRE_ROW_LEDS + LED_START_NUM) - 1; k >= (LED_START_NUM + 2); k--)
  146.   {
  147.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  148.   }
  149.  
  150.   // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  151.   if ( random8() < spark )
  152.   {
  153.     int y = random8(3);
  154.     heat[LED_START_NUM + y] = qadd8( heat[LED_START_NUM + y], random8(160, 255) );
  155.   }
  156.  
  157.   // Step 4.  Map from heat cells to LED colors
  158.   for ( int j = LED_START_NUM; j < (FIRE_ROW_LEDS + LED_START_NUM); j++)
  159.   {
  160.     // CRGB color = HeatColor(min(150, heat[j]));
  161.     CRGB color = ColorFromPalette(gPal, min(150, heat[j]));
  162.     int pixelnumber;
  163.     if ( gReverseDirection )
  164.     {
  165.       pixelnumber = ((FIRE_ROW_LEDS + LED_START_NUM) - 1) - (j - LED_START_NUM);
  166.     }
  167.     else
  168.     {
  169.       pixelnumber = j;
  170.     }
  171.     FireStrip.setPixelColor(pixelnumber, color.r, color.g, color.b);
  172.   }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement