Advertisement
Guest User

Funky Clouds 2014

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