Guest User

FunkyClouds-8x100

a guest
Jun 13th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. /*
  2. Funky Clouds
  3.  
  4. Toy Collection For
  5. Matrix Effects
  6. @BM 2014(?!)
  7.  
  8. www.stefan-petrick.de/wordpress_beta
  9.  
  10. Keep the code alive by evolving
  11. ...and then do whatever you love with the source.
  12.  
  13. */
  14.  
  15. #include <FastLED.h>
  16.  
  17. // Matrix Size
  18.  
  19. const uint8_t WIDTH = 16;
  20. const uint8_t HEIGHT = 16;
  21.  
  22. // LED Setup
  23.  
  24.  
  25. #define BRIGHTNESS 255
  26. #define NUM_LEDS (800)
  27. CRGB leds[NUM_LEDS];
  28. CRGB buffer[NUM_LEDS];
  29.  
  30. byte count[4]; // counters for the wave functions
  31.  
  32. void setup()
  33. {
  34. //FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  35. FastLED.addLeds<WS2812B, 2, GRB>(leds, HEIGHT * 0, 100);
  36. FastLED.addLeds<WS2812B, 14, GRB>(leds, HEIGHT * 1, 100);
  37. FastLED.addLeds<WS2812B, 7, GRB>(leds, HEIGHT * 2, 100);
  38. FastLED.addLeds<WS2812B, 8, GRB>(leds, HEIGHT * 3, 100);
  39. FastLED.addLeds<WS2812B, 6, GRB>(leds, HEIGHT * 4, 100);
  40. FastLED.addLeds<WS2812B, 20, GRB>(leds, HEIGHT * 5, 100);
  41. FastLED.addLeds<WS2812B, 21, GRB>(leds, HEIGHT * 6, 100);
  42. FastLED.addLeds<WS2812B, 5, GRB>(leds, HEIGHT * 7, 100);
  43. FastLED.setBrightness(BRIGHTNESS);
  44. }
  45.  
  46. void loop()
  47. {
  48. // since I found out that integers are casted impicit that part became easy...
  49. // so just have 4 sinewaves with differnt speeds running
  50. count[0] = count[0] + 7;
  51. count[1] = count[1] + 5;
  52. count[2] = count[2] + 3;
  53. count[3] = count[3] + 2;
  54.  
  55. // first plant the seed into the buffer
  56. //buffer[XY(sin8(count[0])/20, cos8(count[1])/17)] = CHSV (count[0] , 255, 255);
  57.  
  58. //buffer[XY(sin8(count[2])/17, cos8(count[3])/17)] = CHSV (count[1] , 255, 255);
  59.  
  60. Line(((sin8(count[0])/17)+(sin8(count[2])/17))/2, ((sin8(count[1])/17)+(sin8(count[3])/17))/2, // combine 2 wavefunctions for more variety of x
  61. ((sin8(count[3])/17)+(sin8(count[2])/17))/2, ((sin8(count[1])/17)+(sin8(count[2])/17))/2, count[0]); // and y, color just simple linear ramp
  62.  
  63. // just some try and error:
  64. //Line(sin8(count[3])/17, cos8(count[1])/17, sin8(count[1])/17, cos8(count[2])/17, count[3]);
  65. //buffer[XY(quadwave8(count)/17, 4)] = CHSV (0 , 255, 255); // lines following different wave fonctions
  66. //buffer[XY(cubicwave8(count)/17, 6)] = CHSV (40 , 255, 255);
  67. //buffer[XY(triwave8(count)/17, 8)] = CHSV (80 , 255, 255);
  68.  
  69. // duplicate the seed in the buffer
  70. //Caleidoscope4();
  71.  
  72. // add buffer to leds
  73. ShowBuffer();
  74.  
  75. // clear buffer
  76. ClearBuffer();
  77.  
  78. // rotate leds
  79. Spiral(7,7,8,110);
  80.  
  81. //DimmAll(230);
  82.  
  83. FastLED.show();
  84.  
  85. // do not delete the current leds, just fade them down for the tail effect
  86. //DimmAll(220);
  87.  
  88. }
  89.  
  90. // finds the right index for a S shaped matrix
  91. // ...maybe you need a different mapping for your setup
  92. int XY(int x, int y) {
  93. if(y > HEIGHT) { y = HEIGHT; }
  94. if(y < 0) { y = 0; }
  95. if(x > WIDTH) { x = WIDTH;}
  96. if(x < 0) { x = 0; }
  97. if(x % 2 == 1) {
  98. return (x * (WIDTH) + (HEIGHT - y -1));
  99. } else {
  100. // use that line only, if you have all rows beginning at the same side
  101. return (x * (WIDTH) + y);
  102. }
  103. }
  104.  
  105. // scale the brightness of the screenbuffer down
  106. void DimmAll(byte value)
  107. {
  108. for(int i = 0; i < NUM_LEDS; i++)
  109. {
  110. leds[i].nscale8(value);
  111. }
  112. }
  113.  
  114. /*
  115. Caleidoscope1 mirrors from source to A, B and C
  116.  
  117. y
  118.  
  119. | |
  120. | B | C
  121. |_______________
  122. | |
  123. |source | A
  124. |_______________ x
  125.  
  126. */
  127. void Caleidoscope1() {
  128. for(int x = 0; x < WIDTH / 2 ; x++) {
  129. for(int y = 0; y < HEIGHT / 2; y++) {
  130. leds[XY( WIDTH - 1 - x, y )] = leds[XY( x, y )]; // copy to A
  131. leds[XY( x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // copy to B
  132. leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // copy to C
  133.  
  134. }
  135. }
  136. }
  137.  
  138. /*
  139. Caleidoscope2 rotates from source to A, B and C
  140.  
  141. y
  142.  
  143. | |
  144. | C | B
  145. |_______________
  146. | |
  147. |source | A
  148. |_______________ x
  149.  
  150. */
  151. void Caleidoscope2() {
  152. for(int x = 0; x < WIDTH / 2 ; x++) {
  153. for(int y = 0; y < HEIGHT / 2; y++) {
  154. leds[XY( WIDTH - 1 - x, y )] = leds[XY( y, x )]; // rotate to A
  155. leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // rotate to B
  156. leds[XY( x, HEIGHT - 1 - y )] = leds[XY( y, x )]; // rotate to C
  157.  
  158.  
  159. }
  160. }
  161. }
  162.  
  163. // adds the color of one quarter to the other 3
  164. void Caleidoscope3() {
  165. for(int x = 0; x < WIDTH / 2 ; x++) {
  166. for(int y = 0; y < HEIGHT / 2; y++) {
  167. leds[XY( WIDTH - 1 - x, y )] += leds[XY( y, x )]; // rotate to A
  168. leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += leds[XY( x, y )]; // rotate to B
  169. leds[XY( x, HEIGHT - 1 - y )] += leds[XY( y, x )]; // rotate to C
  170.  
  171.  
  172. }
  173. }
  174. }
  175.  
  176. // add the complete buffer 3 times while rotating
  177. void Caleidoscope4() {
  178. for(int x = 0; x < WIDTH ; x++) {
  179. for(int y = 0; y < HEIGHT ; y++) {
  180. buffer[XY( WIDTH - 1 - x, y )] += buffer[XY( y, x )]; // rotate to A
  181. buffer[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += buffer[XY( x, y )]; // rotate to B
  182. buffer[XY( x, HEIGHT - 1 - y )] += buffer[XY( y, x )]; // rotate to C
  183.  
  184.  
  185. }
  186. }
  187. }
  188.  
  189. void ShowBuffer() {
  190. for(int i = 0; i < NUM_LEDS ; i++) {
  191. leds[i] += buffer[i];
  192. }
  193. }
  194.  
  195. void ClearBuffer() {
  196. for(int i = 0; i < NUM_LEDS ; i++) {
  197. buffer[i] = 0;
  198. }
  199. }
  200.  
  201. // creates a little twister for softening
  202. void Spiral(int x,int y, int r, byte dimm) {
  203. for(int d = r; d >= 0; d--) { // from the outside to the inside
  204. for(int i = x-d; i <= x+d; i++) {
  205. leds[XY(i,y-d)] += leds[XY(i+1,y-d)]; // lowest row to the right
  206. leds[XY(i,y-d)].nscale8( dimm );}
  207. for(int i = y-d; i <= y+d; i++) {
  208. leds[XY(x+d,i)] += leds[XY(x+d,i+1)]; // right colum up
  209. leds[XY(x+d,i)].nscale8( dimm );}
  210. for(int i = x+d; i >= x-d; i--) {
  211. leds[XY(i,y+d)] += leds[XY(i-1,y+d)]; // upper row to the left
  212. leds[XY(i,y+d)].nscale8( dimm );}
  213. for(int i = y+d; i >= y-d; i--) {
  214. leds[XY(x-d,i)] += leds[XY(x-d,i-1)]; // left colum down
  215. leds[XY(x-d,i)].nscale8( dimm );}
  216. }
  217. }
  218.  
  219. // Bresenham line algorythm
  220. void Line(int x0, int y0, int x1, int y1, byte color)
  221. {
  222. int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
  223. int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
  224. int err = dx+dy, e2;
  225. for(;;){
  226. buffer[XY(x0, y0)] += CHSV(color, 255, 255);
  227. if (x0==x1 && y0==y1) break;
  228. e2 = 2*err;
  229. if (e2 > dy) { err += dy; x0 += sx; }
  230. if (e2 < dx) { err += dx; y0 += sy; }
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment