SHARE
TWEET

Untitled

TopHatRaver Jan 14th, 2020 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <FastLED.h>
  2.  
  3. #define DATA_PIN   11
  4. #define CLK_PIN    13
  5. #define DATA_PIN_1 26
  6. #define CLK_PIN_1  27
  7.  
  8. #define COLOR_ORDER BGR
  9. #define LED_TYPE APA102
  10.  
  11.  
  12. #define BRIGHTNESS          50
  13. #define FRAMES_PER_SECOND  120
  14.  
  15. // Helper functions for an two-dimensional XY matrix of pixels.
  16. // Simple 2-D demo code is included as well.
  17. //
  18. //     XY(x,y) takes x and y coordinates and returns an LED index number,
  19. //             for use like this:  leds[ XY(x,y) ] == CRGB::Red;
  20. //             No error checking is performed on the ranges of x and y.
  21. //
  22. //     XYsafe(x,y) takes x and y coordinates and returns an LED index number,
  23. //             for use like this:  leds[ XY(x,y) ] == CRGB::Red;
  24. //             Error checking IS performed on the ranges of x and y, and an
  25. //             index of "-1" is returned.  Special instructions below
  26. //             explain how to use this without having to do your own error
  27. //             checking every time you use this function.  
  28. //             This is a slightly more advanced technique, and
  29. //             it REQUIRES SPECIAL ADDITIONAL setup, described below.
  30.  
  31.  
  32. // Params for width and height
  33. const uint8_t kMatrixWidth = 22;
  34. const uint8_t kMatrixHeight = 6;
  35.  
  36. int Xvalue = kMatrixWidth;
  37. int Yvalue = kMatrixHeight;
  38.  
  39. // Param for different pixel layouts
  40. const bool    kMatrixSerpentineLayout = true;
  41. typedef void (*SimplePatternList[])();
  42.  
  43.  
  44.  
  45. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  46. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  47.  
  48. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  49.  
  50. uint16_t XY( uint8_t x, uint8_t y)
  51. {
  52.   uint16_t i;
  53.  
  54.   if( kMatrixSerpentineLayout == false) {
  55.     i = (y * kMatrixWidth) + x;
  56.   }
  57.  
  58.   if( kMatrixSerpentineLayout == true) {
  59.     if( y & 0x01) {
  60.       // Odd rows run backwards
  61.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  62.       i = (y * kMatrixWidth) + reverseX;
  63.     } else {
  64.       // Even rows run forwards
  65.       i = (y * kMatrixWidth) + x;
  66.     }
  67.   }
  68.  
  69.   return i;
  70. }
  71.  
  72. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  73. CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
  74. CRGB* const in_leds( leds_plus_safety_pixel + 1);
  75. CRGB* const out_leds( leds_plus_safety_pixel + 1);
  76.  
  77.  
  78. uint16_t XYsafe( uint8_t x, uint8_t y)
  79. {
  80.   if( x >= kMatrixWidth) return -1;
  81.   if( y >= kMatrixHeight) return -1;
  82.   return XY(x,y);
  83. }
  84.  
  85.  
  86. int stripnum[6][2] = {
  87.   {0, 21},
  88.   {22,43},
  89.   {44,65},
  90.   {66,87},
  91.   {88, 109},
  92.   {110,131}
  93. };
  94.  
  95. int counter = 1;
  96.  
  97. void setup() {
  98.   FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN, COLOR_ORDER>(in_leds,NUM_LEDS);
  99.   FastLED.addLeds<LED_TYPE,DATA_PIN_1,CLK_PIN_1, COLOR_ORDER>(out_leds,NUM_LEDS);
  100.  
  101.   FastLED.setBrightness( BRIGHTNESS );
  102.   Serial.begin(9600);
  103. }
  104.  
  105. SimplePatternList gPatterns = {
  106.     RainbowSwirl_Out_Heart_In,
  107.     //RainbowSwirl_In,
  108.     //RainbowSwirl_Out,
  109.    
  110.     //RainbowSwirl_In_Out,
  111.    
  112. //  RainbowSwirl,
  113. //  RainbowSwirl2,
  114. //  RainbowSwirl3,
  115. //  TwoSidedHeart2,
  116.     Heart1
  117. };
  118.  
  119. void loop()
  120. {
  121.   gPatterns[gCurrentPatternNumber]();
  122.  
  123.   FastLED.show();  
  124.   FastLED.delay(1000/FRAMES_PER_SECOND);
  125.  
  126.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  127.   EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  128.  
  129. }
  130.  
  131. void nextPattern()
  132. {
  133.   // add one to the current pattern number, and wrap around at the end
  134.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  135. }
  136.  
  137. void DrawOneFrame( byte startHue8, int8_t yHueDelta8, int8_t xHueDelta8)
  138. {
  139.   byte lineStartHue = startHue8;
  140.   for( byte y = 0; y < kMatrixHeight; y++) {
  141.     lineStartHue += yHueDelta8;
  142.     byte pixelHue = lineStartHue;      
  143.     for( byte x = 0; x < kMatrixWidth; x++) {
  144.       pixelHue += xHueDelta8;
  145.       in_leds[ XY(x, y)]  = CHSV( pixelHue, 255, 255);
  146.     }
  147.   }
  148. }
  149.  
  150.  
  151.  
  152.  
  153. void rainbow_flash()
  154. {
  155.    uint32_t ms = millis();
  156.     int32_t yHueDelta32 = ((int32_t)cos16( ms * (27/1) ) * (350 / kMatrixWidth));
  157.     int32_t xHueDelta32 = ((int32_t)cos16( ms * (39/1) ) * (310 / kMatrixHeight));
  158.     DrawOneFrame( ms / 65536, yHueDelta32 / 32768, xHueDelta32 / 32768);
  159.     if( ms < 5000 ) {
  160.       FastLED.setBrightness( scale8( BRIGHTNESS, (ms * 256) / 5000));
  161.     } else {
  162.       FastLED.setBrightness(BRIGHTNESS);
  163.     }
  164.     FastLED.show();
  165. }
  166.  
  167. void Heart1()
  168. {
  169.   for(int y = 0; y<Yvalue; y++)
  170.   {
  171.     for(int x = 0; x<Xvalue; x++)
  172.     {
  173.       in_leds[XYsafe(x,y)] = CRGB::Red;   //FILL ONE STRIP WITH RED
  174.       in_leds[XYsafe(x,y-1)] = CRGB::Black;   //REMOVE COLOR FROM PRIOR STRIP
  175.       if(y == 0) {in_leds[XYsafe(x,Yvalue-1)] = CRGB::Black;  } //REMOVE COLOR FROM PRIOR STRIP
  176.     }
  177.     FastLED.show();
  178.     delay(10);
  179.   }
  180. }
  181.  
  182. void TwoSidedHeart()  //FILLS HEART SOLID AND MOVES TO NEXT HEART
  183. {
  184.   if((counter%3) == 0)
  185.     {fill_gradient(in_leds, stripnum[0][0],CHSV(0,255,255),stripnum[0][1], CHSV(0,255,255),SHORTEST_HUES);
  186.     fill_gradient(in_leds, stripnum[3][0],CHSV(0,255,255),stripnum[3][1],  CHSV(0,255,255),SHORTEST_HUES);}
  187.   else if((counter%2) == 0)
  188.     {fill_gradient(in_leds, stripnum[2][0],CHSV(0,255,255),stripnum[2][1], CHSV(0,255,255),SHORTEST_HUES);
  189.     fill_gradient(in_leds, stripnum[5][0],CHSV(0,255,255),stripnum[5][1],  CHSV(0,255,255),SHORTEST_HUES);}
  190.   else if((counter%1) == 0)
  191.     {fill_gradient(in_leds, stripnum[1][0],CHSV(0,255,255),stripnum[1][1], CHSV(0,255,255),SHORTEST_HUES);
  192.     fill_gradient(in_leds, stripnum[4][0],CHSV(0,255,255),stripnum[4][1],  CHSV(0,255,255),SHORTEST_HUES);}
  193.    
  194.     FastLED.show();
  195.     delay(1);
  196.     fadeToBlackBy(in_leds,NUM_LEDS, 230);
  197.     //fill_solid(leds, NUM_LEDS, CRGB:: Black);
  198.     //FastLED.show();
  199.     counter++;
  200. }
  201.  
  202. void TwoSidedHeart2()  //FILLS UP LEDS IN HEART FROM BOTTOM TO TOP
  203. {
  204.  
  205.  
  206.   for(int x = 0; x<Xvalue; x++)
  207.         {
  208.         in_leds[XYsafe(x,counter)] = CRGB:: Red;
  209.         in_leds[XYsafe(Xvalue-1 - x, counter+3)] = CRGB::Red;
  210.         FastLED.show();
  211.         delay(50);
  212.         }
  213.    
  214.    counter++;
  215.    if (counter >2) {counter = 0; fill_solid(in_leds, NUM_LEDS, CRGB::Black);}
  216. }
  217.  
  218. void RainbowSwirl_In()  //FILLS RAINBOW UP EACH STRIP
  219. {
  220.   int colorhue = 255/Xvalue;
  221.   for(int x = 0; x<Xvalue; x++)
  222.   {
  223.     for(int y = 0; y<Yvalue; y++)
  224.     {
  225.       in_leds[XYsafe(x,y)] = CHSV(colorhue*x,255,255);
  226.       FastLED.show();
  227.       //delay(5);
  228.       fadeToBlackBy(in_leds, NUM_LEDS, NUM_LEDS/Xvalue);
  229.     }
  230.  
  231. }
  232. }
  233.  
  234. void RainbowSwirl_Out()  //FILLS RAINBOW UP EACH STRIP
  235. {
  236.   int colorhue = 255/Xvalue;
  237.   for(int x = 0; x<Xvalue; x++)
  238.   {
  239.     for(int y = 0; y<Yvalue; y++)
  240.     {
  241.       out_leds[XYsafe(x,y)] = CHSV(colorhue*x,255,255);
  242.       FastLED.show();
  243.       //delay(5);
  244.       fadeToBlackBy(out_leds, NUM_LEDS, NUM_LEDS/Xvalue);
  245.     }
  246.  
  247. }
  248. }
  249.  
  250. void RainbowSwirl2()  //FILLS RAINBOW THROUGH ENTIRE LED
  251. {
  252.   int colorhue = 255/NUM_LEDS;
  253.   for(int x = 0; x<Xvalue; x++)
  254.   {
  255.     for(int y = 0; y<Yvalue; y++)
  256.     {
  257.       in_leds[XYsafe(x,y)] = CHSV(colorhue++,255,255);
  258.       FastLED.show();
  259.       delay(5);
  260.     }
  261.  
  262. }
  263. }
  264.  
  265. void RainbowSwirl3()  //FILLS EACH STRIP ONE BY ONE
  266. {
  267.   int colorhue = 255/Xvalue;
  268.     for(int y = 0; y<Yvalue; y++)
  269.     {
  270.       for(int x = 0; x<Xvalue; x++)
  271.      {
  272.       in_leds[XYsafe(x,y)] = CHSV(colorhue*x,255,255);
  273.       }
  274.       FastLED.show();
  275.       delay(500);
  276. }
  277. }
  278.  
  279.  
  280. void RainbowSwirl_In_Out()
  281. {
  282.   RainbowSwirl_In();
  283.   RainbowSwirl_Out();
  284.  
  285. }
  286.  
  287. void RainbowSwirl_Out_Heart_In()
  288. {
  289.   RainbowSwirl_In();
  290.   Heart1();
  291. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top