Sky_Jones

Rainbow Noise

Jul 7th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.44 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 6
  4. #define BRIGHTNESS  255
  5. #define LED_TYPE    WS2811
  6. #define COLOR_ORDER RGB
  7.  
  8. const uint8_t kMatrixWidth  = 4;
  9. const uint8_t kMatrixHeight = 2;
  10. const bool    kMatrixSerpentineLayout = true;
  11.  
  12.  
  13. // This example combines two features of FastLED to produce a remarkable range of
  14. // effects from a relatively small amount of code.  This example combines FastLED's
  15. // color palette lookup functions with FastLED's Perlin/simplex noise generator, and
  16. // the combination is extremely powerful.
  17. //
  18. // You might want to look at the "ColorPalette" and "Noise" examples separately
  19. // if this example code seems daunting.
  20. //
  21. //
  22. // The basic setup here is that for each frame, we generate a new array of
  23. // 'noise' data, and then map it onto the LED matrix through a color palette.
  24. //
  25. // Periodically, the color palette is changed, and new noise-generation parameters
  26. // are chosen at the same time.  In this example, specific noise-generation
  27. // values have been selected to match the given color palettes; some are faster,
  28. // or slower, or larger, or smaller than others, but there's no reason these
  29. // parameters can't be freely mixed-and-matched.
  30. //
  31. // In addition, this example includes some fast automatic 'data smoothing' at
  32. // lower noise speeds to help produce smoother animations in those cases.
  33. //
  34. // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
  35. // used, as well as some 'hand-defined' ones, and some proceedurally generated
  36. // palettes.
  37.  
  38.  
  39. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  40. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  41.  
  42. // The leds
  43. CRGB leds[kMatrixWidth * kMatrixHeight];
  44.  
  45. // The 16 bit version of our coordinates
  46. static uint16_t x;
  47. static uint16_t y;
  48. static uint16_t z;
  49.  
  50. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  51. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  52. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  53. // water.
  54. uint16_t speed = 1; // speed is set dynamically once we've started up
  55.  
  56. // Scale determines how far apart the pixels in our noise matrix are.  Try
  57. // changing these values around to see how it affects the motion of the display.  The
  58. // higher the value of scale, the more "zoomed out" the noise iwll be.  A value
  59. // of 1 will be so zoomed in, you'll mostly see solid colors.
  60. uint16_t scale = 30; // scale is set dynamically once we've started up
  61.  
  62. // This is the array that we keep our computed noise values in
  63. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  64.  
  65. CRGBPalette16 currentPalette( PartyColors_p );
  66. uint8_t       colorLoop = 1;
  67.  
  68. void setup() {
  69.   delay(3000);
  70.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  71.   LEDS.setBrightness(BRIGHTNESS);
  72.  
  73.   // Initialize our coordinates to some random values
  74.   x = random16();
  75.   y = random16();
  76.   z = random16();
  77. }
  78.  
  79.  
  80.  
  81. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  82. void fillnoise8() {
  83.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  84.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  85.   // The amount of data smoothing we're doing depends on "speed".
  86.   uint8_t dataSmoothing = 0;
  87.   if( speed < 50) {
  88.     dataSmoothing = 200 - (speed * 4);
  89.   }
  90.  
  91.   for(int i = 0; i < MAX_DIMENSION; i++) {
  92.     int ioffset = scale * i;
  93.     for(int j = 0; j < MAX_DIMENSION; j++) {
  94.       int joffset = scale * j;
  95.      
  96.       uint8_t data = inoise8(x + ioffset,y + joffset,z);
  97.  
  98.       // The range of the inoise8 function is roughly 16-238.
  99.       // These two operations expand those values out to roughly 0..255
  100.       // You can comment them out if you want the raw noise data.
  101.       data = qsub8(data,16);
  102.       data = qadd8(data,scale8(data,39));
  103.  
  104.       if( dataSmoothing ) {
  105.         uint8_t olddata = noise[i][j];
  106.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  107.         data = newdata;
  108.       }
  109.      
  110.       noise[i][j] = data;
  111.     }
  112.   }
  113.  
  114.   z += speed;
  115.  
  116.   // apply slow drift to X and Y, just for visual variation.
  117.   x += speed / 8;
  118.   y -= speed / 16;
  119. }
  120.  
  121. void mapNoiseToLEDsUsingPalette()
  122. {
  123.   static uint8_t ihue=0;
  124.  
  125.   for(int i = 0; i < kMatrixWidth; i++) {
  126.     for(int j = 0; j < kMatrixHeight; j++) {
  127.       // We use the value at the (i,j) coordinate in the noise
  128.       // array for our brightness, and the flipped value from (j,i)
  129.       // for our pixel's index into the color palette.
  130.  
  131.       uint8_t index = noise[j][i];
  132.       uint8_t bri =   noise[i][j];
  133.  
  134.       // if this palette is a 'loop', add a slowly-changing base value
  135.       if( colorLoop) {
  136.         index += ihue;
  137.       }
  138.  
  139.       // brighten up, as the color palette itself often contains the
  140.       // light/dark dynamic range desired
  141.       if( bri > 127 ) {
  142.         bri = 255;
  143.       } else {
  144.         bri = dim8_raw( bri * 2);
  145.       }
  146.  
  147.       CRGB color = ColorFromPalette( currentPalette, index, bri);
  148.       leds[XY(i,j)] = color;
  149.     }
  150.   }
  151.  
  152.   ihue+=1;
  153. }
  154.  
  155. void loop() {
  156.   // Periodically choose a new palette, speed, and scale
  157.   ChangePaletteAndSettingsPeriodically();
  158.  
  159.   // generate noise data
  160.   fillnoise8();
  161.  
  162.   // convert the noise data to colors in the LED array
  163.   // using the current palette
  164.   mapNoiseToLEDsUsingPalette();
  165.  
  166.   LEDS.show();
  167.   // delay(10);
  168. }
  169.  
  170.  
  171.  
  172. // There are several different palettes of colors demonstrated here.
  173. //
  174. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  175. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  176. //
  177. // Additionally, you can manually define your own color palettes, or you can write
  178. // code that creates color palettes on the fly.
  179.  
  180. // 1 = 5 sec per palette
  181. // 2 = 10 sec per palette
  182. // etc
  183. #define HOLD_PALETTES_X_TIMES_AS_LONG 3
  184.  
  185. void ChangePaletteAndSettingsPeriodically()
  186. {
  187.   uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
  188.   static uint8_t lastSecond = 99;
  189.  
  190.   if( lastSecond != secondHand) {
  191.     lastSecond = secondHand;
  192.     if( secondHand ==  0)  { currentPalette = CRGBPalette16(
  193.     CHSV( 0, 255, 255),  CHSV( 16, 255, 255),  CHSV( 32, 255, 255),  CHSV( 48, 255, 255),
  194.     CHSV( 64, 255, 255),  CHSV( 80, 255, 255),  CHSV( 96, 255, 255),  CHSV( 116, 255, 255),
  195.     CHSV( 128, 255, 255),  CHSV( 144, 255, 255),  CHSV( 152, 255, 255),  CHSV( 160, 255, 255),
  196.     CHSV( 180, 255, 255),  CHSV( 192, 255, 255),  CHSV( 208, 255, 255),  CHSV( 224, 255, 255) );
  197. speed = 5; scale = 30; colorLoop = 1;}
  198. }
  199. }
  200. // This function generates a random palette that's a gradient
  201. // between four different colors.  The first is a dim hue, the second is
  202. // a bright hue, the third is a bright pastel, and the last is
  203. // another bright hue.  This gives some visual bright/dark variation
  204. // which is more interesting than just a gradient of different hues.
  205.  
  206.  
  207. //
  208. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  209. //
  210. uint16_t XY( uint8_t x, uint8_t y)
  211. {
  212.   uint16_t i;
  213.   if( kMatrixSerpentineLayout == false) {
  214.     i = (y * kMatrixWidth) + x;
  215.   }
  216.   if( kMatrixSerpentineLayout == true) {
  217.     if( y & 0x01) {
  218.       // Odd rows run backwards
  219.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  220.       i = (y * kMatrixWidth) + reverseX;
  221.     } else {
  222.       // Even rows run forwards
  223.       i = (y * kMatrixWidth) + x;
  224.     }
  225.   }
  226.   return i;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment