Advertisement
jedimasta

Untitled

Nov 13th, 2017
29
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This #include statement was automatically added by the Particle IDE.
  2. #include <FastLED.h>
  3. #include "Particle.h"
  4.  
  5. FASTLED_USING_NAMESPACE;
  6.  
  7. #define LED_PIN     4
  8. #define COLOR_ORDER GRB
  9. #define CHIPSET     WS2811
  10. #define NUM_LEDS    301 //88 for testing, 300 for prodction
  11. #define BRIGHTNESS  50
  12. #define FRAMES_PER_SECOND 30
  13.  
  14. bool gReverseDirection = false;
  15. unsigned long previousMillis = 0;
  16. unsigned long currentMillis;
  17. unsigned long twinkleMillis = 0;
  18. int eepValue;
  19. int eepAddr = 2;
  20. int mode;
  21. int maxShells = round(NUM_LEDS / 30);
  22. int lifespan = 30;
  23. long totalModes = 5;
  24. int changeToColor(String newColor);
  25. uint32_t currentColor=0x000000;
  26.  
  27. NSFastLED::CRGB leds[NUM_LEDS];
  28. CRGBPalette16 gPal;
  29.  
  30. void runColor(String currentColor);
  31. void runRainbow();
  32. void twinkle();
  33. void fireworks();
  34. void runPalette(NSFastLED::CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette);
  35.  
  36.  
  37. class FireworkShell {
  38.     int stringPos;    //origin of the blast
  39.     int age;          //how long the blast has been on the string
  40.     int maxLife;      //how old this one can get
  41.     uint8_t hue;       //color of the shell
  42.  
  43.   public:
  44.     FireworkShell(int xPos, int howOld, int deathAge, uint8_t shellColor) {
  45.       stringPos = xPos;
  46.       age = howOld;
  47.       maxLife = deathAge;
  48.       hue = shellColor;
  49.     }
  50.  
  51.     void Update() {
  52.  
  53.       if (age < maxLife) {
  54.         int newPosR = stringPos + age;
  55.         int newPosL = stringPos - age;
  56.         if (newPosR < NUM_LEDS) leds[newPosR] +=  CHSV(hue, 255, 255);
  57.         if (newPosL >= 0) leds[newPosL] +=  CHSV(hue, 255, 255);
  58.  
  59.         if (age > 0) {
  60.  
  61.  
  62.           int tailR = newPosR - 1;
  63.           int tailL = newPosL + 1;
  64.           if (tailR > 0 && tailR < NUM_LEDS) {
  65.             leds[tailR] = CHSV(hue + 40, 255, random(50, 200));
  66.           }
  67.           if (tailL > 0 && tailL < NUM_LEDS) {
  68.             leds[tailL] = CHSV(hue + 40, 255,  random(50, 200));
  69.           }
  70.  
  71.  
  72.  
  73.         }
  74.  
  75.       }
  76.       if (age < maxLife + 20) {
  77.         age++;
  78.       } else {
  79.         age = 0;
  80.         hue = random(0, 213);
  81.         maxLife = random(5, lifespan);
  82.         stringPos =  random(0, NUM_LEDS);
  83.         //Serial.println(maxLife);
  84.       }
  85.     }
  86. };
  87.  
  88. FireworkShell shells[] = {
  89.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  90.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  91.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  92.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  93.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  94.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  95.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  96.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  97.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan), random(0, 255)),
  98.   FireworkShell(random(0, NUM_LEDS), 0, random(5, lifespan),  random(0, 255))
  99. };
  100.  
  101.  
  102. void setup() {
  103.   delay(3000); // sanity delay
  104.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  105.   FastLED.setBrightness( BRIGHTNESS );
  106.   //FastLED.setMaxPowerInVoltsAndMilliamps(5, 1500);
  107.   randomSeed(analogRead(0));
  108.   //Serial.begin(9600);
  109.  
  110.   Particle.variable("mode",mode);
  111.   Particle.function("updateMode", modeChange);
  112.  
  113.   Particle.function("changeColor", changeToColor);
  114.   Particle.variable("newColor", currentColor);
  115.    
  116.     EEPROM.get(eepAddr, eepValue);
  117.     if(eepValue == 0xFFFF) {
  118.         // EEPROM was empty -> initialize value
  119.         mode = 0;
  120.     }else{
  121.         mode = eepValue;
  122.     }
  123.  
  124.   //mode = 0;
  125. }
  126.  
  127.  
  128. extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
  129.  
  130. void loop()
  131. {
  132.   currentMillis = millis();
  133.   //Particle.publish("Mode is ",  String (mode));
  134.  
  135.   switch (mode) {
  136.  
  137.     case 0:
  138.       runRainbow();
  139.      twinkle();
  140.       break;
  141.  
  142.     case 1:
  143.       //runColor(CRGB::Blue);
  144.       runPalette(leds, NUM_LEDS,  gGradientPalettes[0]);
  145.       twinkle();
  146.       break;
  147.  
  148.     case 2:
  149.       runPalette(leds, NUM_LEDS,  gGradientPalettes[1]);
  150.       twinkle();
  151.       break;
  152.      
  153.     case 3:
  154.       runPalette(leds, NUM_LEDS,  gGradientPalettes[2]);
  155.       twinkle();
  156.       break;
  157.      
  158.     case 4:
  159.       runPalette(leds, NUM_LEDS,  gGradientPalettes[3]);
  160.       twinkle();
  161.       break;
  162.      
  163.     case 5:
  164.       runPalette(leds, NUM_LEDS,  gGradientPalettes[4]);
  165.       twinkle();
  166.       break;
  167.      
  168.     case 6:
  169.         fireworks();
  170.         break;
  171.        
  172.     case 7:
  173.        runPalette(leds, NUM_LEDS,  gGradientPalettes[5]);
  174.       twinkle();
  175.      
  176.     case 8:
  177.        runPalette(leds, NUM_LEDS,  gGradientPalettes[6]);
  178.       twinkle();
  179.      
  180.     case 9:
  181.        runPalette(leds, NUM_LEDS,  gGradientPalettes[7]);
  182.       twinkle();
  183.      
  184.     case 10:
  185.         runColor(currentColor);
  186.         twinkle();
  187.         break;
  188.   }
  189. }
  190.  
  191.  
  192. int changeToColor(String newColor){
  193.     Particle.publish("Color received is ",  String (newColor));
  194.     currentColor = strtol(newColor, NULL, 16);
  195.     Particle.publish("Hex received is ",  currentColor);
  196. }
  197.  
  198. int modeChange(String newMode){
  199.     int modeNum = newMode.toInt();
  200.     mode = modeNum;
  201.     EEPROM.put(eepAddr, modeNum);
  202. }
  203.  
  204.  
  205.  
  206.  
  207. void runColor(uint32_t currentColor) {
  208.   if (currentMillis - previousMillis >= 200) {
  209.     previousMillis = currentMillis;
  210.     fill_solid(leds, NUM_LEDS, currentColor);
  211.   }
  212. }
  213.  
  214. void runRainbow() {
  215.   if (currentMillis - previousMillis >= 30) {
  216.     previousMillis = currentMillis;
  217.     static uint8_t hue = 0;
  218.     fill_rainbow(leds, NUM_LEDS, hue++);
  219.     FastLED.show();
  220.   }
  221. }
  222.  
  223. void twinkle() {
  224.   //create random twinkle
  225.   int rp = random(500,2000);
  226.   if (currentMillis - twinkleMillis >= rp) {
  227.     twinkleMillis = currentMillis;
  228.     int pixel = random(NUM_LEDS);
  229.     leds[random(NUM_LEDS)] = CRGB::White;
  230.     FastLED.show();
  231.   }
  232. }
  233.  
  234. void fireworks() {
  235.  EVERY_N_MILLISECONDS(30){
  236.     fadeToBlackBy(leds, NUM_LEDS, 30);
  237.     Serial.println(maxShells);
  238.  
  239.     for (int i = 0; i < maxShells; i++) {
  240.       shells[i].Update();
  241.     }
  242.     FastLED.show();
  243.   };
  244. }
  245.  
  246.  
  247. void runPalette(NSFastLED::CRGB* ledarray, uint16_t numleds, const CRGBPalette16& gCurrentPalette)
  248. {
  249.   if (currentMillis - previousMillis >= 30) {
  250.     previousMillis = currentMillis;
  251.     static uint8_t startindex = 0;
  252.     startindex--;
  253.     fill_palette( ledarray, numleds, startindex, (256 / NUM_LEDS) + 1, gCurrentPalette, BRIGHTNESS, LINEARBLEND);
  254.     FastLED.show();
  255.   }
  256. }
  257.  
  258. DEFINE_GRADIENT_PALETTE( holly_gp) {
  259.   0,    0,  255,  0,  //green
  260.   48,   0,  255,  0,  //green
  261.   49,   255,  0,  0,  //red
  262.   64,   255,  0,  0,  //red
  263.   65,   0,  255,  0,  //green
  264.   114,   0,  255,  0,  //green
  265.   115,   255,  0,  0,  //red
  266.   118,   255,  0,  0,  //red
  267.   119,   0,  255,  0,  //green
  268.   168,  0,  255,  0,  //green
  269.   169,  255,  0,  0,  //red
  270.   184,  255,  0,  0,  //red
  271.   185,  0,  255,  0,  //green
  272.   234,  0,  255,  0,  //green
  273.   235,  255,  0,  0,  //red
  274.   255,  255,  0,  0   //red
  275. };
  276.  
  277. DEFINE_GRADIENT_PALETTE( candycane_gp) {
  278.   0 , 128, 128, 128,  //white
  279.   32 , 128, 128, 128,  //white
  280.   33 , 255, 0, 0,  //red
  281.   66 , 255, 0, 0,  //red
  282.   67 , 128, 128, 128,  //white
  283.   100 , 128, 128, 128,  //white
  284.   101 , 255, 0, 0,  //red
  285.   134 , 255, 0, 0,  //red
  286.   135 , 128, 128, 128,  //white
  287.   168 , 128, 128, 128,  //white
  288.   169 , 255, 0, 0,  //red
  289.   202 , 255, 0, 0,  //red
  290.   203 , 128, 128, 128,  //white
  291.   236 , 128, 128, 128,  //white
  292.   237 , 255, 0, 0,  //red
  293.   255 , 255, 0, 0  //red
  294. };
  295.  
  296. DEFINE_GRADIENT_PALETTE( snowynight_gp) {
  297.   0, 163, 182, 199,
  298.   41,  188, 192, 200,
  299.   117,  117, 157, 240,
  300.   204,  117, 224, 240,
  301.   255,  163, 182, 199
  302. };
  303.  
  304. DEFINE_GRADIENT_PALETTE( silvergold_gp) {
  305.     0, 237,121,  1,
  306.    25, 237,121,  1,
  307.    51, 229,149,  1,
  308.    76, 222,178,  1,
  309.   102, 237,215, 59,
  310.   127, 255,255,255,
  311.   153, 118,164,188,
  312.   178,  46,101,145,
  313.   204,  19, 60, 95,
  314.   229,   4, 31, 56,
  315.   255,   4, 31, 56
  316.   };
  317.  
  318. // Gradient palette "es_autumn_04_gp", originally from
  319. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/es/autumn/tn/es_autumn_04.png.index.html
  320. // converted for FastLED with gammas (2.6, 2.2, 2.5)
  321. // Size: 20 bytes of program space.
  322.  
  323. DEFINE_GRADIENT_PALETTE( autumn_gp ) {
  324.     0,  71,  135,  0,
  325.   101,  88,  1,  0,
  326.   165, 210, 22,  1,
  327.   234, 255,166, 42,
  328.   255, 255,166, 42
  329.   };
  330.  
  331.   // Gradient palette "grand_old_flag_gp", originally from
  332. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/pj/3/tn/grand-old-flag.png.index.html
  333. // converted for FastLED with gammas (2.6, 2.2, 2.5)
  334. // Size: 128 bytes of program space.
  335.  
  336. DEFINE_GRADIENT_PALETTE( grand_old_flag_gp ) {
  337.     0,   1,  2,105,
  338.    94,   1,  2,105,
  339.    99, 199,  1,  7,
  340.   104, 199,  1,  7,
  341.   109, 255,255,255,
  342.   117, 255,255,255,
  343.   122, 199,  1,  7,
  344.   124, 199,  1,  7,
  345.   130, 255,255,255,
  346.   137, 255,255,255,
  347.   145, 199,  1,  7,
  348.   150, 199,  1,  7,
  349.   155, 255,255,255,
  350.   160, 255,255,255,
  351.   165, 199,  1,  7,
  352.   170, 199,  1,  7,
  353.   175, 255,255,255,
  354.   181, 255,255,255,
  355.   188, 199,  1,  7,
  356.   191, 199,  1,  7,
  357.   198, 255,255,255,
  358.   204, 255,255,255,
  359.   209, 199,  1,  7,
  360.   211, 199,  1,  7,
  361.   219, 255,255,255,
  362.   224, 255,255,255,
  363.   229, 199,  1,  7,
  364.   234, 199,  1,  7,
  365.   239, 255,255,255,
  366.   244, 255,255,255,
  367.   249, 199,  1,  7,
  368.   255, 199,  1,  7
  369.   };
  370.  
  371.   // Gradient palette "christmas_candy_gp", originally from
  372. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/ocal/tn/christmas-candy.png.index.html
  373. // converted for FastLED with gammas (2.6, 2.2, 2.5)
  374. // Size: 44 bytes of program space.
  375.  
  376. DEFINE_GRADIENT_PALETTE( christmas_candy_gp ) {
  377.     0, 255,255,255,
  378.    25, 255,  0,  0,
  379.    51, 255,255,255,
  380.    76,   0, 55,  0,
  381.   102, 255,255,255,
  382.   127, 255,  0,  0,
  383.   153, 255,255,255,
  384.   178,   0, 55,  0,
  385.   204, 255,255,255,
  386.   229, 255,  0,  0,
  387.   255, 255,255,255
  388.   };
  389.  
  390. // Gradient palette "green_purple_gp", originally from
  391. // http://soliton.vm.bytemark.co.uk/pub/cpt-city/km/tn/green-purple.png.index.html
  392. // converted for FastLED with gammas (2.6, 2.2, 2.5)
  393. // Size: 260 bytes of program space.
  394.  
  395. DEFINE_GRADIENT_PALETTE( green_purple_gp ) {
  396.       0, 255, 155,  0,
  397.      40, 255, 155,  0,
  398.      49,   0,   0,  0,
  399.      96,   0,   0,  0,
  400.     107,   0, 254,  0,
  401.     179,   0, 254,  0,
  402.     183, 203,   0,255,
  403.     255, 203,   0,255
  404.     };
  405.  
  406.  
  407.  
  408.  
  409. const TProgmemRGBGradientPalettePtr gGradientPalettes[] = {
  410.   holly_gp,
  411.   candycane_gp,
  412.   snowynight_gp,
  413.   silvergold_gp,
  414.   autumn_gp,
  415.   grand_old_flag_gp,
  416.   christmas_candy_gp,
  417.   green_purple_gp,
  418. };
Advertisement
RAW Paste Data Copied
Advertisement