From 9dbd9af829042bed069b744554dba0c1195ceb6d Mon Sep 17 00:00:00 2001 From: Andrew Tuline Date: Fri, 3 Feb 2017 08:55:14 -0800 Subject: [PATCH] A few updates. --- aanimations/aanimations.ino | 84 +++++++++++++++++++++++++++++++++ aatemplate/aatemplate.ino | 94 +++++++++++++++++++++++-------------- 2 files changed, 143 insertions(+), 35 deletions(-) create mode 100644 aanimations/aanimations.ino diff --git a/aanimations/aanimations.ino b/aanimations/aanimations.ino new file mode 100644 index 0000000..f36eb4b --- /dev/null +++ b/aanimations/aanimations.ino @@ -0,0 +1,84 @@ +/* aanimations + * + * By: Can't recall where I found this. + * + * Modified by: Andrew Tuline + * + * Date: January, 2017 + * + * This sketch demonstrates how to blend between two animations running at the same time. + * + */ + +#include "FastLED.h" // FastLED library. + +#if FASTLED_VERSION < 3001000 +#error "Requires FastLED 3.1 or later; check github for latest code." +#endif + +#define LED_DT 12 // Data pin to connect to the strip. +#define LED_CK 11 // Clock pin for WS2801 or APA102. +#define COLOR_ORDER BGR // It's GRB for WS2812 and BGR for APA102. +#define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to change LEDS.addLeds. +#define NUM_LEDS 60 // Number of LED's. + +// Global variables can be changed on the fly. +uint8_t max_bright = 128; // Overall brightness definition. It can be changed on the fly. + +// have 3 independent CRGBs - 2 for the sources and one for the output +CRGB leds[NUM_LEDS]; +CRGB leds2[NUM_LEDS]; +CRGB leds3[NUM_LEDS]; + + + +void setup() { + + delay(1000); // Power-up safety delay. + Serial.begin(57600); // Initialize serial port for debugging. + + LEDS.addLeds(leds, NUM_LEDS); // Use this for WS2801 or APA102 +// LEDS.addLeds(leds, NUM_LEDS); // Use this for WS2812 + + FastLED.setBrightness(max_bright); + set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA. + +} // setup() + + + +void loop() { + + animationA(); // render the first animation into leds2 + animationB(); // render the second animation into leds3 + + uint8_t ratio = beatsin8(2); // Alternate between 0 and 255 every minute + + for (int i = 0; i < NUM_LEDS; i++) { // mix the 2 arrays together + leds[i] = blend( leds2[i], leds3[i], ratio ); + } + + FastLED.show(); + +} // loop() + + + +void animationA() { // running red stripe. + + for (int i = 0; i < NUM_LEDS; i++) { + uint8_t red = (millis() / 10) + (i * 12); // speed, length + if (red > 128) red = 0; + leds2[i] = CRGB(red, 0, 0); + } +} // animationA() + + + +void animationB() { // running green stripe in opposite direction. + for (int i = 0; i < NUM_LEDS; i++) { + uint8_t green = (millis() / 5) - (i * 12); // speed, length + if (green > 128) green = 0; + leds3[i] = CRGB(0, green, 0); + } +} // animationB() diff --git a/aatemplate/aatemplate.ino b/aatemplate/aatemplate.ino index c37b058..fc754f4 100644 --- a/aatemplate/aatemplate.ino +++ b/aatemplate/aatemplate.ino @@ -1,93 +1,117 @@ - /* Display Template for FastLED - -By: Andrew Tuline - -Date: July, 2015 - -This is a simple non-blocking FastLED display sequence template. - -FastLED is available at https://github.com/FastLED/FastLED - -*/ - - - -#include "FastLED.h" // FastLED library. Please use the latest development version. + * + * By: Andrew Tuline + * + * Modified by: Andrew Tuline + * + * Date: July, 2015 + * + * This is a simple non-blocking FastLED display sequence template. + * + * + */ + +#include "FastLED.h" // FastLED library. #if FASTLED_VERSION < 3001000 #error "Requires FastLED 3.1 or later; check github for latest code." #endif - +#define qsubd(x, b) ((x>b)?b:0) // Clip. . . . A digital unsigned subtraction macro. if result <0, then x=0. Otherwise, x=b. +#define qsuba(x, b) ((x>b)?x-b:0) // Level shift. . . Unsigned subtraction macro. if result <0, then x=0. Otherwise x=x-b. + + // Fixed definitions cannot change on the fly. #define LED_DT 12 // Data pin to connect to the strip. #define LED_CK 11 // Clock pin for WS2801 or APA102. #define COLOR_ORDER BGR // It's GRB for WS2812 and BGR for APA102. -#define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to change LEDS.addLeds. -#define NUM_LEDS 20 // Number of LED's. +#define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit. +#define NUM_LEDS 60 // Number of LED's. // Global variables can be changed on the fly. -uint8_t max_bright = 64; // Overall brightness definition. It can be changed on the fly. +uint8_t max_bright = 128; // Overall brightness. struct CRGB leds[NUM_LEDS]; // Initialize our LED array. +// Palette definitions +CRGBPalette16 currentPalette; +CRGBPalette16 targetPalette; +TBlendType currentBlending; // NOBLEND or LINEARBLEND // Define variables used by the sequences. int twinkrate = 100; // The higher the value, the lower the number of twinkles. -uint8_t thisdelay = 60; // A delay value for the sequence(s). +uint8_t thisdelay = 10; // A delay value for the sequence(s). uint8_t thisfade = 8; // How quickly does it fade? Lower = slower fade rate. uint8_t thishue = 50; // The hue. -uint8_t thissat = 100; // The saturation, where 255 = brilliant colours. +uint8_t thissat = 255; // The saturation, where 255 = brilliant colours. uint8_t thisbri = 255; // Brightness of a sequence. -bool randhue = 0; // Do we want random colours all the time? 1 = yes. +bool randhue = 1; // Do we want random colours all the time? 1 = yes. void setup() { - delay(3000); // Power-up safety delay. - Serial.begin(57600); // Initialize serial port for debugging. + Serial.begin(57600); // Initialize serial port for debugging. + delay(1000); // Soft startup to ease the flow of electrons. + LEDS.addLeds(leds, NUM_LEDS); // Use this for WS2801 or APA102 // LEDS.addLeds(leds, NUM_LEDS); // Use this for WS2812 + + currentBlending = LINEARBLEND; FastLED.setBrightness(max_bright); set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA. + } // setup() void loop () { + ChangeMe(); // Check the demo loop for changes to the variables. + EVERY_N_MILLISECONDS(100) { + uint8_t maxChanges = 24; + nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability. + } + EVERY_N_MILLISECONDS(thisdelay) { // FastLED based non-blocking delay to update/display the sequence. twinkle(); - show_at_max_brightness_for_power(); } + EVERY_N_SECONDS(5) { // Change the target palette to a random one every 5 seconds. + static uint8_t baseC = random8(); // You can use this as a baseline colour if you want similar hues in the next line. + 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))); + } + + FastLED.show(); + Serial.println(LEDS.getFPS()); // Display frames per second on the serial monitor. + } // loop() void twinkle() { - if (twinkrate < NUM_LEDS) twinkrate = NUM_LEDS; // Makes sure the twinkrate will cover ALL of the LED's as it's used as the maximum LED index value. - int i = random16(twinkrate); // A random number based on twinkrate. Higher number => fewer twinkles. - if (randhue) thishue = random16(0,255); // Randomize every LED if TRUE - if (i < NUM_LEDS) leds[i] = CHSV(thishue, thissat, thisbri); // Only the lowest probability twinkles will do. You could even randomize the hue/saturation. - for (int j = 0; j < NUM_LEDS; j++) leds[j].fadeToBlackBy(thisfade); // Use the FastLED fade method. + + if (random8() < twinkrate) leds[random16(NUM_LEDS)] += ColorFromPalette(currentPalette, (randhue ? random8() : thishue), 255, currentBlending); + fadeToBlackBy(leds, NUM_LEDS, thisfade); + } // twinkle() void ChangeMe() { // A time (rather than loop) based demo sequencer. This gives us full control over the length of each sequence. - uint8_t secondHand = (millis() / 1000) % 15; // IMPORTANT!!! Change '15' to a different value to change duration of the loop. + + uint8_t secondHand = (millis() / 1000) % 10; // IMPORTANT!!! Change '15' to a different value to change duration of the loop. static uint8_t lastSecond = 99; // Static variable, means it's only defined once. This is our 'debounce' variable. if (lastSecond != secondHand) { // Debounce to make sure we're not repeating an assignment. lastSecond = secondHand; - if (secondHand == 0) {thisdelay = 60; randhue = 1; thissat=255; thisfade=16; twinkrate=20;} // You can change values here, one at a time , or altogether. - if (secondHand == 5) {thisdelay = 60; randhue = 0; thishue=128; thisfade=64; twinkrate=100;} - if (secondHand == 10) {thisdelay = 60; thishue=random16(0,64); twinkrate=40;} // Only gets called once, and not continuously for the next several seconds. Therefore, no rainbows. + switch(secondHand) { + case 0: thisdelay = 10; randhue = 1; thissat=255; thisfade=8; twinkrate=150; break; // You can change values here, one at a time , or altogether. + case 5: thisdelay = 100; randhue = 0; thishue=random8(); thisfade=2; twinkrate=20; break; + case 10: break; + } } -} // ChangeMe() +} // ChangeMe()