Advertisement
atuline

inoise8_mover.ino

Aug 5th, 2016
5,746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. /* inoise8_mover
  2.  *
  3.  * By: Andrew Tuline
  4.  *
  5.  * Date: February, 2017
  6.  *
  7.  * We've used sine waves and counting to move pixels around a strand. In this case, I'm using Perlin Noise to move a pixel up and down the strand.
  8.  *
  9.  * The advantage here is that it provides random natural movement without requiring lots of fancy math by joe programmer.
  10.  *
  11.  */
  12.  
  13.  
  14. #include "FastLED.h"
  15.  
  16. #if FASTLED_VERSION < 3001000
  17. #error "Requires FastLED 3.1 or later; check github for latest code."
  18. #endif
  19.  
  20. // Fixed definitions cannot change on the fly.
  21. #define LED_DT 12                                             // Data pin to connect to the strip.
  22. #define LED_CK 11                                             // Clock pin for WS2801 or APA102.
  23. #define COLOR_ORDER GRB                                       // It's GRB for WS2812 and BGR for APA102.
  24. #define LED_TYPE WS2812                                       // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit.
  25. #define NUM_LEDS 40                                           // Number of LED's.
  26.  
  27.  
  28. // Global variables can be changed on the fly.
  29. uint8_t max_bright = 128;                                      // Overall brightness definition. It can be changed on the fly.
  30.  
  31. struct CRGB leds[NUM_LEDS];
  32.  
  33. CRGBPalette16 currentPalette=LavaColors_p;
  34. CRGBPalette16 targetPalette=OceanColors_p;
  35. TBlendType    currentBlending;                                // NOBLEND or LINEARBLEND
  36.  
  37.  
  38. void setup() {
  39.  
  40.   Serial.begin(115200);                                        // Initialize serial port for debugging.
  41.   delay(1000);                                                // Soft startup to ease the flow of electrons.
  42.  
  43.   LEDS.addLeds<LED_TYPE,LED_DT,COLOR_ORDER>(leds,NUM_LEDS);
  44. //  LEDS.addLeds<LED_TYPE,LED_DT,LED_CK, COLOR_ORDER>(leds,NUM_LEDS);
  45.  
  46.   FastLED.setBrightness(max_bright);
  47.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);               // FastLED Power management set at 5V, 500mA.
  48. } // setup()
  49.  
  50.  
  51.  
  52. void loop() {
  53.  
  54.     inoise8_mover();
  55.     fadeToBlackBy(leds, NUM_LEDS, 4);                     // Update the LED array with noise at the new location
  56.    
  57.   EVERY_N_MILLISECONDS(10) {
  58.     nblendPaletteTowardPalette(currentPalette, targetPalette, 24);   // AWESOME palette blending capability.
  59.   }
  60.  
  61.   EVERY_N_SECONDS(5) {                                        // Change the target palette to a random one every 5 seconds.
  62.     targetPalette = CRGBPalette16(CHSV(random8(), 255, random8(128,255)),
  63.                                   CHSV(random8(), 255, random8(128,255)),
  64.                                   CHSV(random8(), 192, random8(128,255)),
  65.                                   CHSV(random8(), 255, random8(128,255)));
  66.   }
  67.   FastLED.show();
  68.  
  69. } // loop()
  70.  
  71.  
  72.  
  73. void inoise8_mover() {
  74.  
  75.   static uint16_t dist;
  76.  
  77.   for (int i=0; i<3; i++) {
  78.     uint8_t locn = inoise8(millis()*8/64+i*500, millis()*8/64);                 // Get a new pixel location from moving noise.
  79.     uint8_t pixlen = map(locn,50,192,0,NUM_LEDS-1)%(NUM_LEDS-1);                // Map that to the length of the strand, and ensure we don't go over.
  80.     leds[pixlen] = ColorFromPalette(currentPalette, pixlen, 255, LINEARBLEND);  // Use that value for both the location as well as the palette index colour for the pixel.
  81.   }                                            
  82. } // inoise8_mover()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement