Advertisement
nathancarter

Jacket saber+fire w-button BROKEN

Sep 30th, 2020 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.08 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 255
  16.  
  17. #define FIRE_TOTAL_LEDS 55
  18. #define FIRE_PIN 19
  19. #define FIRE_BRIGHTNESS_NOMINAL 127
  20. #define FIRE_BRIGHTNESS_MAX 255
  21. #define FIRE_SPEED 500// 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 5000 // 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 = 0; // 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.     TimerBright = (TimerMaster + BRIGHT_DELAY);
  77.   }
  78.  
  79.   if (TimerMaster >= TimerBright)  // If the brightness timer has aged out
  80.   {
  81.     // Reset both Fire and Saber brightness to nominal values
  82.     SaberBrightness = SABER_BRIGHTNESS_NOMINAL;
  83.     FireBrightness = FIRE_BRIGHTNESS_NOMINAL;
  84.   }
  85.  
  86.  
  87.   if (TimerMaster >= TimerSaber)
  88.   {
  89.     SaberStrip.setBrightness(SaberBrightness);
  90.     saberPower();
  91.   }
  92.  
  93.   if (TimerMaster >= TimerFire)
  94.   {
  95.     FireStrip.setBrightness(FireBrightness);
  96.     //    saberPower2();
  97.     Fire2012(0, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  98.     //    Fire2012(11, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  99.     //    Fire2012(22, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  100.     //    Fire2012(33, 1, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  101.     //   Fire2012(44, 0, random(COOLING * 0.8, COOLING * 1.2), random(SPARKING * 0.8, SPARKING * 1.2));
  102.     TimerFire = (TimerMaster + FIRE_SPEED);
  103.     FireStrip.show();
  104.   }
  105. }
  106.  
  107.  
  108. void saberPower()
  109. {
  110.  
  111.   // Check to see if we're turning on (0) or turning off (1)
  112.   if (SaberToggle == 0)
  113.   {
  114.     // Count from bottom to top, turning each one on
  115.     // Wait for SABER_SPEED ms before turning the next one on
  116.     if (SaberLoopCounter < SABER_NUM_LEDS)
  117.     {
  118.       SaberStrip.setPixelColor(SaberLoopCounter, SABER_COLOR);
  119.       SaberStrip.show();
  120.       TimerSaber = (TimerMaster + SABER_SPEED);
  121.       SaberLoopCounter++;
  122.     }
  123.     // When top is reached, toggle the on/off
  124.     // Then wait for SABER_PAUSE_TOP ms before turning off
  125.     else
  126.     {
  127.       SaberToggle = !SaberToggle;
  128.       TimerSaber = (TimerMaster + SABER_PAUSE_TOP);
  129.     }
  130.   }
  131.   else
  132.   {
  133.     // Count from top to bottom, turning each one off
  134.     // Wait for SABER_SPEED ms before turning the next one off
  135.     if (SaberLoopCounter > 0)
  136.     {
  137.       SaberLoopCounter--;
  138.       SaberStrip.setPixelColor(SaberLoopCounter, 0);
  139.       SaberStrip.show();
  140.       TimerSaber = (TimerMaster + SABER_SPEED);
  141.  
  142.     }
  143.     // When top is reached, toggle the on/off
  144.     // Then wait for SABER_PAUSE_TOP ms before turning off
  145.     else
  146.     {
  147.       SaberToggle = !SaberToggle;
  148.       TimerSaber = (TimerMaster + SABER_PAUSE_BOTTOM);
  149.     }
  150.   }
  151. }
  152. /*
  153.   void saberPower2()
  154.   {
  155.  
  156.   // Check to see if we're turning on (0) or turning off (1)
  157.   if (SaberToggle2 == 0)
  158.   {
  159.     // Count from bottom to top, turning each one on
  160.     // Wait for SABER_SPEED ms before turning the next one on
  161.     if (SaberLoopCounter2 < SABER_NUM_LEDS2)
  162.     {
  163.       FireStrip.setPixelColor(SaberLoopCounter2, SABER_COLOR2);
  164.       FireStrip.show();
  165.       TimerSaber2 = (TimerMaster + SABER_SPEED2);
  166.       SaberLoopCounter2++;
  167.     }
  168.     // When top is reached, toggle the on/off
  169.     // Then wait for SABER_PAUSE_TOP ms before turning off
  170.     else
  171.     {
  172.       SaberToggle2 = !SaberToggle2;
  173.       TimerSaber2 = (TimerMaster + SABER_PAUSE_TOP);
  174.     }
  175.   }
  176.   else
  177.   {
  178.     // Count from top to bottom, turning each one off
  179.     // Wait for SABER_SPEED ms before turning the next one off
  180.     if (SaberLoopCounter2 > 0)
  181.     {
  182.       SaberLoopCounter2--;
  183.       FireStrip.setPixelColor(SaberLoopCounter2, 0);
  184.       FireStrip.show();
  185.       TimerSaber2 = (TimerMaster + SABER_SPEED2);
  186.  
  187.     }
  188.     // When top is reached, toggle the on/off
  189.     // Then wait for SABER_PAUSE_TOP ms before turning off
  190.     else
  191.     {
  192.       SaberToggle2 = !SaberToggle2;
  193.       TimerSaber2 = (TimerMaster + SABER_PAUSE_BOTTOM);
  194.     }
  195.   }
  196.   }
  197. */
  198. void Fire2012(int LED_START_NUM, bool gReverseDirection, int cool, int spark)
  199. {
  200.  
  201.   // just set it all to a fixed value to see if anything will even light up
  202.   for ( int i = 0; i < FIRE_TOTAL_LEDS; i++)
  203.   {
  204.     heat[i] = 128;
  205.   }
  206.  
  207.   for ( int j = 0; j < FIRE_TOTAL_LEDS; j++)
  208.   {
  209.     FireStrip.setPixelColor(j, HeatColor(min(150, heat[j])));
  210.   }
  211.   FireStrip.show();
  212.  
  213. }
  214.  
  215. /*
  216.   void Fire2012(int LED_START_NUM, bool gReverseDirection, int cool, int spark)
  217.   {
  218.   // Array of temperature readings at each simulation cell
  219.   // static byte heat[FIRE_ROW_LEDS];
  220.   //  randomSeed(analogRead(0));
  221.   // Step 1.  Cool down every cell a little
  222.   for ( int i = LED_START_NUM; i < (FIRE_ROW_LEDS + LED_START_NUM); i++)
  223.   {
  224.     heat[i] = qsub8( heat[i],  random8(0, ((cool * 10) / FIRE_ROW_LEDS) + 2));
  225.   }
  226.  
  227.   // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  228.   for ( int k = FIRE_ROW_LEDS - 1; k >= 2; k--)
  229.   {
  230.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  231.   }
  232.  
  233.   // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  234.   if ( random8() < spark )
  235.   {
  236.     int y = random8(2);
  237.     heat[LED_START_NUM + y] = qadd8( heat[LED_START_NUM + y], random8(160, 255) );
  238.   }
  239.  
  240.   // Step 4.  Map from heat cells to LED colors
  241.   for ( int j = 0; j < FIRE_TOTAL_LEDS; j++)
  242.   {
  243.     FireStrip.setPixelColor(j, HeatColor(min(150, heat[j])));
  244.   }
  245.   FireStrip.show();
  246. */
  247.  
  248.  
  249. /*
  250.   // Step 4.  Map from heat cells to LED colors
  251.   for ( int j = LED_START_NUM; j < (FIRE_ROW_LEDS + LED_START_NUM); j++)
  252.   {
  253.    CRGB color = HeatColor(min(150, heat[j - LED_START_NUM]));
  254.    int pixelnumber;
  255.    if ( gReverseDirection )
  256.    {
  257.      pixelnumber = ((FIRE_ROW_LEDS + LED_START_NUM) - 1) - (j - LED_START_NUM);
  258.    }
  259.    else
  260.    {
  261.      pixelnumber = j;
  262.    }
  263.    //firestrip[pixelnumber] = color;
  264.    FireStrip.setPixelColor(pixelnumber, color);
  265.    }
  266. */
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement