Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Toys For LED Matrix Effects
- www.stefan-petrick.de/wordpress_beta
- */
- #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 80
- #define NUM_LEDS (WIDTH * HEIGHT)
- CRGB leds[NUM_LEDS];
- CRGB buffer[NUM_LEDS];
- byte count;
- void setup()
- {
- FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- }
- void loop()
- {
- count=count+5;
- // first plant the seed into the buffer
- buffer[XY(sin8(count)/17, cos8(count)/17)] = CHSV (160 , 255, 255); // the circle
- 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);
- 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
- 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;
- }
- }
- 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 );}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement