Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define DATA_PIN 11
- #define CLK_PIN 13
- #define DATA_PIN_1 26
- #define CLK_PIN_1 27
- #define COLOR_ORDER BGR
- #define LED_TYPE APA102
- #define BRIGHTNESS 50
- #define FRAMES_PER_SECOND 120
- // Helper functions for an two-dimensional XY matrix of pixels.
- // Simple 2-D demo code is included as well.
- //
- // XY(x,y) takes x and y coordinates and returns an LED index number,
- // for use like this: leds[ XY(x,y) ] == CRGB::Red;
- // No error checking is performed on the ranges of x and y.
- //
- // XYsafe(x,y) takes x and y coordinates and returns an LED index number,
- // for use like this: leds[ XY(x,y) ] == CRGB::Red;
- // Error checking IS performed on the ranges of x and y, and an
- // index of "-1" is returned. Special instructions below
- // explain how to use this without having to do your own error
- // checking every time you use this function.
- // This is a slightly more advanced technique, and
- // it REQUIRES SPECIAL ADDITIONAL setup, described below.
- // Params for width and height
- const uint8_t kMatrixWidth = 22;
- const uint8_t kMatrixHeight = 6;
- int Xvalue = kMatrixWidth;
- int Yvalue = kMatrixHeight;
- // Param for different pixel layouts
- const bool kMatrixSerpentineLayout = true;
- typedef void (*SimplePatternList[])();
- uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
- uint16_t XY( uint8_t x, uint8_t y)
- {
- uint16_t i;
- if( kMatrixSerpentineLayout == false) {
- i = (y * kMatrixWidth) + x;
- }
- if( kMatrixSerpentineLayout == true) {
- if( y & 0x01) {
- // Odd rows run backwards
- uint8_t reverseX = (kMatrixWidth - 1) - x;
- i = (y * kMatrixWidth) + reverseX;
- } else {
- // Even rows run forwards
- i = (y * kMatrixWidth) + x;
- }
- }
- return i;
- }
- #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
- CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
- CRGB* const in_leds( leds_plus_safety_pixel + 1);
- CRGB* const out_leds( leds_plus_safety_pixel + 1);
- uint16_t XYsafe( uint8_t x, uint8_t y)
- {
- if( x >= kMatrixWidth) return -1;
- if( y >= kMatrixHeight) return -1;
- return XY(x,y);
- }
- int stripnum[6][2] = {
- {0, 21},
- {22,43},
- {44,65},
- {66,87},
- {88, 109},
- {110,131}
- };
- int counter = 1;
- void setup() {
- FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN, COLOR_ORDER>(in_leds,NUM_LEDS);
- FastLED.addLeds<LED_TYPE,DATA_PIN_1,CLK_PIN_1, COLOR_ORDER>(out_leds,NUM_LEDS);
- FastLED.setBrightness( BRIGHTNESS );
- Serial.begin(9600);
- }
- SimplePatternList gPatterns = {
- RainbowSwirl_Out_Heart_In,
- //RainbowSwirl_In,
- //RainbowSwirl_Out,
- //RainbowSwirl_In_Out,
- // RainbowSwirl,
- // RainbowSwirl2,
- // RainbowSwirl3,
- // TwoSidedHeart2,
- Heart1
- };
- void loop()
- {
- gPatterns[gCurrentPatternNumber]();
- FastLED.show();
- FastLED.delay(1000/FRAMES_PER_SECOND);
- EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
- EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
- }
- void nextPattern()
- {
- // add one to the current pattern number, and wrap around at the end
- gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
- }
- void DrawOneFrame( byte startHue8, int8_t yHueDelta8, int8_t xHueDelta8)
- {
- byte lineStartHue = startHue8;
- for( byte y = 0; y < kMatrixHeight; y++) {
- lineStartHue += yHueDelta8;
- byte pixelHue = lineStartHue;
- for( byte x = 0; x < kMatrixWidth; x++) {
- pixelHue += xHueDelta8;
- in_leds[ XY(x, y)] = CHSV( pixelHue, 255, 255);
- }
- }
- }
- void rainbow_flash()
- {
- uint32_t ms = millis();
- int32_t yHueDelta32 = ((int32_t)cos16( ms * (27/1) ) * (350 / kMatrixWidth));
- int32_t xHueDelta32 = ((int32_t)cos16( ms * (39/1) ) * (310 / kMatrixHeight));
- DrawOneFrame( ms / 65536, yHueDelta32 / 32768, xHueDelta32 / 32768);
- if( ms < 5000 ) {
- FastLED.setBrightness( scale8( BRIGHTNESS, (ms * 256) / 5000));
- } else {
- FastLED.setBrightness(BRIGHTNESS);
- }
- FastLED.show();
- }
- void Heart1()
- {
- for(int y = 0; y<Yvalue; y++)
- {
- for(int x = 0; x<Xvalue; x++)
- {
- in_leds[XYsafe(x,y)] = CRGB::Red; //FILL ONE STRIP WITH RED
- in_leds[XYsafe(x,y-1)] = CRGB::Black; //REMOVE COLOR FROM PRIOR STRIP
- if(y == 0) {in_leds[XYsafe(x,Yvalue-1)] = CRGB::Black; } //REMOVE COLOR FROM PRIOR STRIP
- }
- FastLED.show();
- delay(10);
- }
- }
- void TwoSidedHeart() //FILLS HEART SOLID AND MOVES TO NEXT HEART
- {
- if((counter%3) == 0)
- {fill_gradient(in_leds, stripnum[0][0],CHSV(0,255,255),stripnum[0][1], CHSV(0,255,255),SHORTEST_HUES);
- fill_gradient(in_leds, stripnum[3][0],CHSV(0,255,255),stripnum[3][1], CHSV(0,255,255),SHORTEST_HUES);}
- else if((counter%2) == 0)
- {fill_gradient(in_leds, stripnum[2][0],CHSV(0,255,255),stripnum[2][1], CHSV(0,255,255),SHORTEST_HUES);
- fill_gradient(in_leds, stripnum[5][0],CHSV(0,255,255),stripnum[5][1], CHSV(0,255,255),SHORTEST_HUES);}
- else if((counter%1) == 0)
- {fill_gradient(in_leds, stripnum[1][0],CHSV(0,255,255),stripnum[1][1], CHSV(0,255,255),SHORTEST_HUES);
- fill_gradient(in_leds, stripnum[4][0],CHSV(0,255,255),stripnum[4][1], CHSV(0,255,255),SHORTEST_HUES);}
- FastLED.show();
- delay(1);
- fadeToBlackBy(in_leds,NUM_LEDS, 230);
- //fill_solid(leds, NUM_LEDS, CRGB:: Black);
- //FastLED.show();
- counter++;
- }
- void TwoSidedHeart2() //FILLS UP LEDS IN HEART FROM BOTTOM TO TOP
- {
- for(int x = 0; x<Xvalue; x++)
- {
- in_leds[XYsafe(x,counter)] = CRGB:: Red;
- in_leds[XYsafe(Xvalue-1 - x, counter+3)] = CRGB::Red;
- FastLED.show();
- delay(50);
- }
- counter++;
- if (counter >2) {counter = 0; fill_solid(in_leds, NUM_LEDS, CRGB::Black);}
- }
- void RainbowSwirl_In() //FILLS RAINBOW UP EACH STRIP
- {
- int colorhue = 255/Xvalue;
- for(int x = 0; x<Xvalue; x++)
- {
- for(int y = 0; y<Yvalue; y++)
- {
- in_leds[XYsafe(x,y)] = CHSV(colorhue*x,255,255);
- FastLED.show();
- //delay(5);
- fadeToBlackBy(in_leds, NUM_LEDS, NUM_LEDS/Xvalue);
- }
- }
- }
- void RainbowSwirl_Out() //FILLS RAINBOW UP EACH STRIP
- {
- int colorhue = 255/Xvalue;
- for(int x = 0; x<Xvalue; x++)
- {
- for(int y = 0; y<Yvalue; y++)
- {
- out_leds[XYsafe(x,y)] = CHSV(colorhue*x,255,255);
- FastLED.show();
- //delay(5);
- fadeToBlackBy(out_leds, NUM_LEDS, NUM_LEDS/Xvalue);
- }
- }
- }
- void RainbowSwirl2() //FILLS RAINBOW THROUGH ENTIRE LED
- {
- int colorhue = 255/NUM_LEDS;
- for(int x = 0; x<Xvalue; x++)
- {
- for(int y = 0; y<Yvalue; y++)
- {
- in_leds[XYsafe(x,y)] = CHSV(colorhue++,255,255);
- FastLED.show();
- delay(5);
- }
- }
- }
- void RainbowSwirl3() //FILLS EACH STRIP ONE BY ONE
- {
- int colorhue = 255/Xvalue;
- for(int y = 0; y<Yvalue; y++)
- {
- for(int x = 0; x<Xvalue; x++)
- {
- in_leds[XYsafe(x,y)] = CHSV(colorhue*x,255,255);
- }
- FastLED.show();
- delay(500);
- }
- }
- void RainbowSwirl_In_Out()
- {
- RainbowSwirl_In();
- RainbowSwirl_Out();
- }
- void RainbowSwirl_Out_Heart_In()
- {
- RainbowSwirl_In();
- Heart1();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement