Advertisement
nathancarter

Jacket saber+fire+button FIXED

Oct 1st, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 KB | None | 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 SABER_NUM_LEDS 45
  9. #define SABER_PIN 18
  10. #define SABER_COLOR 0x44b0db // use RGB (hex) color picker
  11. #define SABER_SPEED 8  //ms delay between lights, higher is slower
  12. #define SABER_PAUSE_TOP 3000
  13. #define SABER_PAUSE_BOTTOM 1000
  14. #define SABER_BRIGHTNESS_NOMINAL 8
  15. #define SABER_BRIGHTNESS_MAX 128
  16.  
  17. #define FIRE_TOTAL_LEDS 55
  18. #define FIRE_PIN 19
  19. #define FIRE_BRIGHTNESS_NOMINAL 8
  20. #define FIRE_BRIGHTNESS_MAX 255
  21. #define FIRE_SPEED 75// ms between fire refresh rate, higher is slower
  22. #define COOLING 70
  23. #define SPARKING 270
  24. #define FIRE_ROW_LEDS 11
  25. #define FIRE_ROW_COUNT 5
  26.  
  27. #define BRIGHTBUTTON_PIN 17
  28. #define BRIGHT_DELAY 10000 // how many ms to stay in bright mode after button is pressed
  29.  
  30. Adafruit_NeoPixel SaberStrip = Adafruit_NeoPixel(SABER_NUM_LEDS, SABER_PIN, NEO_GRB + NEO_KHZ800);
  31. Adafruit_NeoPixel FireStrip = Adafruit_NeoPixel(FIRE_TOTAL_LEDS, FIRE_PIN, NEO_GRB + NEO_KHZ800);
  32.  
  33. Button BrightButton(BRIGHTBUTTON_PIN);       // define the button that increases brightness
  34.  
  35. unsigned long TimerMaster = 0; // master clock timer
  36. unsigned long TimerSaber = 0; // saber loop timer
  37. unsigned long TimerFire = 0; // fire update timer
  38. unsigned long TimerBright = 0; // bright-mode timer
  39. int SaberLoopCounter = SABER_NUM_LEDS; // loop counter for Saber
  40. bool SaberToggle = 0; // wipe to color or wipe to off
  41. int SaberBrightness = SABER_BRIGHTNESS_NOMINAL; // brightness value to be changed on button press
  42. int FireBrightness = FIRE_BRIGHTNESS_NOMINAL; // brightness value to be changed on button press
  43.  
  44. byte heat[FIRE_TOTAL_LEDS]; // global array of heat values for all five rows
  45.  
  46. /*
  47.   // trying the saber code on the fire strip
  48.   // to rule out hardware problems,
  49.   unsigned long TimerSaber2 = 0; // saber loop timer
  50.   int SaberLoopCounter2 = 0; // loop counter for Saber
  51.   bool SaberToggle2 = 0; // wipe to color or wipe to off
  52.   #define SABER_NUM_LEDS2 55
  53.   #define SABER_COLOR2 0xFFb0db // use RGB (hex) color picker
  54.   #define SABER_SPEED2 3  //ms delay between lights, higher is slower
  55. */
  56.  
  57. void setup()
  58. {
  59.   delay(1500); // sanity delay
  60.   SaberStrip.begin(); // initialize saber strip
  61.   SaberStrip.setBrightness(SaberBrightness);
  62.   FireStrip.begin(); // initialize fire strip
  63.   FireStrip.setBrightness(FireBrightness);
  64.   BrightButton.begin();
  65. }
  66.  
  67. void loop()
  68. {
  69.   TimerMaster = millis();
  70.   BrightButton.read();
  71.   if (BrightButton.wasPressed())
  72.   {
  73.     // If the button was pressed, crank up the brightness for a certain amount of time
  74.     SaberBrightness = SABER_BRIGHTNESS_MAX;
  75.     FireBrightness = FIRE_BRIGHTNESS_MAX;
  76.     // And reset the saber to start at the bottom
  77.     for (int i = 0; i < SABER_NUM_LEDS; i++)
  78.     {
  79.       SaberStrip.setPixelColor(i, 0);
  80.     }
  81.     SaberLoopCounter = SABER_NUM_LEDS;
  82.     SaberToggle = 0;
  83.     TimerSaber = TimerMaster;
  84.     SaberStrip.show();
  85.     FireStrip.show();
  86.     TimerBright = (TimerMaster + BRIGHT_DELAY);
  87.   }
  88.  
  89.   if (TimerMaster >= TimerBright)  // If the brightness timer has aged out
  90.   {
  91.     // Reset both Fire and Saber brightness to nominal values
  92.     SaberBrightness = SABER_BRIGHTNESS_NOMINAL;
  93.     FireBrightness = FIRE_BRIGHTNESS_NOMINAL;
  94.   }
  95.  
  96.  
  97.   if (TimerMaster >= TimerSaber)
  98.   {
  99.     SaberStrip.setBrightness(SaberBrightness);
  100.     saberPower();
  101.   }
  102.  
  103.   if (TimerMaster >= TimerFire)
  104.   {
  105.     FireStrip.setBrightness(FireBrightness);
  106.     Fire2012(0, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  107.     Fire2012(11, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  108.     Fire2012(22, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  109.     Fire2012(33, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  110.     Fire2012(44, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  111.     TimerFire = (TimerMaster + FIRE_SPEED);
  112.     FireStrip.show();
  113.   }
  114. }
  115.  
  116.  
  117. void saberPower()
  118. {
  119.  
  120.   // Check to see if we're turning on (0) or turning off (1)
  121.   if (SaberToggle == 0)
  122.   {
  123.     // Count from top to bottom, turning each one on
  124.     // Wait for SABER_SPEED ms before turning the next one on
  125.     if (SaberLoopCounter >= 0)
  126.     {
  127.       SaberStrip.setPixelColor(SaberLoopCounter, SABER_COLOR);
  128.       SaberStrip.show();
  129.       TimerSaber = (TimerMaster + SABER_SPEED);
  130.       SaberLoopCounter--;
  131.     }
  132.     // When top is reached, toggle the on/off
  133.     // Then wait for SABER_PAUSE_TOP ms before turning off
  134.     else
  135.     {
  136.       SaberToggle = !SaberToggle;
  137.       TimerSaber = (TimerMaster + SABER_PAUSE_TOP);
  138.     }
  139.   }
  140.   else
  141.   {
  142.     // Count from top to bottom, turning each one off
  143.     // Wait for SABER_SPEED ms before turning the next one off
  144.     if (SaberLoopCounter < SABER_NUM_LEDS)
  145.     {
  146.       SaberLoopCounter++;
  147.       SaberStrip.setPixelColor(SaberLoopCounter, 0);
  148.       SaberStrip.show();
  149.       TimerSaber = (TimerMaster + SABER_SPEED);
  150.  
  151.     }
  152.     // When top is reached, toggle the on/off
  153.     // Then wait for SABER_PAUSE_TOP ms before turning off
  154.     else
  155.     {
  156.       SaberToggle = !SaberToggle;
  157.       TimerSaber = (TimerMaster + SABER_PAUSE_BOTTOM);
  158.     }
  159.   }
  160. }
  161.  
  162.  
  163. void Fire2012(int LED_START_NUM, bool gReverseDirection, int cool, int spark)
  164. {
  165.   // Step 1.  Cool down every cell a little
  166.   for ( int i = LED_START_NUM; i < (FIRE_ROW_LEDS + LED_START_NUM); i++)
  167.   {
  168.     heat[i] = qsub8( heat[i],  random8(0, ((cool * 10) / FIRE_ROW_LEDS) + 2));
  169.   }
  170.  
  171.   // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  172.   for ( int k = (FIRE_ROW_LEDS + LED_START_NUM) - 1; k >= (LED_START_NUM+2); k--)
  173.   {
  174.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  175.   }
  176.  
  177.   // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  178.   if ( random8() < spark )
  179.   {
  180.     int y = random8(3);
  181.     heat[LED_START_NUM + y] = qadd8( heat[LED_START_NUM + y], random8(160, 255) );
  182.   }
  183.  
  184.   // Step 4.  Map from heat cells to LED colors
  185.   for ( int j = LED_START_NUM; j < (FIRE_ROW_LEDS + LED_START_NUM); j++)
  186.   {
  187.     CRGB color = HeatColor(min(150, heat[j]));
  188.     int pixelnumber;
  189.     if ( gReverseDirection )
  190.     {
  191.       pixelnumber = ((FIRE_ROW_LEDS + LED_START_NUM) - 1) - (j - LED_START_NUM);
  192.     }
  193.     else
  194.     {
  195.       pixelnumber = j;
  196.     }
  197.     FireStrip.setPixelColor(pixelnumber, color.r, color.g, color.b);
  198.   }
  199. }
  200.  
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement