atuline

Updated template.

Jun 16th, 2020
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.13 KB | None | 0 0
  1. /* Display Template for FastLED
  2.  *
  3.  * By: Andrew Tuline
  4.  *
  5.  * Modified by: Andrew Tuline
  6.  *
  7.  * Date: July, 2015
  8.  *
  9.  * This is a simple non-blocking FastLED display sequence template.
  10.  *
  11.  *
  12.  */
  13.  
  14. #ifdef ESP8266
  15. #define FASTLED_ALLOW_INTERRUPTS 0                            // Used for ESP8266 with WS2812 LED's. Ugh!!!
  16. #endif
  17.  
  18. #include <FastLED.h>                                          // FastLED library.
  19.  
  20.  
  21. #define qsubd(x, b) ((x>b)?b:0)                               // Clip. . . . A digital unsigned subtraction macro. if result <0, then x=0. Otherwise, x=b.
  22. #define qsuba(x, b) ((x>b)?x-b:0)                             // Level shift. . . Unsigned subtraction macro. if result <0, then x=0. Otherwise x=x-b.
  23.  
  24.  
  25. // Fixed definitions cannot change on the fly.
  26. #define LED_DT 2                                             // Data pin to connect to the strip.
  27. #define LED_CK 11                                             // Clock pin for WS2801 or APA102.
  28. #define COLOR_ORDER GRB                                       // It's GRB for WS2812 and BGR for APA102.
  29. #define LED_TYPE WS2812                                       // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit.
  30. #define NUM_LEDS 40                                           // Number of LED's.
  31.  
  32. // Global variables can be changed on the fly.
  33. uint8_t max_bright = 128;                                      // Overall brightness.
  34.  
  35. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  36.  
  37. // Palette definitions
  38. CRGBPalette16 currentPalette;
  39. CRGBPalette16 targetPalette;
  40. TBlendType    currentBlending = LINEARBLEND;                                // NOBLEND or LINEARBLEND
  41.  
  42. // Define variables used by the sequences.
  43. int      twinkrate = 100;                                     // The higher the value, the lower the number of twinkles.
  44. uint8_t  thisdelay =  10;                                     // A delay value for the sequence(s).
  45. uint8_t   thisfade =   8;                                     // How quickly does it fade? Lower = slower fade rate.
  46. uint8_t    thishue =  50;                                     // The hue.
  47. uint8_t    thissat = 255;                                     // The saturation, where 255 = brilliant colours.
  48. uint8_t    thisbri = 255;                                     // Brightness of a sequence.
  49. bool       randhue =   1;                                     // Do we want random colours all the time? 1 = yes.
  50.  
  51.  
  52.  
  53. void setup() {
  54.  
  55.   Serial.begin(115200);                                        // Initialize serial port for debugging.
  56.   delay(1000);                                                // Soft startup to ease the flow of electrons.
  57.  
  58. //LEDS.addLeds<LED_TYPE,LED_DT,LED_CK,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);   // Use this for WS2801 or APA102
  59.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);                                       // Use this for WS2812
  60.  
  61.   FastLED.setBrightness(max_bright);
  62.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);             // FastLED Power management set at 5V, 500mA.
  63.  
  64. } // setup()
  65.  
  66.  
  67.  
  68. void loop () {
  69.  
  70.   showfps();
  71.  
  72.   EVERY_N_MILLISECONDS(100) {                                 // FastLED based non-blocking FIXED delay.
  73.     uint8_t maxChanges = 24;
  74.     nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);   // AWESOME palette blending capability.
  75.   }
  76.  
  77.  
  78.   EVERY_N_MILLIS_I(thistimer, thisdelay) {                     // FastLED based non-blocking VARIABLE delay to update/display the sequence.
  79.     testa();
  80.   }
  81.   thistimer.setPeriod(thisdelay);                             // Here is where you change the timing for the above.
  82.  
  83.  
  84.   EVERY_N_SECONDS(5) {                                        // Change the target palette to a random one every 5 seconds.
  85.     static uint8_t baseC = random8();                         // You can use this as a baseline colour if you want similar hues in the next line.
  86.     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)));
  87.   }
  88.  
  89.   twinkleup();
  90.  
  91.   FastLED.show();
  92.  
  93.   Serial.println(LEDS.getFPS());                              // Display frames per second on the serial monitor.
  94.  
  95. } // loop()
  96.  
  97.  
  98.  
  99. void testa() {
  100.  
  101. }
  102.  
  103. void twinkleup() {
  104.  
  105. // Local variables
  106.   uint8_t ranstart;                                                                 // Our first random number is the starting brightness in our sin() calculation.
  107.   uint8_t mylen;                                                                    // The second random number is used as our frequency.
  108.   uint8_t mysin;                                                                    // The result of sin(start+millis/frequency).
  109.  
  110.   random16_set_seed(535);                                                           // The randomizer needs to be re-set each time through the loop in order for the 'random' numbers to be the same each time through.
  111.  
  112.   for (int i = 0; i<NUM_LEDS; i++) {
  113.     ranstart = random8();                                                           // The starting value (aka brightness) for each pixel.
  114.     mylen = random8(10,20);                                                         // The frequency of our sine wave.
  115.     mysin = sin8(ranstart + millis()/mylen);                                        // Combined those with millis() which progresses it along.
  116.     leds[i] = ColorFromPalette(currentPalette, i*20, mysin, currentBlending);       // Now, let's run it through the palette lookup.
  117.   }
  118.  
  119. } // twinkleup()
  120.  
  121.  
  122.  
  123.  
  124. void showfps() {                                              // Show rames per seocond on the serial monitor.
  125.  
  126.   long currentMillis = 0;                                       // Variables used by our fps counter.
  127.  
  128.   static long lastMillis = 0;
  129.   static long loops = 0;
  130.  
  131.   currentMillis=millis();
  132.   loops++;
  133.   if(currentMillis - lastMillis >1000) {
  134.     Serial.println(loops);                                    // Print it once a second.
  135.     lastMillis = currentMillis;
  136.     loops = 0;
  137.   }
  138. } // showfps()
Add Comment
Please, Sign In to add comment