Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.47 KB | None | 0 0
  1. #include "FastLED.h"
  2. #define LEDstrips 28
  3. #define LEDper 22
  4. #define LEDtype WS2813
  5. #define LEDcorr TypicalSMD5050
  6. #define LEDdither 255     //try 0 to reduce flickering
  7. uint8_t LEDbright = 11;
  8. int LEDamps = 3000;
  9.  
  10. uint8_t hue[2];       // These are arrays because two patterns will overlap
  11. int rowbucket[2];     // Counters for effects that expand over time
  12. int colbucket[2];
  13. int countbucket[2];
  14.  
  15. uint8_t deffade = 5;      // Default fade value for the fader function
  16. uint8_t currfade = 0;
  17.  
  18. uint8_t patternum = 0;    // For keeping track of what patterns are running
  19. uint8_t oldpattern = 1;
  20. uint16_t crossct = 255;
  21.  
  22. // have 3 independent CRGBs - 2 for the sources and one for the output
  23. CRGB leds[LEDstrips][LEDper];                                 // There is probably a way to do this with only 2 CRGB objects
  24. CRGB leds2[LEDstrips][LEDper];
  25. CRGB leds3[LEDstrips][LEDper];
  26.  
  27. DEFINE_GRADIENT_PALETTE( startup ){ //RYGCB
  28.   0,   255,  0,  0,      //red
  29.   51,   255,  0,  0,
  30.   52,   255,  255,  0,    //yellow
  31.   102,  255,  255,  0,    
  32.   103,  0,  255,  0,      //green
  33.   153,  0,  255,  0,
  34.   154,  0,  255,  255,    //cyan
  35.   205,  0,  255,  255,
  36.   206,  0,  0,  255,      //blue
  37.   255,  0,  0,  255};
  38.  
  39. DEFINE_GRADIENT_PALETTE( tropicana ){
  40.     0,    0, 0, 0,
  41.   71, 66, 245, 230,
  42.   145, 255, 255, 255,
  43.   255,  245, 66, 203};
  44.  
  45. //Pattern Key
  46. typedef void (*SimplePatternList[])();
  47. uint8_t PATTERNnum = 0; // Index number of which pattern is current
  48. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  49.  
  50. void setup() {
  51.   delay(500);
  52.   // Initialize LEDs
  53.    FastLED.setMaxPowerInVoltsAndMilliamps(5,LEDamps); //play with the order here
  54.    //FastLED.setTemperature();
  55.    FastLED.setBrightness(LEDbright);
  56.    FastLED.setDither(LEDdither);
  57.    FastLED.addLeds<LEDtype, PIN_C4, GRB>(leds[0], LEDper).setCorrection(LEDcorr);       // I'm switching to paralell output soon :)
  58.    FastLED.addLeds<LEDtype, PIN_C3, GRB>(leds[1], LEDper).setCorrection(LEDcorr);
  59.    FastLED.addLeds<LEDtype, PIN_C2, GRB>(leds[2], LEDper).setCorrection(LEDcorr);
  60.    FastLED.addLeds<LEDtype, PIN_C1, GRB>(leds[3], LEDper).setCorrection(LEDcorr);
  61.    FastLED.addLeds<LEDtype, PIN_C0, GRB>(leds[4], LEDper).setCorrection(LEDcorr);
  62.    FastLED.addLeds<LEDtype, PIN_E1, GRB>(leds[5], LEDper).setCorrection(LEDcorr);
  63.    FastLED.addLeds<LEDtype, PIN_E0, GRB>(leds[6], LEDper).setCorrection(LEDcorr);
  64.    FastLED.addLeds<LEDtype, PIN_D7, GRB>(leds[7], LEDper).setCorrection(LEDcorr);
  65.    FastLED.addLeds<LEDtype, PIN_D6, GRB>(leds[8], LEDper).setCorrection(LEDcorr);
  66.    FastLED.addLeds<LEDtype, PIN_D5, GRB>(leds[9], LEDper).setCorrection(LEDcorr);
  67.    FastLED.addLeds<LEDtype, PIN_D4, GRB>(leds[10], LEDper).setCorrection(LEDcorr);
  68.    FastLED.addLeds<LEDtype, PIN_D1, GRB>(leds[11], LEDper).setCorrection(LEDcorr);
  69.    FastLED.addLeds<LEDtype, PIN_D0, GRB>(leds[12], LEDper).setCorrection(LEDcorr);
  70.    FastLED.addLeds<LEDtype, PIN_B7, GRB>(leds[13], LEDper).setCorrection(LEDcorr);
  71.    FastLED.addLeds<LEDtype, PIN_B4, GRB>(leds[14], LEDper).setCorrection(LEDcorr);
  72.    FastLED.addLeds<LEDtype, PIN_B3, GRB>(leds[15], LEDper).setCorrection(LEDcorr);
  73.    FastLED.addLeds<LEDtype, PIN_B2, GRB>(leds[16], LEDper).setCorrection(LEDcorr);
  74.    FastLED.addLeds<LEDtype, PIN_B1, GRB>(leds[17], LEDper).setCorrection(LEDcorr);
  75.    FastLED.addLeds<LEDtype, PIN_B0, GRB>(leds[18], LEDper).setCorrection(LEDcorr);
  76.    FastLED.addLeds<LEDtype, PIN_E7, GRB>(leds[19], LEDper).setCorrection(LEDcorr);
  77.    FastLED.addLeds<LEDtype, PIN_E6, GRB>(leds[20], LEDper).setCorrection(LEDcorr);
  78.    FastLED.addLeds<LEDtype, PIN_F0, GRB>(leds[21], LEDper).setCorrection(LEDcorr);
  79.    FastLED.addLeds<LEDtype, PIN_F1, GRB>(leds[22], LEDper).setCorrection(LEDcorr);
  80.    FastLED.addLeds<LEDtype, PIN_F2, GRB>(leds[23], LEDper).setCorrection(LEDcorr);
  81.    FastLED.addLeds<LEDtype, PIN_F3, GRB>(leds[24], LEDper).setCorrection(LEDcorr);
  82.    FastLED.addLeds<LEDtype, PIN_F4, GRB>(leds[25], LEDper).setCorrection(LEDcorr);
  83.    FastLED.addLeds<LEDtype, PIN_F5, GRB>(leds[26], LEDper).setCorrection(LEDcorr);
  84.    FastLED.addLeds<LEDtype, PIN_F6, GRB>(leds[27], LEDper).setCorrection(LEDcorr);
  85.    delay(500);
  86.    FastLED.clear();
  87.    FastLED.show();
  88.    Serial.begin(9600);
  89.    delay(500);
  90. }
  91. SimplePatternList PATTERNlist = {diagonal, tropic, waterfall};    // PATTERNlist[PATTERNnum]();
  92.  
  93. void loop() {
  94.   //uint8_t ratio = ease8InOutCubic(beatsin8(5));         fades in a straight line that looks pretty good right now, but could change that later
  95.  
  96.   EVERY_N_MILLIS(100){        // I'm frame rate limited, and havign this be low makes sure that some patterns don't run a ton faster than other patterns. Could definitely speed this up once I'm on a faster controller
  97.     crossfader();
  98.     hue[0]++;
  99.     hue[1]++;
  100.     FastLED.show();  
  101.   }
  102. }
  103.  
  104. void diagonal(){
  105.   fader(deffade);         // apply some fading, since this pattern is full screen it does not need to be too high
  106.   uint8_t z = fetcher(0);     // get which version of the global variables to use based on what the current pattern is
  107.   CRGBPalette16 palette = startup;
  108.  
  109.   for (int r = 0; r < rowbucket[z]/2; r++){
  110.     for (int c = 0; c < colbucket[z]/4; c++){
  111.        leds[c][r] = ColorFromPalette( palette, hue[z]+(c+r)*10, 255, LINEARBLEND); // normal palette access
  112.     }
  113.   }
  114.  
  115.   if (colbucket[z]/4 < LEDstrips){      // expand width over time
  116.     colbucket[z]++;
  117.   }
  118.   if (rowbucket[z]/2 < LEDper) {        // expand length over time
  119.     rowbucket[z]++;
  120.   }
  121. }
  122.  
  123. void tropic(){
  124.   fader(deffade);
  125.   uint8_t z = fetcher(1);
  126.   CRGBPalette16 palette = tropicana;      // I highly reccomend playing with palettes that have large sections of black in them. It creates great sections of discontinuity
  127.  
  128.   uint8_t BeatsPerMinute = 14;
  129.   uint8_t beat = beatsin16( BeatsPerMinute, 25, 455);
  130.   for(byte x = 0; x < LEDstrips; x++){
  131.     for( int y = 0; y < LEDper; y++){
  132.       leds[x][y] = ColorFromPalette(palette, hue[z]+(y*10)+x*2, beat-hue[z]+(y*10)-x*2, LINEARBLEND);   //leds[x][y] = ColorFromPalette(palette, hue+(y*10)+x*2, beat-hue+(y*10)-x*2, LINEARBLEND);
  133.     }
  134.   }
  135.   hue[z] += 3;
  136. }
  137.  
  138. void waterfall(){
  139.   fader(deffade);
  140.   uint8_t z = fetcher(2);  
  141.   CRGBPalette16 palette = CloudColors_p;
  142.  
  143.   for (int row = 0; row < rowbucket[z]/2; row++){
  144.     for (int col = 0; col < LEDstrips; col++){         // Write each row with start colour and a random saturation      
  145.       leds[col][row] = ColorFromPalette(palette, hue[z] + row*15+sin8((row*7 + col*10+countbucket[z]*7))*(1+row)/80, 255, LINEARBLEND);          // This monster determines how much the wave twists, sin8 function is for base twist, and stuff to the left is for how much twist per row
  146.     }                                                                                                                                            // I'd love to make it twist back and forth, but I haven't come back to it yet, let me know if you can make it work
  147.   }
  148.   hue[z] = hue[z] - 25;
  149.   if(rowbucket[z]/2 < LEDper){
  150.     rowbucket[z]++;
  151.   }  
  152.    countbucket[z]++;
  153. }
  154.  
  155. void patcrossproc(){                                // Every time you switch patterns run this to begin crossfading
  156.   oldpattern = patternum;                           // set the current pattern to be the old one so we can make it use the same variables
  157.   crossct = 0;                                      // reset the blend amount
  158.   patternum++;                                      // increase pattern number   -> this just goes in sequence, but the best part of this is that you can control this by remote or any other system
  159.  
  160.   rowbucket[0] = rowbucket[1];      // copy row status to default rows, and reset them for the new pattern
  161.   rowbucket[1] = 0;
  162.   colbucket[0] = colbucket[1];
  163.   colbucket[1] = 0;
  164.   countbucket[0] = countbucket[1];
  165.   countbucket[1] = 0;
  166.   hue[0] = hue[1];
  167.   hue[1] = 0;
  168.  
  169.   if(patternum > 2){
  170.     patternum = 0;
  171.   }
  172. }
  173.  
  174. void crossfader(){
  175.  if(crossct >= 255){
  176.     PATTERNlist[patternum]();   // run completed pattern only when fading is complete
  177.     if (millis() > 15000){  
  178.       EVERY_N_SECONDS(15){  // new pattern timer
  179.         patcrossproc();
  180.       }
  181.     }  
  182.   }
  183.   else if(crossct < 255){
  184.     crossct+=5;           // higher increase faster xfade
  185.     if(crossct > 255){   // overflow prevention
  186.       crossct = 255;
  187.     }
  188.     uint8_t blendamt = crossct;
  189.  
  190.    if (millis() > 15000){       // Run the old pattern and save to array
  191.     PATTERNlist[oldpattern]();
  192.       for(uint8_t x = 0; x < LEDstrips; x++){
  193.         for(uint8_t y = 0; y < LEDper; y++){
  194.           leds2[x][y] = leds[x][y];
  195.         }
  196.       }
  197.     }
  198.    
  199.     PATTERNlist[patternum]();   // Run the new pattern and save to array
  200.     for(uint8_t x = 0; x < LEDstrips; x++){
  201.       for(uint8_t y = 0; y < LEDper; y++){
  202.         leds3[x][y] = leds[x][y];
  203.       }
  204.     }
  205.    
  206.     for(uint8_t x = 0; x < LEDstrips; x++){     // blend em
  207.       for(uint8_t y = 0; y < LEDper; y++){
  208.         leds[x][y] = blend( leds2[x][y], leds3[x][y], blendamt);   // Blend arrays of LEDs, third value is blend %
  209.       }
  210.     }
  211.   }
  212. }
  213.  
  214. void fader(uint8_t targfade){
  215.   if(currfade > targfade){
  216.       currfade = targfade;
  217.   }
  218.   for(uint8_t x = 0; x < LEDstrips; x++){
  219.     fadeToBlackBy( leds[x], LEDper, currfade);
  220.   }
  221.   EVERY_N_MILLIS(50){
  222.     if(currfade < targfade){
  223.       currfade++;
  224.     }
  225.     else if(currfade > targfade){
  226.       currfade = targfade;
  227.     }
  228.   }
  229. }
  230.  
  231. uint8_t fetcher(uint8_t oldcheck){        // Get which counters should be used
  232.   if(oldcheck == oldpattern){
  233.     return 0;
  234.   } else {
  235.     return 1;
  236.   }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement