Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define LED_PIN 6
- #define BRIGHTNESS 255
- #define LED_TYPE WS2811
- #define COLOR_ORDER RGB
- const uint8_t kMatrixWidth = 10;
- const uint8_t kMatrixHeight = 4;
- const bool kMatrixSerpentineLayout = true;
- #define UPDATES_PER_SECOND 100
- #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
- #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
- // The leds
- CRGB leds[kMatrixWidth * kMatrixHeight];
- // The 16 bit version of our coordinates
- static uint16_t x;
- static uint16_t y;
- static uint16_t z;
- // We're using the x/y dimensions to map to the x/y pixels on the matrix. We'll
- // use the z-axis for "time". speed determines how fast time moves forward. Try
- // 1 for a very slow moving effect, or 60 for something that ends up looking like
- // water.
- uint16_t speed = 1; // speed is set dynamically once we've started up
- // Scale determines how far apart the pixels in our noise matrix are. Try
- // changing these values around to see how it affects the motion of the display. The
- // higher the value of scale, the more "zoomed out" the noise iwll be. A value
- // of 1 will be so zoomed in, you'll mostly see solid colors.
- uint16_t scale = 30; // scale is set dynamically once we've started up
- // This is the array that we keep our computed noise values in
- uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
- CRGBPalette16 currentPalette( CRGB::Black );
- CRGBPalette16 targetPalette( CRGB::Black );
- uint8_t colorLoop = 1;
- void setup() {
- delay(3000);
- LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
- LEDS.setBrightness(BRIGHTNESS);
- // Initialize our coordinates to some random values
- x = random16();
- y = random16();
- z = random16();
- }
- // Fill the x/y array of 8-bit noise values using the inoise8 function.
- void fillnoise8() {
- // If we're runing at a low "speed", some 8-bit artifacts become visible
- // from frame-to-frame. In order to reduce this, we can do some fast data-smoothing.
- // The amount of data smoothing we're doing depends on "speed".
- uint8_t dataSmoothing = 0;
- if( speed < 50) {
- dataSmoothing = 200 - (speed * 4);
- }
- for(int i = 0; i < MAX_DIMENSION; i++) {
- int ioffset = scale * i;
- for(int j = 0; j < MAX_DIMENSION; j++) {
- int joffset = scale * j;
- uint8_t data = inoise8(x + ioffset,y + joffset,z);
- // The range of the inoise8 function is roughly 16-238.
- // These two operations expand those values out to roughly 0..255
- // You can comment them out if you want the raw noise data.
- data = qsub8(data,16);
- data = qadd8(data,scale8(data,39));
- if( dataSmoothing ) {
- uint8_t olddata = noise[i][j];
- uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
- data = newdata;
- }
- noise[i][j] = data;
- }
- }
- z += speed;
- // apply slow drift to X and Y, just for visual variation.
- x += speed / 8;
- y -= speed / 16;
- }
- void mapNoiseToLEDsUsingPalette()
- {
- static uint8_t ihue=0;
- for(int i = 0; i < kMatrixWidth; i++) {
- for(int j = 0; j < kMatrixHeight; j++) {
- // We use the value at the (i,j) coordinate in the noise
- // array for our brightness, and the flipped value from (j,i)
- // for our pixel's index into the color palette.
- uint8_t index = noise[j][i];
- uint8_t bri = noise[i][j];
- // if this palette is a 'loop', add a slowly-changing base value
- if( colorLoop) {
- index += ihue;
- }
- // brighten up, as the color palette itself often contains the
- // light/dark dynamic range desired
- if( bri > 127 ) {
- bri = 255;
- } else {
- bri = dim8_raw( bri * 2);
- }
- CRGB color = ColorFromPalette( currentPalette, index, bri);
- leds[XY(i,j)] = color;
- }
- }
- ihue+=1;
- }
- void loop() {
- ChangePalettePeriodically();
- // Crossfade current palette slowly toward the target palette
- //
- // Each time that nblendPaletteTowardPalette is called, small changes
- // are made to currentPalette to bring it closer to matching targetPalette.
- // You can control how many changes are made in each call:
- // - the default of 24 is a good balance
- // - meaningful values are 1-48. 1=veeeeeeeery slow, 48=quickest
- // - "0" means do not change the currentPalette at all; freeze
- uint8_t maxChanges = 6;
- nblendPaletteTowardPalette( currentPalette, targetPalette, maxChanges);
- // generate noise data
- fillnoise8();
- // convert the noise data to colors in the LED array
- // using the current palette
- mapNoiseToLEDsUsingPalette();
- LEDS.show();
- // delay(10);
- }
- // There are several different palettes of colors demonstrated here.
- //
- // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
- // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
- //
- // Additionally, you can manually define your own color palettes, or you can write
- // code that creates color palettes on the fly.
- // 1 = 5 sec per palette
- // 2 = 10 sec per palette
- // etc
- #define HOLD_PALETTES_X_TIMES_AS_LONG 3
- void ChangePalettePeriodically()
- {
- uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
- static uint8_t lastSecond = 99;
- if( lastSecond != secondHand) {
- lastSecond = secondHand;
- /* if( secondHand == 0) { targetPalette = CRGBPalette16( //White
- CHSV( 0, 0, 255), CHSV( 0, 0, 224), CHSV( 0, 0, 192), CHSV( 0, 0, 160),
- CHSV( 0, 0, 128), CHSV( 0, 0, 96), CHSV( 0, 0, 64), CHSV( 0, 0, 32),
- CHSV( 0, 0, 0), CHSV( 0, 0, 32), CHSV( 0, 0, 64), CHSV( 0, 0, 96),
- CHSV( 0, 0, 128), CHSV( 0, 0, 160), CHSV( 0, 0, 192), CHSV( 0, 0, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- */ if( secondHand == 3) { targetPalette = CRGBPalette16( //Red
- CHSV( 0, 255, 255), CHSV( 0, 255, 224), CHSV( 0, 255, 192), CHSV( 0, 255, 160),
- CHSV( 0, 255, 128), CHSV( 0, 255, 96), CHSV( 0, 255, 64), CHSV( 0, 255, 32),
- CHSV( 0, 255, 0), CHSV( 0, 255, 32), CHSV( 0, 255, 64), CHSV( 0, 255, 96),
- CHSV( 0, 255, 128), CHSV( 0, 255, 160), CHSV( 0, 255, 192), CHSV( 0, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 6) { targetPalette = CRGBPalette16( //Orange
- CHSV( 16, 255, 255), CHSV( 16, 255, 224), CHSV( 16, 255, 192), CHSV( 16, 255, 160),
- CHSV( 16, 255, 128), CHSV( 16, 255, 96), CHSV( 16, 255, 64), CHSV( 16, 255, 32),
- CHSV( 16, 255, 0), CHSV( 16, 255, 32), CHSV( 16, 255, 64), CHSV( 16, 255, 96),
- CHSV( 16, 255, 128), CHSV( 16, 255, 160), CHSV( 16, 255, 192), CHSV( 16, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 9) { targetPalette = CRGBPalette16( //Yellow
- CHSV( 64, 255, 255), CHSV( 64, 255, 224), CHSV( 64, 255, 192), CHSV( 64, 255, 160),
- CHSV( 64, 255, 128), CHSV( 64, 255, 96), CHSV( 64, 255, 64), CHSV( 64, 255, 32),
- CHSV( 64, 255, 0), CHSV( 64, 255, 32), CHSV( 64, 255, 64), CHSV( 64, 255, 96),
- CHSV( 64, 255, 128), CHSV( 64, 255, 160), CHSV( 64, 255, 192), CHSV( 64, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 12) { targetPalette = CRGBPalette16( //Green
- CHSV( 96, 255, 255), CHSV( 96, 255, 224), CHSV( 96, 255, 192), CHSV( 96, 255, 160),
- CHSV( 96, 255, 128), CHSV( 96, 255, 96), CHSV( 96, 255, 64), CHSV( 96, 255, 32),
- CHSV( 96, 255, 0), CHSV( 96, 255, 32), CHSV( 96, 255, 64), CHSV( 96, 255, 96),
- CHSV( 96, 255, 128), CHSV( 96, 255, 160), CHSV( 96, 255, 192), CHSV( 96, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 15) { targetPalette = CRGBPalette16( //Aqua
- CHSV( 128, 255, 255), CHSV( 128, 255, 224), CHSV( 128, 255, 192), CHSV( 128, 255, 160),
- CHSV( 128, 255, 128), CHSV( 128, 255, 96), CHSV( 128, 255, 64), CHSV( 128, 255, 32),
- CHSV( 128, 255, 0), CHSV( 128, 255, 32), CHSV( 128, 255, 64), CHSV( 128, 255, 96),
- CHSV( 128, 255, 128), CHSV( 128, 255, 160), CHSV( 128, 255, 192), CHSV( 128, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 18) { targetPalette = CRGBPalette16( //Blue
- CHSV( 160, 255, 255), CHSV( 160, 255, 224), CHSV( 160, 255, 192), CHSV( 160, 255, 160),
- CHSV( 160, 255, 128), CHSV( 160, 255, 96), CHSV( 160, 255, 64), CHSV( 160, 255, 32),
- CHSV( 160, 255, 0), CHSV( 160, 255, 32), CHSV( 160, 255, 64), CHSV( 160, 255, 96),
- CHSV( 160, 255, 128), CHSV( 160, 255, 160), CHSV( 160, 255, 192), CHSV( 160, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 21) { targetPalette = CRGBPalette16( //Purple
- CHSV( 192, 255, 255), CHSV( 192, 255, 224), CHSV( 192, 255, 192), CHSV( 192, 255, 160),
- CHSV( 192, 255, 128), CHSV( 192, 255, 96), CHSV( 192, 255, 64), CHSV( 192, 255, 32),
- CHSV( 192, 255, 0), CHSV( 192, 255, 32), CHSV( 192, 255, 64), CHSV( 192, 255, 96),
- CHSV( 192, 255, 128), CHSV( 192, 255, 160), CHSV( 192, 255, 192), CHSV( 192, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 24) { targetPalette = CRGBPalette16( //Pink
- CHSV( 224, 255, 255), CHSV( 224, 255, 224), CHSV( 224, 255, 192), CHSV( 224, 255, 160),
- CHSV( 224, 255, 128), CHSV( 224, 255, 96), CHSV( 224, 255, 64), CHSV( 224, 255, 32),
- CHSV( 224, 255, 0), CHSV( 224, 255, 32), CHSV( 224, 255, 64), CHSV( 224, 255, 96),
- CHSV( 224, 255, 128), CHSV( 224, 255, 160), CHSV( 224, 255, 192), CHSV( 224, 255, 224) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 27) { targetPalette = CRGBPalette16( //Rainbow Strobe
- CHSV( 0, 255, 255), CHSV( 16, 255, 255), CHSV( 32, 255, 255), CHSV( 48, 255, 255),
- CHSV( 64, 255, 255), CHSV( 80, 255, 255), CHSV( 96, 255, 255), CHSV( 116, 255, 255),
- CHSV( 128, 255, 255), CHSV( 144, 255, 255), CHSV( 152, 255, 255), CHSV( 160, 255, 255),
- CHSV( 180, 255, 255), CHSV( 192, 255, 255), CHSV( 208, 255, 255), CHSV( 224, 255, 255) );
- speed = 1; scale = 1; colorLoop = 1; }
- if( secondHand == 30) { targetPalette = CRGBPalette16( //Red-Orange-Yellow
- CHSV( 0, 255, 255), CHSV( 8, 255, 255), CHSV( 16, 255, 255), CHSV( 24, 255, 255),
- CHSV( 32, 255, 255), CHSV( 40, 255, 255), CHSV( 48, 255, 255), CHSV( 56, 255, 255),
- CHSV( 64, 255, 255), CHSV( 56, 255, 255), CHSV( 48, 255, 255), CHSV( 40, 255, 255),
- CHSV( 32, 255, 255), CHSV( 24, 255, 255), CHSV( 16, 255, 255), CHSV( 8, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 33) { targetPalette = CRGBPalette16( //Orange-Yellow-Green
- CHSV( 32, 255, 255), CHSV( 40, 255, 255), CHSV( 48, 255, 255), CHSV( 56, 255, 255),
- CHSV( 64, 255, 255), CHSV( 72, 255, 255), CHSV( 80, 255, 255), CHSV( 88, 255, 255),
- CHSV( 96, 255, 255), CHSV( 88, 255, 255), CHSV( 80, 255, 255), CHSV( 72, 255, 255),
- CHSV( 64, 255, 255), CHSV( 56, 255, 255), CHSV( 48, 255, 255), CHSV( 40, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 36) { targetPalette = CRGBPalette16( //Yellow-Green-Aqua
- CHSV( 64, 255, 255), CHSV( 72, 255, 255), CHSV( 80, 255, 255), CHSV( 88, 255, 255),
- CHSV( 96, 255, 255), CHSV( 104, 255, 255), CHSV( 112, 255, 255), CHSV( 120, 255, 255),
- CHSV( 128, 255, 255), CHSV( 120, 255, 255), CHSV( 112, 255, 255), CHSV( 104, 255, 255),
- CHSV( 96, 255, 255), CHSV( 88, 255, 255), CHSV( 80, 255, 255), CHSV( 72, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 39) { targetPalette = CRGBPalette16( //Green-Aqua-Blue
- CHSV( 96, 255, 255), CHSV( 104, 255, 255), CHSV( 112, 255, 255), CHSV( 120, 255, 255),
- CHSV( 128, 255, 255), CHSV( 136, 255, 255), CHSV( 144, 255, 255), CHSV( 152, 255, 255),
- CHSV( 160, 255, 255), CHSV( 152, 255, 255), CHSV( 144, 255, 255), CHSV( 136, 255, 255),
- CHSV( 128, 255, 255), CHSV( 120, 255, 255), CHSV( 112, 255, 255), CHSV( 104, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 42) { targetPalette = CRGBPalette16( //Aqua-Blue-Purple
- CHSV( 128, 255, 255), CHSV( 136, 255, 255), CHSV( 144, 255, 255), CHSV( 152, 255, 255),
- CHSV( 160, 255, 255), CHSV( 168, 255, 255), CHSV( 176, 255, 255), CHSV( 184, 255, 255),
- CHSV( 192, 255, 255), CHSV( 184, 255, 255), CHSV( 176, 255, 255), CHSV( 168, 255, 255),
- CHSV( 160, 255, 255), CHSV( 152, 255, 255), CHSV( 144, 255, 255), CHSV( 136, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 45) { targetPalette = CRGBPalette16( //Blue-Purple-Pink
- CHSV( 160, 255, 255), CHSV( 168, 255, 255), CHSV( 176, 255, 255), CHSV( 184, 255, 255),
- CHSV( 192, 255, 255), CHSV( 200, 255, 255), CHSV( 208, 255, 255), CHSV( 216, 255, 255),
- CHSV( 224, 255, 255), CHSV( 216, 255, 255), CHSV( 208, 255, 255), CHSV( 200, 255, 255),
- CHSV( 192, 255, 255), CHSV( 184, 255, 255), CHSV( 176, 255, 255), CHSV( 168, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 48) { targetPalette = CRGBPalette16( //Purple-Pink-Red
- CHSV( 192, 255, 255), CHSV( 200, 255, 255), CHSV( 208, 255, 255), CHSV( 216, 255, 255),
- CHSV( 224, 255, 255), CHSV( 232, 255, 255), CHSV( 240, 255, 255), CHSV( 248, 255, 255),
- CHSV( 0, 255, 255), CHSV( 248, 255, 255), CHSV( 240, 255, 255), CHSV( 232, 255, 255),
- CHSV( 224, 255, 255), CHSV( 216, 255, 255), CHSV( 208, 255, 255), CHSV( 200, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 51) { targetPalette = CRGBPalette16( //Purple-Red-Orange
- CHSV( 224, 255, 255), CHSV( 232, 255, 255), CHSV( 240, 255, 255), CHSV( 248, 255, 255),
- CHSV( 0, 255, 255), CHSV( 8, 255, 255), CHSV( 16, 255, 255), CHSV( 24, 255, 255),
- CHSV( 32, 255, 255), CHSV( 24, 255, 255), CHSV( 16, 255, 255), CHSV( 8, 255, 255),
- CHSV( 0, 255, 255), CHSV( 248, 255, 255), CHSV( 240, 255, 255), CHSV( 232, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 54) { targetPalette = CRGBPalette16( //Rainbow Noise
- CHSV( 0, 255, 255), CHSV( 16, 255, 255), CHSV( 32, 255, 255), CHSV( 48, 255, 255),
- CHSV( 64, 255, 255), CHSV( 80, 255, 255), CHSV( 96, 255, 255), CHSV( 116, 255, 255),
- CHSV( 128, 255, 255), CHSV( 144, 255, 255), CHSV( 152, 255, 255), CHSV( 160, 255, 255),
- CHSV( 180, 255, 255), CHSV( 192, 255, 255), CHSV( 208, 255, 255), CHSV( 224, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; }
- if( secondHand == 57) { targetPalette = CRGBPalette16( //Rainbow Noise
- CHSV( 0, 255, 255), CHSV( 16, 255, 255), CHSV( 32, 255, 255), CHSV( 48, 255, 255),
- CHSV( 64, 255, 255), CHSV( 80, 255, 255), CHSV( 96, 255, 255), CHSV( 116, 255, 255),
- CHSV( 128, 255, 255), CHSV( 144, 255, 255), CHSV( 152, 255, 255), CHSV( 160, 255, 255),
- CHSV( 180, 255, 255), CHSV( 192, 255, 255), CHSV( 208, 255, 255), CHSV( 224, 255, 255) );
- speed = 1; scale = 100; colorLoop = 1; } }
- }
- //
- // Mark's xy coordinate mapping code. See the XYMatrix for more information on it.
- //
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment