Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Twinlekup
- *
- * By: Andrew Tuline
- *
- * Date: July, 2019
- *
- * This program fades an array of LED's in and out individually and shows how you can save state information for multiple
- * properties and with no bytes used for each LED! Yes, that's ZERO bytes used. If we used array, we would possibly need to
- * store the following information for each LED:
- *
- * leds[x] has current colour.
- * Max colour value (3 bytes).
- * Fade down/up direction (1 bit).
- * Current fade scale value (2 bytes).
- * Varying twinkle rates (1 byte).
- * Colour rotation (global setting).
- * A flag for each LED as to whether or not it twinkles.
- *
- *
- *
- * In addition, this program:
- *
- * Uses palettes.
- * Doesn't use delays or EVERY_N_MILLIS.
- * Uses 8 bit trig math.
- *
- *
- * What is this magic you ask? It's based on random numbers, or rather, pseudo random numbers. If you generate a
- * list of random numbers starting with the same seed every time, then those 'random' numbers will always be the
- * same. Rather than using structs and arrays to store property information for each LED, we generate those random
- * numbers on the fly and use that for these proerties. Combined with a millis() counter along with sine waves, we
- * can use those random numbers for multiple properties for our entire array of LED's. No memory used.
- *
- * Now, to find out what other animations can make use of these pseudo random numbers and save on precious memory?
- *
- */
- #include <FastLED.h>
- #define LED_DT 12
- #define NUM_LEDS 30
- struct CRGB leds[NUM_LEDS];
- CRGBPalette16 currentPalette = PartyColors_p;
- void setup() {
- Serial.begin(115200);
- LEDS.addLeds<WS2812, LED_DT, GRB>(leds, NUM_LEDS); // Configure the hardware, and define the array.
- } // setup()
- void loop() {
- twinkleup();
- FastLED.show();
- } // loop()
- void twinkleup() {
- uint8_t intensity = 128; // 8-bit percentage of LED's lit.
- random16_set_seed(535); // The randomizer needs to be re-set each time through the loop.
- for (int i=0; i<NUM_LEDS; i++) {
- uint8_t startVal = random8(); // The starting value (aka brightness) for each pixel.
- uint8_t freq = random8(64, 192); // The frequency of our sine wave for each LED. By Andrew Tuline.
- uint8_t pixBri = sin8(startVal + 16 * millis()/(255-freq)); // Combined starting value with speed to come up with unique sine wave for each LED.
- if (random8() > intensity) pixBri = 0; // Reduce number of LED's based on intensity.
- leds[i] = ColorFromPalette(currentPalette, random8()+millis()/100, pixBri, LINEARBLEND); // Use a random colour that slowly changes over time.
- }
- } // twinkleup()
Add Comment
Please, Sign In to add comment