Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Display Template for FastLED
- *
- * By: Andrew Tuline
- *
- * Modified by: Andrew Tuline
- *
- * Date: July, 2015
- *
- * This is a simple non-blocking FastLED display sequence template.
- *
- *
- */
- #ifdef ESP8266
- #define FASTLED_ALLOW_INTERRUPTS 0 // Used for ESP8266 with WS2812 LED's. Ugh!!!
- #endif
- #include <FastLED.h> // FastLED library.
- #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 2 // Data pin to connect to the strip.
- #define LED_CK 11 // Clock pin for WS2801 or APA102.
- #define COLOR_ORDER GRB // It's GRB for WS2812 and BGR for APA102.
- #define LED_TYPE WS2812 // Using APA102, WS2812, WS2801. Don't forget to modify LEDS.addLeds to suit.
- #define NUM_LEDS 40 // Number of LED's.
- // Global variables 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 = LINEARBLEND; // 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 = 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 = 255; // The saturation, where 255 = brilliant colours.
- uint8_t thisbri = 255; // Brightness of a sequence.
- bool randhue = 1; // Do we want random colours all the time? 1 = yes.
- void setup() {
- Serial.begin(115200); // Initialize serial port for debugging.
- delay(1000); // Soft startup to ease the flow of electrons.
- //LEDS.addLeds<LED_TYPE,LED_DT,LED_CK,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // Use this for WS2801 or APA102
- LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812
- FastLED.setBrightness(max_bright);
- FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); // FastLED Power management set at 5V, 500mA.
- } // setup()
- void loop () {
- showfps();
- EVERY_N_MILLISECONDS(100) { // FastLED based non-blocking FIXED delay.
- uint8_t maxChanges = 24;
- nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
- }
- EVERY_N_MILLIS_I(thistimer, thisdelay) { // FastLED based non-blocking VARIABLE delay to update/display the sequence.
- testa();
- }
- thistimer.setPeriod(thisdelay); // Here is where you change the timing for the above.
- 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)));
- }
- twinkleup();
- FastLED.show();
- Serial.println(LEDS.getFPS()); // Display frames per second on the serial monitor.
- } // loop()
- void testa() {
- }
- void twinkleup() {
- // Local variables
- uint8_t ranstart; // Our first random number is the starting brightness in our sin() calculation.
- uint8_t mylen; // The second random number is used as our frequency.
- uint8_t mysin; // The result of sin(start+millis/frequency).
- 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.
- for (int i = 0; i<NUM_LEDS; i++) {
- ranstart = random8(); // The starting value (aka brightness) for each pixel.
- mylen = random8(10,20); // The frequency of our sine wave.
- mysin = sin8(ranstart + millis()/mylen); // Combined those with millis() which progresses it along.
- leds[i] = ColorFromPalette(currentPalette, i*20, mysin, currentBlending); // Now, let's run it through the palette lookup.
- }
- } // twinkleup()
- void showfps() { // Show rames per seocond on the serial monitor.
- long currentMillis = 0; // Variables used by our fps counter.
- static long lastMillis = 0;
- static long loops = 0;
- currentMillis=millis();
- loops++;
- if(currentMillis - lastMillis >1000) {
- Serial.println(loops); // Print it once a second.
- lastMillis = currentMillis;
- loops = 0;
- }
- } // showfps()
Add Comment
Please, Sign In to add comment