Advertisement
Guest User

Stefan Petrick

a guest
May 14th, 2014
1,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. /*
  2.     Toys For LED Matrix Effects
  3.  
  4. www.stefan-petrick.de/wordpress_beta
  5.  
  6. */
  7.  
  8. #include <FastLED.h>
  9.  
  10. // Matrix Size
  11.  
  12. const uint8_t WIDTH  = 16;
  13. const uint8_t HEIGHT = 16;
  14.  
  15. // LED Setup
  16.  
  17. #define LED_PIN     23
  18. #define COLOR_ORDER GRB
  19. #define CHIPSET     WS2812
  20. #define BRIGHTNESS  80
  21. #define NUM_LEDS (WIDTH * HEIGHT)
  22. CRGB    leds[NUM_LEDS];
  23. CRGB    buffer[NUM_LEDS];
  24.  
  25. byte count;
  26.  
  27. void setup()
  28. {
  29.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  30.   FastLED.setBrightness(BRIGHTNESS);
  31. }
  32.  
  33. void loop()
  34. {
  35.   count=count+5;
  36.    
  37.   // first plant the seed into the buffer
  38.   buffer[XY(sin8(count)/17, cos8(count)/17)] = CHSV (160 , 255, 255); // the circle  
  39.   buffer[XY(quadwave8(count)/17, 4)] = CHSV (0 , 255, 255); // lines following different wave fonctions
  40.   buffer[XY(cubicwave8(count)/17, 6)] = CHSV (40 , 255, 255);
  41.   buffer[XY(triwave8(count)/17, 8)] = CHSV (80 , 255, 255);
  42.  
  43.   // duplicate the seed in the buffer
  44.   Caleidoscope4();
  45.  
  46.   // add buffer to leds
  47.   ShowBuffer();
  48.  
  49.   // clear buffer
  50.   ClearBuffer();
  51.  
  52.   // rotate leds
  53.   Spiral(7,7,8,110);
  54.  
  55.   FastLED.show();
  56.  
  57.   // do not delete the current leds, just fade them down for the tail effect
  58.   //DimmAll(220);
  59.  
  60. }
  61.  
  62. // finds the right index for a S shaped matrix
  63. int XY(int x, int y) {
  64.   if(y > HEIGHT) { y = HEIGHT; }
  65.   if(y < 0) { y = 0; }
  66.   if(x > WIDTH) { x = WIDTH;}
  67.   if(x < 0) { x = 0; }
  68.   if(x % 2 == 1) {  
  69.   return (x * (WIDTH) + (HEIGHT - y -1));
  70.   } else {
  71.     // use that line only, if you have all rows beginning at the same side
  72.     return (x * (WIDTH) + y);  
  73.   }
  74. }
  75.  
  76. // scale the brightness of the screenbuffer down
  77. void DimmAll(byte value)  
  78. {
  79.   for(int i = 0; i < NUM_LEDS; i++)
  80.   {
  81.     leds[i].nscale8(value);
  82.   }
  83. }
  84.  
  85. /*
  86. Caleidoscope1 mirrors from source to A, B and C
  87.  
  88. y
  89.  
  90. |       |
  91. |   B   |   C
  92. |_______________
  93. |       |
  94. |source |   A
  95. |_______________ x
  96.  
  97. */
  98. void Caleidoscope1() {
  99.   for(int x = 0; x < WIDTH / 2 ; x++) {
  100.     for(int y = 0; y < HEIGHT / 2; y++) {
  101.       leds[XY( WIDTH - 1 - x, y )] = leds[XY( x, y )];              // copy to A
  102.       leds[XY( x, HEIGHT - 1 - y )] = leds[XY( x, y )];             // copy to B
  103.       leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // copy to C
  104.      
  105.     }
  106.   }
  107. }
  108.  
  109. /*
  110. Caleidoscope2 rotates from source to A, B and C
  111.  
  112. y
  113.  
  114. |       |
  115. |   C   |   B
  116. |_______________
  117. |       |
  118. |source |   A
  119. |_______________ x
  120.  
  121. */
  122. void Caleidoscope2() {
  123.   for(int x = 0; x < WIDTH / 2 ; x++) {
  124.     for(int y = 0; y < HEIGHT / 2; y++) {
  125.       leds[XY( WIDTH - 1 - x, y )] = leds[XY( y, x )];    // rotate to A
  126.       leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )];    // rotate to B
  127.       leds[XY( x, HEIGHT - 1 - y )] = leds[XY( y, x )];    // rotate to C
  128.      
  129.      
  130.     }
  131.   }
  132. }
  133.  
  134. // adds the color of one quarter to the other 3
  135. void Caleidoscope3() {
  136.   for(int x = 0; x < WIDTH / 2 ; x++) {
  137.     for(int y = 0; y < HEIGHT / 2; y++) {
  138.       leds[XY( WIDTH - 1 - x, y )] += leds[XY( y, x )];    // rotate to A
  139.       leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += leds[XY( x, y )];    // rotate to B
  140.       leds[XY( x, HEIGHT - 1 - y )] += leds[XY( y, x )];    // rotate to C
  141.      
  142.      
  143.     }
  144.   }
  145. }
  146.  
  147. // add the complete buffer 3 times while rotating
  148. void Caleidoscope4() {
  149.   for(int x = 0; x < WIDTH ; x++) {
  150.     for(int y = 0; y < HEIGHT ; y++) {
  151.       buffer[XY( WIDTH - 1 - x, y )] += buffer[XY( y, x )];    // rotate to A
  152.       buffer[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += buffer[XY( x, y )];    // rotate to B
  153.       buffer[XY( x, HEIGHT - 1 - y )] += buffer[XY( y, x )];    // rotate to C
  154.      
  155.      
  156.     }
  157.   }
  158. }
  159.  
  160. void ShowBuffer() {
  161.   for(int i = 0; i < NUM_LEDS ; i++) {
  162.     leds[i] += buffer[i];
  163.   }
  164. }
  165.  
  166. void ClearBuffer() {
  167.   for(int i = 0; i < NUM_LEDS ; i++) {
  168.     buffer[i] = 0;
  169.   }
  170. }
  171.  
  172. void Spiral(int x,int y, int r, byte dimm) {  
  173.   for(int d = r; d >= 0; d--) {                // from the outside to the inside
  174.     for(int i = x-d; i <= x+d; i++) {
  175.        leds[XY(i,y-d)] += leds[XY(i+1,y-d)];   // lowest row to the right
  176.        leds[XY(i,y-d)].nscale8( dimm );}
  177.     for(int i = y-d; i <= y+d; i++) {
  178.        leds[XY(x+d,i)] += leds[XY(x+d,i+1)];   // right colum up
  179.        leds[XY(x+d,i)].nscale8( dimm );}
  180.     for(int i = x+d; i >= x-d; i--) {
  181.        leds[XY(i,y+d)] += leds[XY(i-1,y+d)];   // upper row to the left
  182.        leds[XY(i,y+d)].nscale8( dimm );}
  183.     for(int i = y+d; i >= y-d; i--) {
  184.        leds[XY(x-d,i)] += leds[XY(x-d,i-1)];   // left colum down
  185.        leds[XY(x-d,i)].nscale8( dimm );}
  186.   }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement