Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TwinkLEDS with various delays
- //
- // By: Andrew Tuline
- //
- // Date: Aug, 2014
- //
- // This is a simple FastLED (2.1 and greater) display sequence template with a non-blocking delay.
- //
- // I don't know why twinkle2() doesn't work.
- //
- //
- // FastLED 2.1 is available at https://github.com/FastLED/FastLED/tree/FastLED2.1
- //
- #include <FastLED.h> // FastLED library
- #define LED_DT 13 // Data pin
- #define NUM_LEDS 24 // Number of LED's
- #define COLOR_ORDER GRB // Change the order as necessary
- #define LED_TYPE WS2811 // What kind of strip are you using?
- #define BRIGHTNESS 196 // How bright do we want to go
- struct CRGB leds[NUM_LEDS]; // Initializxe our array
- // Initialize global variables for sequences
- int thisdelay = 8; // A delay value for the sequence(s)
- // Non-blocking delay timers
- long currentMillis = 0;
- long previousMillis = 0;
- void setup() {
- Serial.begin(9600);
- LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- } // setup()
- void loop () {
- // twinkle(); // Use blocking delay - THIS WORKS!!
- twinkle2(); // Use non-blocking delay - THIS DOES NOT WORK!!
- // twinkle3(); // Uses non-blocking delay with print statements -- THIS WORKS!!
- } // loop()
- void twinkle() {
- int i = random8(); // A random number. Higher number => fewer twinkles. Use random16() for values >255.
- if (i < NUM_LEDS) leds[i] = CHSV(50, 100, 255); // 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(8);
- LEDS.show(); // Standard FastLED display
- delay(thisdelay); // Standard delay
- } // twinkle()
- void twinkle2() { // This does not work correctly for some reason.
- currentMillis = millis();
- if(currentMillis - previousMillis > thisdelay) {
- int i = random8(); // A random number. Higher number => fewer twinkles. Use random16() for values >255.
- if (i < NUM_LEDS) leds[i] = CHSV(50, 100, 255); // 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(8);
- LEDS.show(); // Standard FastLED display
- }
- previousMillis = currentMillis;
- } // twinkle2()
- void twinkle3() {
- currentMillis = millis();
- Serial.print(currentMillis); // For some reason, the non-blocking delay works when I add a few print statements.
- Serial.print(" ");
- Serial.println(previousMillis);
- if(currentMillis - previousMillis > thisdelay) {
- int i = random8(); // A random number. Higher number => fewer twinkles. Use random16() for values >255.
- if (i < NUM_LEDS) leds[i] = CHSV(50, 100, 255); // 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(8);
- LEDS.show(); // Standard FastLED display
- }
- previousMillis = currentMillis;
- } // twinkle3()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement