Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<FastLED.h>
- #define LED_PIN 10
- #define BRIGHTNESS 96
- #define NUM_LEDS 50
- #define CLOCK_PIN 9
- #define LED_TYPE WS2801
- #define COLOR_ORDER GRB
- #define BUTTON_PIN 6
- int ledMode = 0;
- const uint8_t kMatrixWidth = 16;
- const uint8_t kMatrixHeight = 16;
- const bool kMatrixSerpentineLayout = true;
- const TProgmemPalette16 DoopColors_p PROGMEM =
- {
- CRGB::Purple,
- CRGB::Black,
- CRGB::Red,
- CRGB::Black,
- CRGB::Purple,
- CRGB::Purple,
- CRGB::Red,
- CRGB::Red,
- CRGB::DarkViolet,
- CRGB::Black,
- CRGB::DarkRed,
- CRGB::Black,
- CRGB::Purple,
- CRGB::Purple,
- CRGB::Crimson,
- CRGB::Crimson
- };
- unsigned long keyPrevMillis = 0;
- const unsigned long keySampleIntervalMs = 25;
- byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
- byte longKeyPressCount = 0;
- byte prevKeyState = HIGH;
- #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;
- uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
- CRGBPalette16 currentPalette( PartyColors_p );
- uint8_t colorLoop = 1;
- void setup() {
- delay(3000);
- FastLED.addLeds<WS2801, LED_PIN, CLOCK_PIN, RGB>(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() {
- // Periodically choose a new palette, speed, and scale
- ChangePaletteAndSettingsPeriodically();
- // generate noise data
- fillnoise8();
- // convert the noise data to colors in the LED array
- // using the current palette
- mapNoiseToLEDsUsingPalette();
- LEDS.show();
- // delay(10);
- }
- //#define HOLD_PALETTES_X_TIMES_AS_LONG 10
- void ChangePaletteAndSettingsPeriodically()
- {
- byte currKeyState = digitalRead(BUTTON_PIN);
- if ((prevKeyState == LOW) && (currKeyState == HIGH))
- prevKeyState = currKeyState;
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1; /* motion speed */
- switch (ledMode) {
- case 0:
- {currentPalette = RainbowColors_p; speed = 20; scale = 30; colorLoop = 1;}
- break;
- case 1:
- { currentPalette = DoopColors_p; speed = 8; scale = 50; colorLoop = 0; }
- break;
- case 2:
- { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 30; colorLoop = 1; }
- break;
- case 3:
- { currentPalette = ForestColors_p; speed = 8; scale =120; colorLoop = 0; }
- break;
- case 4:
- { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 0; }
- break;
- case 5:
- { currentPalette = LavaColors_p; speed = 8; scale = 50; colorLoop = 0; }
- break;
- case 6:
- { currentPalette = OceanColors_p; speed = 20; scale = 90; colorLoop = 0; }
- break;
- case 7:
- { currentPalette = PartyColors_p; speed = 20; scale = 30; colorLoop = 1; }
- break;
- case 8:
- { SetupRandomPalette(); speed = 20; scale = 20; colorLoop = 1; }
- break;
- case 9:
- { SetupRandomPalette(); speed = 50; scale = 50; colorLoop = 1; }
- break;
- case 10:
- { SetupRandomPalette(); speed = 90; scale = 90; colorLoop = 1; }
- break;
- case 11:
- { currentPalette = RainbowStripeColors_p; speed = 30; scale = 20; colorLoop = 1; }
- break;
- case 12:
- { SetupPurpleAndGreenPalette(); speed = 10; scale = 50; colorLoop = 1; }
- break;
- }
- // if( lastSecond != secondHand) {
- // lastSecond = secondHand;
- // if( secondHand == 0) { currentPalette = RainbowColors_p; speed = 20; scale = 30; colorLoop = 1; }
- // if( secondHand == 5) { SetupPurpleAndGreenPalette(); speed = 10; scale = 50; colorLoop = 1; }
- // if( secondHand == 10) { SetupBlackAndWhiteStripedPalette(); speed = 20; scale = 30; colorLoop = 1; }
- // if( secondHand == 15) { currentPalette = ForestColors_p; speed = 8; scale =120; colorLoop = 0; }
- // if( secondHand == 20) { currentPalette = CloudColors_p; speed = 4; scale = 30; colorLoop = 0; }
- // if( secondHand == 25) { currentPalette = LavaColors_p; speed = 8; scale = 50; colorLoop = 0; }
- // if( secondHand == 30) { currentPalette = OceanColors_p; speed = 20; scale = 90; colorLoop = 0; }
- // if( secondHand == 35) { currentPalette = PartyColors_p; speed = 20; scale = 30; colorLoop = 1; }
- // if( secondHand == 40) { SetupRandomPalette(); speed = 20; scale = 20; colorLoop = 1; }
- // if( secondHand == 45) { SetupRandomPalette(); speed = 50; scale = 50; colorLoop = 1; }
- // if( secondHand == 50) { SetupRandomPalette(); speed = 90; scale = 90; colorLoop = 1; }
- // if( secondHand == 55) { currentPalette = RainbowStripeColors_p; speed = 30; scale = 20; colorLoop = 1; }
- // }
- }
- void SetupRandomPalette()
- {
- currentPalette = CRGBPalette16(
- CHSV( random8(), 255, 32),
- CHSV( random8(), 255, 255),
- CHSV( random8(), 128, 255),
- CHSV( random8(), 255, 255));
- }
- // This function sets up a palette of black and white stripes,
- // using code. Since the palette is effectively an array of
- // sixteen CRGB colors, the various fill_* functions can be used
- // to set them up.
- void SetupBlackAndWhiteStripedPalette()
- {
- // 'black out' all 16 palette entries...
- fill_solid( currentPalette, 16, CRGB::Black);
- // and set every fourth one to white.
- currentPalette[0] = CRGB::White;
- currentPalette[4] = CRGB::White;
- currentPalette[8] = CRGB::White;
- currentPalette[12] = CRGB::White;
- }
- // This function sets up a palette of purple and green stripes.
- void SetupPurpleAndGreenPalette()
- {
- CRGB purple = CHSV( HUE_PURPLE, 255, 255);
- CRGB green = CHSV( HUE_GREEN, 255, 255);
- CRGB black = CRGB::Black;
- currentPalette = CRGBPalette16(
- green, green, black, black,
- purple, purple, black, black,
- green, green, black, black,
- purple, purple, black, black );
- }
- //
- // 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