Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Funky Clouds 2014
- // www.stefan-petrick.de/wordpress_beta
- #include <FastLED.h>
- // Matrix dimensions
- const uint8_t WIDTH = 16;
- const uint8_t HEIGHT = 16;
- // LED stuff
- #define LED_PIN 23
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812
- #define BRIGHTNESS 80
- #define NUM_LEDS (WIDTH * HEIGHT)
- CRGB leds[NUM_LEDS];
- // Timer stuff (Oszillators)
- struct timer {unsigned long takt; unsigned long lastMillis; unsigned long count; int delta; byte up; byte down;};
- // Always set the following number right - If you set a timer,
- // you have to set sensfull limits in the setup part
- timer multiTimer[5];
- int timers = sizeof(multiTimer) / sizeof(multiTimer[0]);
- void setup() {
- FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- // set all counting directions positive for the beginning
- for (int i = 0; i < timers; i++) multiTimer[i].delta = 1;
- // set range (up/down), speed (takt=ms between steps) and starting point of all oszillators
- multiTimer[0].takt= 42; //x1
- multiTimer[0].up = WIDTH - 1;
- multiTimer[0].down = 0;
- multiTimer[0].count = 0;
- multiTimer[1].takt= 55; //y1
- multiTimer[1].up = HEIGHT - 1;
- multiTimer[1].down = 0;
- multiTimer[1].count = 0;
- multiTimer[2].takt= 3; //color
- multiTimer[2].up = 255;
- multiTimer[2].down = 0;
- multiTimer[2].count = 0;
- multiTimer[3].takt= 71; //x2
- multiTimer[3].up = WIDTH - 1;
- multiTimer[3].down = 0;
- multiTimer[3].count = 0;
- multiTimer[4].takt= 89; //y2
- multiTimer[4].up = HEIGHT - 1;
- multiTimer[4].down = 0;
- multiTimer[4].count = 0;
- }
- void loop()
- {
- // manage the Oszillators
- UpdateTimers();
- // draw just a line defined by 5 oszillators
- Line(
- multiTimer[3].count, // x1
- multiTimer[4].count, // y1
- multiTimer[0].count, // x2
- multiTimer[1].count, // y2
- multiTimer[2].count); // color
- // manipulate the screen buffer
- // with fixed parameters (could be oszillators too)
- // center x, y, radius, scale color down
- // --> affects always a square with an odd length
- Spiral(7,7,8,128);
- // why not several times?!
- Spiral(8,3,3,128);
- Spiral(5,12,5,128);
- // increase the contrast
- DimmAll(250);
- // done.
- FastLED.show();
- }
- // finds the right index for my S matrix
- int XY(int x, int y) {
- if(y > HEIGHT) { y = HEIGHT; }
- if(y < 0) { y = 0; }
- if(x > WIDTH) { x = WIDTH;}
- if(x < 0) { x = 0; }
- if(x % 2 == 1) {
- return (x * (WIDTH) + (HEIGHT - y -1));
- } else {
- return (x * (WIDTH) + y);
- }
- }
- // counts all variables with different speeds linear up and down
- void UpdateTimers()
- {
- unsigned long now=millis();
- for (int i=0; i < timers; i++)
- {
- while (now-multiTimer[i].lastMillis >= multiTimer[i].takt)
- {
- multiTimer[i].lastMillis += multiTimer[i].takt;
- multiTimer[i].count = multiTimer[i].count + multiTimer[i].delta;
- if ((multiTimer[i].count == multiTimer[i].up) || (multiTimer[i].count == multiTimer[i].down))
- {
- multiTimer[i].delta = -multiTimer[i].delta;
- }
- }
- }
- }
- // fade the image buffer arround
- // x, y: center r: radius dimm: fade down
- void Spiral(int x,int y, int r, byte dimm) {
- for(int d = r; d >= 0; d--) {
- for(int i = x-d; i <= x+d; i++) {
- leds[XY(i,y-d)] += leds[XY(i+1,y-d)];
- leds[XY(i,y-d)].nscale8( dimm );}
- for(int i = y-d; i <= y+d; i++) {
- leds[XY(x+d,i)] += leds[XY(x+d,i+1)];
- leds[XY(x+d,i)].nscale8( dimm );}
- for(int i = x+d; i >= x-d; i--) {
- leds[XY(i,y+d)] += leds[XY(i-1,y+d)];
- leds[XY(i,y+d)].nscale8( dimm );}
- for(int i = y+d; i >= y-d; i--) {
- leds[XY(x-d,i)] += leds[XY(x-d,i-1)];
- leds[XY(x-d,i)].nscale8( dimm );}
- }
- }
- // Bresenham line
- void Line(int x0, int y0, int x1, int y1, byte color)
- {
- int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
- int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
- int err = dx+dy, e2;
- for(;;){
- leds[XY(x0, y0)] += CHSV(color, 255, 255);
- if (x0==x1 && y0==y1) break;
- e2 = 2*err;
- if (e2 > dy) { err += dy; x0 += sx; }
- if (e2 < dx) { err += dx; y0 += sy; }
- }
- }
- void DimmAll(byte value)
- {
- for(int i = 0; i < NUM_LEDS; i++)
- {
- leds[i].nscale8(value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement