Sky_Jones

Rainbow Palette

Mar 12th, 2020
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.45 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 42
  4. #define BRIGHTNESS  255
  5. #define LED_TYPE    WS2811
  6. #define COLOR_ORDER RGB
  7.  
  8. const uint8_t kMatrixWidth  = 50;
  9. const uint8_t kMatrixHeight = 4;
  10. const bool    kMatrixSerpentineLayout = true;
  11.  
  12.  
  13. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  14. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  15.  
  16. // The leds
  17. CRGB leds[kMatrixWidth * kMatrixHeight];
  18.  
  19. // The 16 bit version of our coordinates
  20. static uint16_t x;
  21. static uint16_t y;
  22. static uint16_t z;
  23.  
  24. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  25. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  26. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  27. // water.
  28. uint16_t speed = 1; // speed is set dynamically once we've started up
  29.  
  30. // Scale determines how far apart the pixels in our noise matrix are.  Try
  31. // changing these values around to see how it affects the motion of the display.  The
  32. // higher the value of scale, the more "zoomed out" the noise iwll be.  A value
  33. // of 1 will be so zoomed in, you'll mostly see solid colors.
  34. uint16_t scale = 30; // scale is set dynamically once we've started up
  35.  
  36. // This is the array that we keep our computed noise values in
  37. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  38.  
  39. CRGBPalette16 currentPalette( PartyColors_p );
  40. uint8_t       colorLoop = 1;
  41.  
  42. void setup() {
  43.   delay(3000);
  44.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  45.   LEDS.setBrightness(BRIGHTNESS);
  46.  
  47.   // Initialize our coordinates to some random values
  48.   x = random16();
  49.   y = random16();
  50.   z = random16();
  51. }
  52.  
  53.  
  54.  
  55. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  56. void fillnoise8() {
  57.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  58.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  59.   // The amount of data smoothing we're doing depends on "speed".
  60.   uint8_t dataSmoothing = 0;
  61.   if( speed < 50) {
  62.     dataSmoothing = 200 - (speed * 4);
  63.   }
  64.  
  65.   for(int i = 0; i < MAX_DIMENSION; i++) {
  66.     int ioffset = scale * i;
  67.     for(int j = 0; j < MAX_DIMENSION; j++) {
  68.       int joffset = scale * j;
  69.      
  70.       uint8_t data = inoise8(x + ioffset,y + joffset,z);
  71.  
  72.       // The range of the inoise8 function is roughly 16-238.
  73.       // These two operations expand those values out to roughly 0..255
  74.       // You can comment them out if you want the raw noise data.
  75.       data = qsub8(data,16);
  76.       data = qadd8(data,scale8(data,39));
  77.  
  78.       if( dataSmoothing ) {
  79.         uint8_t olddata = noise[i][j];
  80.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  81.         data = newdata;
  82.       }
  83.      
  84.       noise[i][j] = data;
  85.     }
  86.   }
  87.  
  88.   z += speed;
  89.  
  90.   // apply slow drift to X and Y, just for visual variation.
  91.   x += speed / 8;
  92.   y -= speed / 16;
  93. }
  94.  
  95. void mapNoiseToLEDsUsingPalette()
  96. {
  97.   static uint8_t ihue=0;
  98.  
  99.   for(int i = 0; i < kMatrixWidth; i++) {
  100.     for(int j = 0; j < kMatrixHeight; j++) {
  101.       // We use the value at the (i,j) coordinate in the noise
  102.       // array for our brightness, and the flipped value from (j,i)
  103.       // for our pixel's index into the color palette.
  104.  
  105.       uint8_t index = noise[j][i];
  106.       uint8_t bri =   noise[i][j];
  107.  
  108.       // if this palette is a 'loop', add a slowly-changing base value
  109.       if( colorLoop) {
  110.         index += ihue;
  111.       }
  112.  
  113.       // brighten up, as the color palette itself often contains the
  114.       // light/dark dynamic range desired
  115.       if( bri > 127 ) {
  116.         bri = 255;
  117.       } else {
  118.         bri = dim8_raw( bri * 2);
  119.       }
  120.  
  121.       CRGB color = ColorFromPalette( currentPalette, index, bri);
  122.       leds[XY(i,j)] = color;
  123.     }
  124.   }
  125.  
  126.   ihue+=1;
  127. }
  128.  
  129. void loop() {
  130.   // Periodically choose a new palette, speed, and scale
  131.   ChangePaletteAndSettingsPeriodically();
  132.  
  133.   // generate noise data
  134.   fillnoise8();
  135.  
  136.   // convert the noise data to colors in the LED array
  137.   // using the current palette
  138.   mapNoiseToLEDsUsingPalette();
  139.  
  140.   LEDS.show();
  141.   // delay(10);
  142. }
  143.  
  144.  
  145. // 1 = 5 sec per palette
  146. // 2 = 10 sec per palette
  147. // etc
  148. #define HOLD_PALETTES_X_TIMES_AS_LONG 3
  149.  
  150. void ChangePaletteAndSettingsPeriodically()
  151. {
  152.   uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
  153.   static uint8_t lastSecond = 99;
  154.  
  155.   if( lastSecond != secondHand) {
  156.     lastSecond = secondHand;
  157.     if( secondHand ==  0)  { currentPalette = CRGBPalette16(
  158.     CHSV( 0, 255, 255),  CHSV( 16, 255, 255),  CHSV( 32, 255, 255),  CHSV( 48, 255, 255),
  159.     CHSV( 64, 255, 255),  CHSV( 80, 255, 255),  CHSV( 96, 255, 255),  CHSV( 116, 255, 255),
  160.     CHSV( 128, 255, 255),  CHSV( 144, 255, 255),  CHSV( 152, 255, 255),  CHSV( 160, 255, 255),
  161.     CHSV( 180, 255, 255),  CHSV( 192, 255, 255),  CHSV( 208, 255, 255),  CHSV( 224, 255, 255) );
  162. speed = 5; scale = 30; colorLoop = 1;}
  163. }
  164. }
  165.  
  166. //
  167. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  168. //
  169. uint16_t XY( uint8_t x, uint8_t y)
  170. {
  171.   uint16_t i;
  172.   if( kMatrixSerpentineLayout == false) {
  173.     i = (y * kMatrixWidth) + x;
  174.   }
  175.   if( kMatrixSerpentineLayout == true) {
  176.     if( y & 0x01) {
  177.       // Odd rows run backwards
  178.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  179.       i = (y * kMatrixWidth) + reverseX;
  180.     } else {
  181.       // Even rows run forwards
  182.       i = (y * kMatrixWidth) + x;
  183.     }
  184.   }
  185.   return i;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment