Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Funky Clouds
- Toy Collection For
- Matrix Effects
- @BM 2014(?!)
- www.stefan-petrick.de/wordpress_beta
- Keep the code alive by evolving
- ...and then do whatever you love with the source.
- */
- #include <FastLED.h>
- // Matrix Size
- const uint8_t WIDTH = 16;
- const uint8_t HEIGHT = 16;
- // LED Setup
- #define LED_PIN 23
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812
- #define BRIGHTNESS 255
- #define NUM_LEDS (WIDTH * HEIGHT)
- CRGB leds[NUM_LEDS];
- CRGB buffer[NUM_LEDS];
- byte count[4]; // counters for the wave functions
- void setup()
- {
- FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- }
- void loop()
- {
- // since I found out that integers are casted impicit that part became easy...
- // so just have 4 sinewaves with differnt speeds running
- count[0] = count[0] + 7;
- count[1] = count[1] + 5;
- count[2] = count[2] + 3;
- count[3] = count[3] + 2;
- // first plant the seed into the buffer
- //buffer[XY(sin8(count[0])/20, cos8(count[1])/17)] = CHSV (count[0] , 255, 255);
- //buffer[XY(sin8(count[2])/17, cos8(count[3])/17)] = CHSV (count[1] , 255, 255);
- Line(((sin8(count[0])/17)+(sin8(count[2])/17))/2, ((sin8(count[1])/17)+(sin8(count[3])/17))/2, // combine 2 wavefunctions for more variety of x
- ((sin8(count[3])/17)+(sin8(count[2])/17))/2, ((sin8(count[1])/17)+(sin8(count[2])/17))/2, count[0]); // and y, color just simple linear ramp
- // just some try and error:
- //Line(sin8(count[3])/17, cos8(count[1])/17, sin8(count[1])/17, cos8(count[2])/17, count[3]);
- //buffer[XY(quadwave8(count)/17, 4)] = CHSV (0 , 255, 255); // lines following different wave fonctions
- //buffer[XY(cubicwave8(count)/17, 6)] = CHSV (40 , 255, 255);
- //buffer[XY(triwave8(count)/17, 8)] = CHSV (80 , 255, 255);
- // duplicate the seed in the buffer
- //Caleidoscope4();
- // add buffer to leds
- ShowBuffer();
- // clear buffer
- ClearBuffer();
- // rotate leds
- Spiral(7,7,8,110);
- //DimmAll(230);
- FastLED.show();
- // do not delete the current leds, just fade them down for the tail effect
- //DimmAll(220);
- }
- // finds the right index for a S shaped matrix
- // ...maybe you need a different mapping for your setup
- 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 {
- // use that line only, if you have all rows beginning at the same side
- return (x * (WIDTH) + y);
- }
- }
- // scale the brightness of the screenbuffer down
- void DimmAll(byte value)
- {
- for(int i = 0; i < NUM_LEDS; i++)
- {
- leds[i].nscale8(value);
- }
- }
- /*
- Caleidoscope1 mirrors from source to A, B and C
- y
- | |
- | B | C
- |_______________
- | |
- |source | A
- |_______________ x
- */
- void Caleidoscope1() {
- for(int x = 0; x < WIDTH / 2 ; x++) {
- for(int y = 0; y < HEIGHT / 2; y++) {
- leds[XY( WIDTH - 1 - x, y )] = leds[XY( x, y )]; // copy to A
- leds[XY( x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // copy to B
- leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // copy to C
- }
- }
- }
- /*
- Caleidoscope2 rotates from source to A, B and C
- y
- | |
- | C | B
- |_______________
- | |
- |source | A
- |_______________ x
- */
- void Caleidoscope2() {
- for(int x = 0; x < WIDTH / 2 ; x++) {
- for(int y = 0; y < HEIGHT / 2; y++) {
- leds[XY( WIDTH - 1 - x, y )] = leds[XY( y, x )]; // rotate to A
- leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] = leds[XY( x, y )]; // rotate to B
- leds[XY( x, HEIGHT - 1 - y )] = leds[XY( y, x )]; // rotate to C
- }
- }
- }
- // adds the color of one quarter to the other 3
- void Caleidoscope3() {
- for(int x = 0; x < WIDTH / 2 ; x++) {
- for(int y = 0; y < HEIGHT / 2; y++) {
- leds[XY( WIDTH - 1 - x, y )] += leds[XY( y, x )]; // rotate to A
- leds[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += leds[XY( x, y )]; // rotate to B
- leds[XY( x, HEIGHT - 1 - y )] += leds[XY( y, x )]; // rotate to C
- }
- }
- }
- // add the complete buffer 3 times while rotating
- void Caleidoscope4() {
- for(int x = 0; x < WIDTH ; x++) {
- for(int y = 0; y < HEIGHT ; y++) {
- buffer[XY( WIDTH - 1 - x, y )] += buffer[XY( y, x )]; // rotate to A
- buffer[XY( WIDTH - 1 - x, HEIGHT - 1 - y )] += buffer[XY( x, y )]; // rotate to B
- buffer[XY( x, HEIGHT - 1 - y )] += buffer[XY( y, x )]; // rotate to C
- }
- }
- }
- void ShowBuffer() {
- for(int i = 0; i < NUM_LEDS ; i++) {
- leds[i] += buffer[i];
- }
- }
- void ClearBuffer() {
- for(int i = 0; i < NUM_LEDS ; i++) {
- buffer[i] = 0;
- }
- }
- // creates a little twister for softening
- void Spiral(int x,int y, int r, byte dimm) {
- for(int d = r; d >= 0; d--) { // from the outside to the inside
- for(int i = x-d; i <= x+d; i++) {
- leds[XY(i,y-d)] += leds[XY(i+1,y-d)]; // lowest row to the right
- 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)]; // right colum up
- 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)]; // upper row to the left
- 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)]; // left colum down
- leds[XY(x-d,i)].nscale8( dimm );}
- }
- }
- // Bresenham line algorythm
- 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(;;){
- buffer[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; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement