View difference between Paste ID: pFdQSsFD and BYR9A74W
SHOW: | | - or go back to the newest paste.
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-
// you have to give sensfull limits in the setup part
23+
24
// you have to set sensfull limits in the setup part
25
timer multiTimer[5];
26-
int timers = sizeof( multiTimer ) / sizeof( multiTimer[0] ); 
26+
27
int timers = sizeof(multiTimer) / sizeof(multiTimer[0]); 
28
29
void setup() {
30
  
31-
  FastLED.setBrightness( BRIGHTNESS );
31+
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-
  multiTimer[0].takt=  40;     //x1
38+
39-
  multiTimer[0].up = 15;
39+
  multiTimer[0].takt=  42;     //x1
40
  multiTimer[0].up = WIDTH - 1;
41-
  multiTimer[0].count = 10;
41+
42
  multiTimer[0].count = 0;
43-
  multiTimer[1].takt=  50;     //y1
43+
44-
  multiTimer[1].up = 15;
44+
  multiTimer[1].takt=  55;     //y1
45
  multiTimer[1].up = HEIGHT - 1;
46-
  multiTimer[1].count = 3;
46+
47
  multiTimer[1].count = 0;
48
  
49
  multiTimer[2].takt=  3;      //color
50
  multiTimer[2].up = 255;
51-
  multiTimer[2].count = 5;
51+
52
  multiTimer[2].count = 0;
53-
  multiTimer[3].takt=  70;     //x2  
53+
54-
  multiTimer[3].up = 15;
54+
  multiTimer[3].takt=  71;     //x2  
55
  multiTimer[3].up = WIDTH - 1;
56-
  multiTimer[3].count = 7;
56+
57
  multiTimer[3].count = 0;
58-
  multiTimer[4].takt=  90;     //y2
58+
59-
  multiTimer[4].up = 15;
59+
  multiTimer[4].takt=  89;     //y2
60
  multiTimer[4].up = HEIGHT - 1;
61-
  multiTimer[4].count = 3;
61+
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-
  Spiral(7,7,8,129);
79+
80
  // center x, y, radius, scale color down
81
  // --> affects always a square with an odd length
82-
  Spiral(8,3,3,129);
82+
  Spiral(7,7,8,128);
83-
  Spiral(5,12,5,129);
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-
 
99+
100-
  if(x % 2 == 1) { // 
100+
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-
     
141+
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-
 
154+
155
    e2 = 2*err;
156
    if (e2 > dy) { err += dy; x0 += sx; } 
157-
    leds[XY(x0, y0)] += CHSV( color,255,255);
157+
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
}