SHARE
TWEET

inoise8_pal_demo.ino

atuline Aug 5th, 2016 (edited) 4,408 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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    APA102
  39. #define COLOR_ORDER BGR
  40.  
  41. #define NUM_LEDS 40
  42.  
  43. struct CRGB leds[NUM_LEDS];
  44.  
  45. CRGBPalette16 currentPalette(CRGB::Black);
  46. CRGBPalette16 targetPalette(OceanColors_p);
  47.  
  48.  
  49.  
  50. void setup() {
  51.  
  52.   Serial.begin(57600);
  53.   delay(1000);
  54.  
  55. //  LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);                // WS2812
  56.   LEDS.addLeds<LED_TYPE,LED_PIN,CLK_PIN, COLOR_ORDER>(leds,NUM_LEDS);         // APA102, WS8201
  57.  
  58.   LEDS.setBrightness(BRIGHTNESS);
  59.  
  60. } // setup()
  61.  
  62.  
  63.  
  64. void loop() {
  65.  
  66.   EVERY_N_MILLIS(10) {
  67.     nblendPaletteTowardPalette(currentPalette, targetPalette, 48);          // Blend towards the target palette over 48 iterations
  68.     fillnoise8();                                                           // Update the LED array with noise at the new location. You can put this in its own EVERY_N_MILLIS() function to speed up/slow down.
  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.  
  77. } // loop()
  78.  
  79.  
  80.  
  81. void fillnoise8() {
  82.  
  83.   #define scale 30                                                          // Don't change this programmatically or everything shakes.
  84.  
  85.   static uint16_t dist;                                                     // A random number for our noise generator.
  86.  
  87.   for(int i = 0; i < NUM_LEDS; i++) {                                       // Just ONE loop to fill up the LED array as all of the pixels change.
  88.     uint8_t index = inoise8(i*scale, dist+i*scale);                         // Get a value from the noise function. I'm using both x and y axis.
  89.     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.
  90.   }
  91.   dist += beatsin8(10,1, 4);                                                // Moving along the distance (that random number we started out with). Vary it a bit with a sine wave.
  92.                                                                             // In some sketches, I've used millis() instead of an incremented counter. Works a treat.
  93. } // 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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top