Advertisement
nathancarter

Jacket saber+fire

Sep 30th, 2020
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <FastLED.h>
  3.  
  4. #define COLOR_ORDER GRB
  5. #define CHIPSET     WS2812
  6.  
  7. #define SABER_NUM_LEDS 20
  8. #define SABER_PIN 18
  9. #define SABER_COLOR 0x44b0db // use RGB (hex) color picker
  10. #define SABER_SPEED 20  //ms delay between lights, higher is slower
  11. #define SABER_PAUSE_TOP 3000
  12. #define SABER_PAUSE_BOTTOM 1000
  13. #define SABER_BRIGHTNESS_NOMINAL 8
  14. #define SABER_BRIGHTNESS_MAX 255
  15.  
  16. #define FIRE_TOTAL_LEDS 55
  17. #define FIRE_PIN 19
  18. #define FIRE_BRIGHTNESS 200
  19. #define FIRE_SPEED 500// ms between fire refresh rate, higher is slower
  20. #define COOLING 70
  21. #define SPARKING 270
  22. #define FIRE_ROW_LEDS 11
  23. #define FIRE_ROW_COUNT 5
  24.  
  25. CRGB fireleds[FIRE_ROW_LEDS];
  26. CRGB firestrip[FIRE_TOTAL_LEDS];
  27. Adafruit_NeoPixel SaberStrip = Adafruit_NeoPixel(SABER_NUM_LEDS, SABER_PIN, NEO_GRB + NEO_KHZ800);
  28. //Adafruit_NeoPixel FireStrip = Adafruit_NeoPixel(FIRE_TOTAL_LEDS, FIRE_PIN, NEO_GRB + NEO_KHZ800);
  29.  
  30. unsigned long TimerMaster = 0; // master clock timer
  31. unsigned long TimerSaber = 0; // saber loop timer
  32. unsigned long TimerFire = 0; // fire update timer
  33. int SaberLoopCounter = 0; // loop counter for Saber
  34. bool SaberToggle = 0; // wipe to color or wipe to off
  35. int SaberBrightness = SABER_BRIGHTNESS_NOMINAL; // brightness value to be randomized
  36.  
  37.  
  38. void setup()
  39. {
  40.   delay(3000); // sanity delay
  41.   SaberStrip.begin(); // initialize saber strip
  42.   SaberStrip.setBrightness(SaberBrightness);
  43.   FastLED.addLeds<CHIPSET, FIRE_PIN, COLOR_ORDER>(firestrip, FIRE_TOTAL_LEDS).setCorrection( TypicalLEDStrip );
  44. }
  45.  
  46. void loop()
  47. {
  48.   TimerMaster = millis();
  49.   if (TimerMaster >= TimerSaber)
  50.   {
  51.     saberPower();
  52.   }
  53.   if (TimerMaster >= TimerFire)
  54.   {
  55.     Fire2012(0, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  56.     Fire2012(11, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  57.     Fire2012(22, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  58.     Fire2012(33, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  59.     Fire2012(44, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  60.     TimerFire = (TimerMaster + FIRE_SPEED);
  61.     FastLED.show();
  62.     //FireStrip.show();
  63.   }
  64. }
  65.  
  66.  
  67. void saberPower()
  68. {
  69.  
  70.   // Check to see if we're turning on (0) or turning off (1)
  71.   if (SaberToggle == 0)
  72.   {
  73.     // Count from bottom to top, turning each one on
  74.     // Wait for SABER_SPEED ms before turning the next one on
  75.     if (SaberLoopCounter < SABER_NUM_LEDS)
  76.     {
  77.       SaberStrip.setPixelColor(SaberLoopCounter, SABER_COLOR);
  78.       SaberStrip.show();
  79.       TimerSaber = (TimerMaster + SABER_SPEED);
  80.       SaberLoopCounter++;
  81.     }
  82.     // When top is reached, toggle the on/off
  83.     // Then wait for SABER_PAUSE_TOP ms before turning off
  84.     else
  85.     {
  86.       SaberToggle = !SaberToggle;
  87.       TimerSaber = (TimerMaster + SABER_PAUSE_TOP);
  88.     }
  89.   }
  90.   else
  91.   {
  92.     // Count from top to bottom, turning each one off
  93.     // Wait for SABER_SPEED ms before turning the next one off
  94.     if (SaberLoopCounter > 0)
  95.     {
  96.       SaberLoopCounter--;
  97.       SaberStrip.setPixelColor(SaberLoopCounter, 0);
  98.       SaberStrip.show();
  99.       TimerSaber = (TimerMaster + SABER_SPEED);
  100.  
  101.     }
  102.     // When top is reached, toggle the on/off
  103.     // Then wait for SABER_PAUSE_TOP ms before turning off
  104.     else
  105.     {
  106.       SaberToggle = !SaberToggle;
  107.       TimerSaber = (TimerMaster + SABER_PAUSE_BOTTOM);
  108.     }
  109.   }
  110. }
  111.  
  112.  
  113. void Fire2012(int LED_START_NUM, bool gReverseDirection, int cool, int spark)
  114. {
  115.   // Array of temperature readings at each simulation cell
  116.   static byte heat[FIRE_ROW_LEDS];
  117.   //  randomSeed(analogRead(0));
  118.   // Step 1.  Cool down every cell a little
  119.   for ( int i = 0; i < FIRE_ROW_LEDS; i++)
  120.   {
  121.     heat[i] = qsub8( heat[i],  random8(0, ((cool * 10) / FIRE_ROW_LEDS) + 2));
  122.   }
  123.  
  124.   // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  125.   for ( int k = FIRE_ROW_LEDS - 1; k >= 2; k--)
  126.   {
  127.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  128.   }
  129.  
  130.   // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  131.   if ( random8() < spark )
  132.   {
  133.     int y = random8(7);
  134.     heat[y] = qadd8( heat[y], random8(160, 255) );
  135.   }
  136.  
  137.   // Step 4.  Map from heat cells to LED colors
  138.   for ( int j = LED_START_NUM; j < (FIRE_ROW_LEDS + LED_START_NUM); j++)
  139.   {
  140.     CRGB color = HeatColor(min(150, heat[j - LED_START_NUM]));
  141.     int pixelnumber;
  142.     if ( gReverseDirection )
  143.     {
  144.       pixelnumber = ((FIRE_ROW_LEDS + LED_START_NUM) - 1) - (j - LED_START_NUM);
  145.     }
  146.     else
  147.     {
  148.       pixelnumber = j;
  149.     }
  150.     firestrip[pixelnumber] = color;
  151.     //FireStrip.setPixelColor(pixelnumber, color);
  152.   }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement