Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define LED_PIN 6
- #define NUM_LEDS 60
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- CRGB leds[NUM_LEDS];
- // Fuse simulation variables
- int fusePosition = 0; // Current position of the burning fuse
- bool fuseActive = true; // Whether the fuse is still burning
- unsigned long lastUpdate = 0; // Time tracking for animation
- int fuseSpeed = 150; // Milliseconds between each step (adjust for speed)
- // Color definitions
- CRGB fuseColor = CRGB::Orange; // Active burning point
- CRGB emberColor = CRGB::Red; // Hot ember behind the fuse
- CRGB coolEmberColor = CRGB(64, 16, 0); // Cooling ember
- CRGB deadColor = CRGB::Black; // Burned out section
- void setup() {
- Serial.begin(9600);
- // Initialize FastLED
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(200);
- // Initialize all LEDs to represent an unlit fuse (dim yellow/brown)
- for(int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB(40, 25, 0); // Unlit fuse color
- }
- FastLED.show();
- Serial.println("Fuse burning simulation started!");
- Serial.println("Send 'r' to restart the fuse");
- }
- void loop() {
- // Check for serial input to restart
- if(Serial.available() > 0) {
- char input = Serial.read();
- if(input == 'r' || input == 'R') {
- restartFuse();
- }
- }
- // Update fuse animation
- if(fuseActive && millis() - lastUpdate >= fuseSpeed) {
- updateFuse();
- lastUpdate = millis();
- }
- // Add some flickering to the active burning point
- if(fuseActive && fusePosition < NUM_LEDS) {
- addFlicker();
- }
- FastLED.show();
- delay(20); // Small delay for smooth animation
- }
- void updateFuse() {
- if(fusePosition >= NUM_LEDS) {
- fuseActive = false;
- Serial.println("BOOM! Fuse has burned completely!");
- // Optional: Add explosion effect
- explosionEffect();
- return;
- }
- // Set the current burning position
- leds[fusePosition] = fuseColor;
- // Update the trail behind the fuse
- updateTrail();
- // Move to next position
- fusePosition++;
- Serial.print("Fuse burning at position: ");
- Serial.println(fusePosition);
- }
- void updateTrail() {
- // Update the burning trail behind the active fuse
- for(int i = 0; i < fusePosition; i++) {
- int distanceFromFuse = fusePosition - i;
- if(distanceFromFuse == 1) {
- // Hot ember right behind the fuse
- leds[i] = emberColor;
- }
- else if(distanceFromFuse <= 3) {
- // Cooling embers
- int brightness = map(distanceFromFuse, 2, 3, 128, 32);
- leds[i] = CRGB(brightness, brightness/4, 0);
- }
- else if(distanceFromFuse <= 6) {
- // Cool embers
- int brightness = map(distanceFromFuse, 4, 6, 32, 8);
- leds[i] = CRGB(brightness, brightness/8, 0);
- }
- else {
- // Completely burned out
- leds[i] = deadColor;
- }
- }
- }
- void addFlicker() {
- if(fusePosition < NUM_LEDS) {
- // Add random flickering to the burning point
- int flicker = random(-30, 30);
- CRGB flickerColor = fuseColor;
- flickerColor.r = constrain(flickerColor.r + flicker, 0, 255);
- flickerColor.g = constrain(flickerColor.g + flicker/2, 0, 255);
- flickerColor.b = constrain(flickerColor.b + flicker/4, 0, 255);
- leds[fusePosition] = flickerColor;
- // Occasionally spark ahead
- if(random(100) < 5 && fusePosition + 1 < NUM_LEDS) {
- leds[fusePosition + 1] = CRGB(255, 100, 0);
- }
- }
- }
- void explosionEffect() {
- // Simple explosion effect at the end
- for(int brightness = 0; brightness < 255; brightness += 10) {
- for(int i = 0; i < NUM_LEDS; i++) {
- // Distance-based explosion effect
- int distance = abs(i - (NUM_LEDS - 1));
- int explosionBrightness = max(0, brightness - (distance * 8));
- leds[i] = CRGB(explosionBrightness, explosionBrightness/2, 0);
- }
- FastLED.show();
- delay(30);
- }
- // Fade out
- for(int brightness = 255; brightness >= 0; brightness -= 5) {
- for(int i = 0; i < NUM_LEDS; i++) {
- leds[i].fadeToBlackBy(5);
- }
- FastLED.show();
- delay(20);
- }
- }
- void restartFuse() {
- Serial.println("Restarting fuse...");
- fusePosition = 0;
- fuseActive = true;
- lastUpdate = 0;
- // Reset all LEDs to unlit fuse
- for(int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB(40, 25, 0); // Unlit fuse color
- }
- FastLED.show();
- }
- // Alternative function: Reverse burning (from end to start)
- void startReverseFuse() {
- fusePosition = NUM_LEDS - 1;
- fuseActive = true;
- fuseSpeed = -fuseSpeed; // Negative speed for reverse direction
- }
- // Alternative function: Random spark ignition
- void randomSpark() {
- int sparkPos = random(NUM_LEDS);
- leds[sparkPos] = CRGB::White;
- FastLED.show();
- delay(100);
- leds[sparkPos] = CRGB(40, 25, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment