atuline

TwinkleUp for Arduino and FastLED

Apr 30th, 2020 (edited)
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. /*
  2.  * File: Twinlekup
  3.  *
  4.  * By: Andrew Tuline
  5.  *
  6.  * Date: July, 2019
  7.  *
  8.  * This program fades an array of LED's in and out individually and shows how you can save state information for multiple
  9.  * properties and with no bytes used for each LED! Yes, that's ZERO bytes used. If we used array, we would possibly need to
  10.  * store the following information for each LED:
  11.  *
  12.  * leds[x] has current colour.
  13.  * Max colour value (3 bytes).
  14.  * Fade down/up direction (1 bit).
  15.  * Current fade scale value (2 bytes).
  16.  * Varying twinkle rates (1 byte).
  17.  * Colour rotation (global setting).
  18.  * A flag for each LED as to whether or not it twinkles.
  19.  *
  20.  *
  21.  *
  22.  * In addition, this program:
  23.  *
  24.  * Uses palettes.
  25.  * Doesn't use delays or EVERY_N_MILLIS.
  26.  * Uses 8 bit trig math.
  27.  *
  28.  *
  29.  * What is this magic you ask? It's based on random numbers, or rather, pseudo random numbers. If you generate a
  30.  * list of random numbers starting with the same seed every time, then those 'random' numbers will always be the
  31.  * same. Rather than using structs and arrays to store property information for each LED, we generate those random
  32.  * numbers on the fly and use that for these proerties. Combined with a millis() counter along with sine waves, we
  33.  * can use those random numbers for multiple properties for our entire array of LED's. No memory used.
  34.  *
  35.  * Now, to find out what other animations can make use of these pseudo random numbers and save on precious memory?
  36.  *
  37.  */
  38.  
  39. #include <FastLED.h>
  40.  
  41. #define LED_DT 12
  42. #define NUM_LEDS 30
  43.  
  44. struct CRGB leds[NUM_LEDS];
  45. CRGBPalette16 currentPalette = PartyColors_p;
  46.  
  47.  
  48. void setup() {
  49.   Serial.begin(115200);
  50.   LEDS.addLeds<WS2812, LED_DT, GRB>(leds, NUM_LEDS);  // Configure the hardware, and define the array.
  51. } // setup()
  52.  
  53.  
  54. void loop() {
  55.   twinkleup();
  56.   FastLED.show();
  57. } // loop()
  58.  
  59.  
  60.  
  61. void twinkleup() {
  62.  
  63.   uint8_t intensity = 128;                                                  // 8-bit percentage of LED's lit.
  64.   random16_set_seed(535);                                                   // The randomizer needs to be re-set each time through the loop.
  65.  
  66.   for (int i=0; i<NUM_LEDS; i++) {
  67.     uint8_t startVal = random8();                                           // The starting value (aka brightness) for each pixel.
  68.     uint8_t freq = random8(64, 192);                                        // The frequency of our sine wave for each LED. By Andrew Tuline.
  69.     uint8_t pixBri = sin8(startVal + 16 * millis()/(255-freq));             // Combined starting value with speed to come up with unique sine wave for each LED.
  70.     if (random8() > intensity) pixBri = 0;                                  // Reduce number of LED's based on intensity.
  71.     leds[i] = ColorFromPalette(currentPalette, random8()+millis()/100, pixBri, LINEARBLEND);   // Use a random colour that slowly changes over time.
  72.   }
  73. } // twinkleup()
Add Comment
Please, Sign In to add comment