daily pastebin goal
20%
SHARE
TWEET

inoise8_pal_demo.ino

atuline Aug 5th, 2016 3,607 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. /* Title: inoise8_pal_demo.ino
  2.  *
  3.  * By: Andrew Tuline
  4.  *
  5.  * Date: August, 2016
  6.  *
  7.  * This short sketch demonstrates some of the functions of FastLED, including:
  8.  *
  9.  * Perlin noise
  10.  * Palettes
  11.  * Palette blending
  12.  * Alternatives to blocking delays
  13.  * Beats (and not the Dr. Dre kind, but rather the sinewave kind)
  14.  *
  15.  * Refer to the FastLED noise.h and lib8tion.h routines for more information on these functions.
  16.  *
  17.  *
  18.  * Recommendations for high performance routines:
  19.  *
  20.  *  Don't use blocking delays, especially if you plan to use buttons for input.
  21.  *  Keep loops to a minimum, and don't use nested loops.
  22.  *  Don't use floating point math. It's slow. Use 8 bit where possible.
  23.  *  Let high school and not elementary school math do the work for you, i.e. don't just count pixels; use sine waves or other math functions instead.
  24.  *  FastLED math functions are faster than built in math functions.
  25.  *  
  26.  */
  27.  
  28.  
  29. #include "FastLED.h"
  30.  
  31. #if FASTLED_VERSION < 3001000
  32. #error "Requires FastLED 3.1 or later; check github for latest code."
  33. #endif
  34.  
  35. #define LED_PIN     12
  36. #define CLK_PIN     11
  37. #define BRIGHTNESS  255
  38. #define LED_TYPE    WS2812      // Only use the LED_PIN for WS2812's
  39. #define COLOR_ORDER GRB
  40.  
  41. #define NUM_LEDS 40
  42.  
  43. struct CRGB leds[NUM_LEDS];
  44.  
  45. static uint16_t dist;         // A random number for our noise generator.
  46. uint16_t scale = 30;          // Wouldn't recommend changing this on the fly, or the animation will be really blocky.
  47. uint8_t maxChanges = 48;      // Value for blending between palettes.
  48.  
  49. CRGBPalette16 currentPalette(CRGB::Black);
  50. CRGBPalette16 targetPalette(OceanColors_p);
  51.  
  52.  
  53. void setup() {
  54.   Serial.begin(57600);
  55.   delay(3000);
  56.  
  57.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  58. //  LEDS.addLeds<LED_TYPE,LED_PIN,CLK_PIN, COLOR_ORDER>(leds,NUM_LEDS);
  59.  
  60.   LEDS.setBrightness(BRIGHTNESS);
  61.   dist = random16(12345);          // A semi-random number for our noise generator
  62. } // setup()
  63.  
  64.  
  65. void loop() {
  66.   EVERY_N_MILLISECONDS(10) {
  67.     nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);  // Blend towards the target palette
  68.     fillnoise8();                                                           // Update the LED array with noise at the new location
  69.   }
  70.  
  71.   EVERY_N_SECONDS(5) {             // Change the target palette to a random one every 5 seconds.
  72.     targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128,255)), CHSV(random8(), 255, random8(128,255)), CHSV(random8(), 192, random8(128,255)), CHSV(random8(), 255, random8(128,255)));
  73.   }
  74.  
  75.   LEDS.show();                    // Display the LED's at every loop cycle.
  76. } // loop()
  77.  
  78.  
  79. void fillnoise8() {
  80.   for(int i = 0; i < NUM_LEDS; i++) {                                      // Just ONE loop to fill up the LED array as all of the pixels change.
  81.     uint8_t index = inoise8(i*scale, dist+i*scale) % 255;                  // Get a value from the noise function. I'm using both x and y axis.
  82.     leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND);   // With that value, look up the 8 bit colour palette value and assign it to the current LED.
  83.   }
  84.   dist += beatsin8(10,1, 4);                                               // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave.
  85.                                                                            // In some sketches, I've used millis() instead of an incremented counter. Works a treat.
  86. } // fillnoise8()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top