Advertisement
Guest User

Funky Clouds 2014

a guest
May 6th, 2014
1,436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Funky Clouds 2014
  2. // www.stefan-petrick.de/wordpress_beta
  3.  
  4. #include <FastLED.h>
  5.  
  6. // Matrix dimensions
  7.  
  8. const uint8_t WIDTH  = 16;
  9. const uint8_t HEIGHT = 16;
  10.  
  11. // LED stuff
  12.  
  13. #define LED_PIN     23
  14. #define COLOR_ORDER GRB
  15. #define CHIPSET     WS2812
  16. #define BRIGHTNESS  80
  17. #define NUM_LEDS (WIDTH * HEIGHT)
  18. CRGB leds[NUM_LEDS];
  19.  
  20. // Timer stuff (Oszillators)
  21.  
  22. struct timer {unsigned long takt; unsigned long lastMillis; unsigned long count; int delta; byte up; byte down;};
  23. // Always set the following number right - If you set a timer,
  24. // you have to set sensfull limits in the setup part
  25. timer multiTimer[5];
  26.  
  27. int timers = sizeof(multiTimer) / sizeof(multiTimer[0]);
  28.  
  29. void setup() {
  30.  
  31.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  32.   FastLED.setBrightness(BRIGHTNESS);
  33.  
  34.   // set all counting directions positive for the beginning
  35.   for (int i = 0; i < timers; i++) multiTimer[i].delta = 1;
  36.  
  37.   // set range (up/down), speed (takt=ms between steps) and starting point of all oszillators
  38.  
  39.   multiTimer[0].takt=  42;     //x1
  40.   multiTimer[0].up = WIDTH - 1;
  41.   multiTimer[0].down = 0;
  42.   multiTimer[0].count = 0;
  43.  
  44.   multiTimer[1].takt=  55;     //y1
  45.   multiTimer[1].up = HEIGHT - 1;
  46.   multiTimer[1].down = 0;
  47.   multiTimer[1].count = 0;
  48.  
  49.   multiTimer[2].takt=  3;      //color
  50.   multiTimer[2].up = 255;
  51.   multiTimer[2].down = 0;
  52.   multiTimer[2].count = 0;
  53.  
  54.   multiTimer[3].takt=  71;     //x2  
  55.   multiTimer[3].up = WIDTH - 1;
  56.   multiTimer[3].down = 0;
  57.   multiTimer[3].count = 0;
  58.  
  59.   multiTimer[4].takt=  89;     //y2
  60.   multiTimer[4].up = HEIGHT - 1;
  61.   multiTimer[4].down = 0;
  62.   multiTimer[4].count = 0;
  63. }
  64.  
  65. void loop()
  66. {
  67.   // manage the Oszillators
  68.   UpdateTimers();
  69.  
  70.   // draw just a line defined by 5 oszillators
  71.   Line(
  72.     multiTimer[3].count,  // x1
  73.     multiTimer[4].count,  // y1
  74.     multiTimer[0].count,  // x2
  75.     multiTimer[1].count,  // y2
  76.     multiTimer[2].count); // color
  77.  
  78.   // manipulate the screen buffer
  79.   // with fixed parameters (could be oszillators too)
  80.   // center x, y, radius, scale color down
  81.   // --> affects always a square with an odd length
  82.   Spiral(7,7,8,128);
  83.  
  84.   // why not several times?!
  85.   Spiral(8,3,3,128);
  86.   Spiral(5,12,5,128);
  87.  
  88.   // increase the contrast
  89.   DimmAll(250);
  90.  
  91.   // done.
  92.   FastLED.show();
  93. }
  94.  
  95.  
  96. // finds the right index for my S matrix
  97. int XY(int x, int y) {
  98.   if(y > HEIGHT) { y = HEIGHT; }
  99.   if(y < 0) { y = 0; }
  100.   if(x > WIDTH) { x = WIDTH;}
  101.   if(x < 0) { x = 0; }
  102.   if(x % 2 == 1) {  
  103.   return (x * (WIDTH) + (HEIGHT - y -1));
  104.   } else {
  105.     return (x * (WIDTH) + y);
  106.   }
  107. }
  108.  
  109. // counts all variables with different speeds linear up and down
  110. void UpdateTimers()
  111. {
  112.   unsigned long now=millis();
  113.   for (int i=0; i < timers; i++)
  114.   {
  115.     while (now-multiTimer[i].lastMillis >= multiTimer[i].takt)
  116.     {
  117.       multiTimer[i].lastMillis += multiTimer[i].takt;
  118.       multiTimer[i].count = multiTimer[i].count + multiTimer[i].delta;
  119.       if ((multiTimer[i].count == multiTimer[i].up) || (multiTimer[i].count == multiTimer[i].down))
  120.       {
  121.         multiTimer[i].delta = -multiTimer[i].delta;
  122.       }
  123.     }
  124.   }
  125. }
  126.  
  127. // fade the image buffer arround
  128. // x, y: center   r: radius   dimm: fade down
  129. void Spiral(int x,int y, int r, byte dimm) {  
  130.   for(int d = r; d >= 0; d--) {
  131.     for(int i = x-d; i <= x+d; i++) {
  132.        leds[XY(i,y-d)] += leds[XY(i+1,y-d)];
  133.        leds[XY(i,y-d)].nscale8( dimm );}
  134.     for(int i = y-d; i <= y+d; i++) {
  135.        leds[XY(x+d,i)] += leds[XY(x+d,i+1)];
  136.        leds[XY(x+d,i)].nscale8( dimm );}
  137.     for(int i = x+d; i >= x-d; i--) {
  138.        leds[XY(i,y+d)] += leds[XY(i-1,y+d)];
  139.        leds[XY(i,y+d)].nscale8( dimm );}
  140.     for(int i = y+d; i >= y-d; i--) {
  141.        leds[XY(x-d,i)] += leds[XY(x-d,i-1)];
  142.        leds[XY(x-d,i)].nscale8( dimm );}
  143.   }
  144. }
  145.    
  146. // Bresenham line
  147. void Line(int x0, int y0, int x1, int y1, byte color)
  148. {
  149.   int dx =  abs(x1-x0), sx = x0<x1 ? 1 : -1;
  150.   int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
  151.   int err = dx+dy, e2;
  152.   for(;;){  
  153.     leds[XY(x0, y0)] += CHSV(color, 255, 255);
  154.     if (x0==x1 && y0==y1) break;
  155.     e2 = 2*err;
  156.     if (e2 > dy) { err += dy; x0 += sx; }
  157.     if (e2 < dx) { err += dx; y0 += sy; }
  158.   }
  159. }
  160.  
  161. void DimmAll(byte value)  
  162. {
  163.   for(int i = 0; i < NUM_LEDS; i++)
  164.   {
  165.     leds[i].nscale8(value);
  166.   }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement