atuline

inoise8_pal_demo.ino

Jun 1st, 2020
1,754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.33 KB | None | 0 0
  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.  * Realtime animation that doesn't use delays
  13.  *
  14.  * Refer to the FastLED noise.h and lib8tion.h routines for more information on these functions.
  15.  *
  16.  *
  17.  * Recommendations for high performance routines:
  18.  *
  19.  *  Don't use blocking delays, especially if you plan to use buttons for input.
  20.  *  Keep loops to a minimum, and don't use nested loops if you can avoid them (2D routines excepted).
  21.  *  Don't use floating point math unless you REALLY need the accuracy.
  22.  *  KNOW your data. Min values, max values, everything and then define the data type to match.
  23.  *  Let high school math and not elementary school arithmetic 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 the Arduino's built in math functions.
  25.  *  Define your animation's variables WITHIN the function unless you need to share them with another one.
  26.  *  Use 'static' if you need to remember variable values if they're defined within a function.
  27.  *  Use millis() as a realtime counter, and not count++.
  28.  *  Try to keep your animation call out of an EVERY_N_MILLIS() function.
  29.  *  Put FastLED.show() in your loop and not in the animaton function.
  30.  *  
  31.  */
  32.  
  33.  
  34. #define FASTLED_ALLOW_INTERRUPTS 0                // Used for ESP8266.
  35. #include "FastLED.h"
  36.  
  37.  
  38. #define LED_PIN     2
  39. #define CLK_PIN     D4
  40. #define BRIGHTNESS  255
  41. #define LED_TYPE    WS2812
  42. #define COLOR_ORDER GRB
  43.  
  44. #define NUM_LEDS 40
  45.  
  46. struct CRGB leds[NUM_LEDS];
  47.  
  48. CRGBPalette16 currentPalette(CRGB::Black);
  49. CRGBPalette16 targetPalette(OceanColors_p);
  50.  
  51.  
  52.  
  53. void setup() {
  54.  
  55.   Serial.begin(115200);
  56.   delay(1000);
  57.  
  58.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);                // WS2812
  59. //  LEDS.addLeds<LED_TYPE,LED_PIN,CLK_PIN, COLOR_ORDER>(leds,NUM_LEDS);         // APA102, WS8201
  60.  
  61.   LEDS.setBrightness(BRIGHTNESS);
  62.  
  63. } // setup()
  64.  
  65.  
  66.  
  67. void loop() {
  68.  
  69.   fillnoise8();  
  70.  
  71.   EVERY_N_MILLIS(10) {
  72.     nblendPaletteTowardPalette(currentPalette, targetPalette, 48);          // Blend towards the target palette over 48 iterations.
  73.   }
  74.  
  75.   EVERY_N_SECONDS(5) {                                                      // Change the target palette to a random one every 5 seconds.
  76.     uint8_t baseC=random8();
  77.     targetPalette = CRGBPalette16(CHSV(baseC+random8(32), 255, random8(128,255)),   // Create palettes with similar colours.
  78.                                   CHSV(baseC+random8(64), 255, random8(128,255)),
  79.                                   CHSV(baseC+random8(96), 192, random8(128,255)),
  80.                                   CHSV(baseC+random8(16), 255, random8(128,255)));
  81.   }
  82.  
  83.   LEDS.show();                                                              // Display the LED's at every loop cycle.
  84.  
  85. } // loop()
  86.  
  87.  
  88.  
  89. void fillnoise8() {
  90.  
  91.   #define scale 30                                                          // Don't change this programmatically or everything shakes.
  92.  
  93.   for(int i = 0; i < NUM_LEDS; i++) {                                       // Just ONE loop to fill up the LED array as all of the pixels change.
  94.     uint8_t index = inoise8(i*scale, millis()/10+i*scale);                   // Get a value from the noise function. I'm using both x and y axis.
  95.     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.
  96.   }
  97.  
  98. } // fillnoise8()
Add Comment
Please, Sign In to add comment