Guest User

8x8 Matrix on Funky Clouds

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