Guest User

Funky Lines 8x8 Matrix

a guest
May 7th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.31 KB | None | 0 0
  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  = 8;
  9. const uint8_t HEIGHT = 8;
  10.  
  11. // LED stuff
  12.  
  13. #define LED_PIN     5
  14. #define COLOR_ORDER GRB
  15. #define CHIPSET     WS2812B
  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).setCorrection(SMD5050);
  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=  31;     //x1
  40.   multiTimer[0].up = WIDTH - 1;
  41.   multiTimer[0].down = 0;
  42.   multiTimer[0].count = 0;
  43.  
  44.   multiTimer[1].takt=  37;     //y1
  45.   multiTimer[1].up = HEIGHT - 1;
  46.   multiTimer[1].down = 0;
  47.   multiTimer[1].count = 0;
  48.  
  49.   multiTimer[2].takt=  15;      //color
  50.   multiTimer[2].up = 255;
  51.   multiTimer[2].down = 0;
  52.   multiTimer[2].count = 0;
  53.  
  54.   multiTimer[3].takt=  45;     //x2  
  55.   multiTimer[3].up = WIDTH - 1;
  56.   multiTimer[3].down = 0;
  57.   multiTimer[3].count = 0;
  58.  
  59.   multiTimer[4].takt=  57;     //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(3,3,3,128);
  83. //  Spiral(4,1,2,128);
  84.   // why not several times?!
  85. //  Spiral(8,3,3,128);
  86. //  Spiral(5,12,5,128);
  87.  
  88.   // increase the contrast
  89.   DimmAll(200);
  90.  
  91. //  FastLED.delay(20);
  92.   // done.
  93.   FastLED.show();
  94.  
  95. }
  96.  
  97.  
  98. // finds the right index for my S matrix
  99. int XY(int x, int y) {
  100.   if(y > HEIGHT) { y = HEIGHT; }
  101.   if(y < 0) { y = 0; }
  102.   if(x > WIDTH) { x = WIDTH;}
  103.   if(x < 0) { x = 0; }
  104.   return (x * (WIDTH) + y);
  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.     for(int i = x-d; i <= x+d; i++) {
  130.        leds[XY(i,y-d)] += leds[XY(i+1,y-d)];
  131.        leds[XY(i,y-d)].nscale8( dimm );}
  132.     for(int i = y-d; i <= y+d; i++) {
  133.        leds[XY(x+d,i)] += leds[XY(x+d,i+1)];
  134.        leds[XY(x+d,i)].nscale8( dimm );}
  135.     for(int i = x+d; i >= x-d; i--) {
  136.        leds[XY(i,y+d)] += leds[XY(i-1,y+d)];
  137.        leds[XY(i,y+d)].nscale8( dimm );}
  138.     for(int i = y+d; i >= y-d; i--) {
  139.        leds[XY(x-d,i)] += leds[XY(x-d,i-1)];
  140.        leds[XY(x-d,i)].nscale8( dimm );}
  141.   }
  142. }
  143.    
  144. // Bresenham line
  145. void Line(int x0, int y0, int x1, int y1, byte color)
  146. {
  147.   int dx =  abs(x1-x0), sx = x0<x1 ? 1 : -1;
  148.   int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
  149.   int err = dx+dy, e2;
  150.   for(;;){  
  151.     leds[XY(x0, y0)] += CHSV(color, 255, 255);
  152.     if (x0==x1 && y0==y1) break;
  153.     e2 = 2*err;
  154.     if (e2 > dy) { err += dy; x0 += sx; }
  155.     if (e2 < dx) { err += dx; y0 += sy; }
  156.   }
  157. }
  158.  
  159. void DimmAll(byte value)  
  160. {
  161.   for(int i = 0; i < NUM_LEDS; i++)
  162.   {
  163.     leds[i].nscale8(value);
  164.   }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment