Advertisement
atuline

FastLED TwinkLEDS with various delays

Sep 1st, 2014
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. // TwinkLEDS with various delays
  2. //
  3. // By: Andrew Tuline
  4. //
  5. // Date: Aug, 2014
  6. //
  7. // This is a simple FastLED (2.1 and greater) display sequence template with a non-blocking delay.
  8. //
  9. // I don't know why twinkle2() doesn't work.
  10. //
  11. //
  12. // FastLED 2.1 is available at https://github.com/FastLED/FastLED/tree/FastLED2.1
  13. //
  14.  
  15.  
  16. #include <FastLED.h>                                           // FastLED library
  17.  
  18. #define LED_DT 13                                              // Data pin
  19. #define NUM_LEDS 24                                            // Number of LED's
  20. #define COLOR_ORDER GRB                                        // Change the order as necessary
  21. #define LED_TYPE WS2811                                        // What kind of strip are you using?
  22. #define BRIGHTNESS  196                                        // How bright do we want to go
  23.  
  24. struct CRGB leds[NUM_LEDS];                                    // Initializxe our array
  25.  
  26.  
  27. // Initialize global variables for sequences
  28. int thisdelay = 8;                                             // A delay value for the sequence(s)
  29.  
  30. // Non-blocking delay timers
  31. long currentMillis = 0;
  32. long previousMillis = 0;
  33.  
  34.  
  35. void setup() {
  36.   Serial.begin(9600);
  37.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  38.   FastLED.setBrightness(BRIGHTNESS);
  39. } // setup()
  40.  
  41.  
  42. void loop () {
  43. //  twinkle();                                                   // Use blocking delay - THIS WORKS!!
  44.  twinkle2();                                                   // Use non-blocking delay - THIS DOES NOT WORK!!
  45. //  twinkle3();                                                  // Uses non-blocking delay with print statements -- THIS WORKS!!
  46. } // loop()
  47.  
  48.  
  49. void twinkle() {
  50.   int i = random8();                                                                     // A random number. Higher number => fewer twinkles. Use random16() for values >255.
  51.   if (i < NUM_LEDS) leds[i] = CHSV(50, 100, 255);              // Only the lowest probability twinkles will do. You could even randomize the hue/saturation. .
  52.   for (int j = 0; j < NUM_LEDS; j++) leds[j].fadeToBlackBy(8);
  53.   LEDS.show();                                                 // Standard FastLED display
  54.   delay(thisdelay);                                            // Standard delay
  55. } // twinkle()
  56.  
  57.  
  58. void twinkle2() {                                              // This does not work correctly for some reason.
  59. currentMillis = millis();
  60.   if(currentMillis - previousMillis > thisdelay) {
  61.     int i = random8();                                         // A random number. Higher number => fewer twinkles. Use random16() for values >255.
  62.     if (i < NUM_LEDS) leds[i] = CHSV(50, 100, 255);            // Only the lowest probability twinkles will do. You could even randomize the hue/saturation. .
  63.     for (int j = 0; j < NUM_LEDS; j++) leds[j].fadeToBlackBy(8);
  64.     LEDS.show();                                               // Standard FastLED display
  65.   }
  66.   previousMillis = currentMillis;
  67. } // twinkle2()
  68.  
  69.  
  70. void twinkle3() {
  71. currentMillis = millis();
  72.  
  73.   Serial.print(currentMillis);                                 // For some reason, the non-blocking delay works when I add a few print statements.
  74.   Serial.print(" ");
  75.   Serial.println(previousMillis);
  76.  
  77.   if(currentMillis - previousMillis > thisdelay) {
  78.     int i = random8();                                         // A random number. Higher number => fewer twinkles. Use random16() for values >255.
  79.     if (i < NUM_LEDS) leds[i] = CHSV(50, 100, 255);            // Only the lowest probability twinkles will do. You could even randomize the hue/saturation. .
  80.     for (int j = 0; j < NUM_LEDS; j++) leds[j].fadeToBlackBy(8);
  81.     LEDS.show();                                               // Standard FastLED display
  82.   }
  83.   previousMillis = currentMillis;
  84. } // twinkle3()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement