BurningWreck

FastLED - Burning Fuse effect

Sep 17th, 2025
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | Software | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4. #define NUM_LEDS 60
  5. #define LED_TYPE WS2812B
  6. #define COLOR_ORDER GRB
  7.  
  8. CRGB leds[NUM_LEDS];
  9.  
  10. // Fuse simulation variables
  11. int fusePosition = 0;          // Current position of the burning fuse
  12. bool fuseActive = true;        // Whether the fuse is still burning
  13. unsigned long lastUpdate = 0;  // Time tracking for animation
  14. int fuseSpeed = 150;           // Milliseconds between each step (adjust for speed)
  15.  
  16. // Color definitions
  17. CRGB fuseColor = CRGB::Orange;       // Active burning point
  18. CRGB emberColor = CRGB::Red;         // Hot ember behind the fuse
  19. CRGB coolEmberColor = CRGB(64, 16, 0); // Cooling ember
  20. CRGB deadColor = CRGB::Black;        // Burned out section
  21.  
  22. void setup() {
  23.   Serial.begin(9600);
  24.  
  25.   // Initialize FastLED
  26.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  27.   FastLED.setBrightness(200);
  28.  
  29.   // Initialize all LEDs to represent an unlit fuse (dim yellow/brown)
  30.   for(int i = 0; i < NUM_LEDS; i++) {
  31.     leds[i] = CRGB(40, 25, 0); // Unlit fuse color
  32.   }
  33.   FastLED.show();
  34.  
  35.   Serial.println("Fuse burning simulation started!");
  36.   Serial.println("Send 'r' to restart the fuse");
  37. }
  38.  
  39. void loop() {
  40.   // Check for serial input to restart
  41.   if(Serial.available() > 0) {
  42.     char input = Serial.read();
  43.     if(input == 'r' || input == 'R') {
  44.       restartFuse();
  45.     }
  46.   }
  47.  
  48.   // Update fuse animation
  49.   if(fuseActive && millis() - lastUpdate >= fuseSpeed) {
  50.     updateFuse();
  51.     lastUpdate = millis();
  52.   }
  53.  
  54.   // Add some flickering to the active burning point
  55.   if(fuseActive && fusePosition < NUM_LEDS) {
  56.     addFlicker();
  57.   }
  58.  
  59.   FastLED.show();
  60.   delay(20); // Small delay for smooth animation
  61. }
  62.  
  63. void updateFuse() {
  64.   if(fusePosition >= NUM_LEDS) {
  65.     fuseActive = false;
  66.     Serial.println("BOOM! Fuse has burned completely!");
  67.     // Optional: Add explosion effect
  68.     explosionEffect();
  69.     return;
  70.   }
  71.  
  72.   // Set the current burning position
  73.   leds[fusePosition] = fuseColor;
  74.  
  75.   // Update the trail behind the fuse
  76.   updateTrail();
  77.  
  78.   // Move to next position
  79.   fusePosition++;
  80.  
  81.   Serial.print("Fuse burning at position: ");
  82.   Serial.println(fusePosition);
  83. }
  84.  
  85. void updateTrail() {
  86.   // Update the burning trail behind the active fuse
  87.   for(int i = 0; i < fusePosition; i++) {
  88.     int distanceFromFuse = fusePosition - i;
  89.    
  90.     if(distanceFromFuse == 1) {
  91.       // Hot ember right behind the fuse
  92.       leds[i] = emberColor;
  93.     }
  94.     else if(distanceFromFuse <= 3) {
  95.       // Cooling embers
  96.       int brightness = map(distanceFromFuse, 2, 3, 128, 32);
  97.       leds[i] = CRGB(brightness, brightness/4, 0);
  98.     }
  99.     else if(distanceFromFuse <= 6) {
  100.       // Cool embers
  101.       int brightness = map(distanceFromFuse, 4, 6, 32, 8);
  102.       leds[i] = CRGB(brightness, brightness/8, 0);
  103.     }
  104.     else {
  105.       // Completely burned out
  106.       leds[i] = deadColor;
  107.     }
  108.   }
  109. }
  110.  
  111. void addFlicker() {
  112.   if(fusePosition < NUM_LEDS) {
  113.     // Add random flickering to the burning point
  114.     int flicker = random(-30, 30);
  115.     CRGB flickerColor = fuseColor;
  116.    
  117.     flickerColor.r = constrain(flickerColor.r + flicker, 0, 255);
  118.     flickerColor.g = constrain(flickerColor.g + flicker/2, 0, 255);
  119.     flickerColor.b = constrain(flickerColor.b + flicker/4, 0, 255);
  120.    
  121.     leds[fusePosition] = flickerColor;
  122.    
  123.     // Occasionally spark ahead
  124.     if(random(100) < 5 && fusePosition + 1 < NUM_LEDS) {
  125.       leds[fusePosition + 1] = CRGB(255, 100, 0);
  126.     }
  127.   }
  128. }
  129.  
  130. void explosionEffect() {
  131.   // Simple explosion effect at the end
  132.   for(int brightness = 0; brightness < 255; brightness += 10) {
  133.     for(int i = 0; i < NUM_LEDS; i++) {
  134.       // Distance-based explosion effect
  135.       int distance = abs(i - (NUM_LEDS - 1));
  136.       int explosionBrightness = max(0, brightness - (distance * 8));
  137.      
  138.       leds[i] = CRGB(explosionBrightness, explosionBrightness/2, 0);
  139.     }
  140.     FastLED.show();
  141.     delay(30);
  142.   }
  143.  
  144.   // Fade out
  145.   for(int brightness = 255; brightness >= 0; brightness -= 5) {
  146.     for(int i = 0; i < NUM_LEDS; i++) {
  147.       leds[i].fadeToBlackBy(5);
  148.     }
  149.     FastLED.show();
  150.     delay(20);
  151.   }
  152. }
  153.  
  154. void restartFuse() {
  155.   Serial.println("Restarting fuse...");
  156.  
  157.   fusePosition = 0;
  158.   fuseActive = true;
  159.   lastUpdate = 0;
  160.  
  161.   // Reset all LEDs to unlit fuse
  162.   for(int i = 0; i < NUM_LEDS; i++) {
  163.     leds[i] = CRGB(40, 25, 0); // Unlit fuse color
  164.   }
  165.  
  166.   FastLED.show();
  167. }
  168.  
  169. // Alternative function: Reverse burning (from end to start)
  170. void startReverseFuse() {
  171.   fusePosition = NUM_LEDS - 1;
  172.   fuseActive = true;
  173.   fuseSpeed = -fuseSpeed; // Negative speed for reverse direction
  174. }
  175.  
  176. // Alternative function: Random spark ignition
  177. void randomSpark() {
  178.   int sparkPos = random(NUM_LEDS);
  179.   leds[sparkPos] = CRGB::White;
  180.   FastLED.show();
  181.   delay(100);
  182.   leds[sparkPos] = CRGB(40, 25, 0);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment