BurningWreck

FastLED - Random blink test

Sep 12th, 2025
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | Software | 0 0
  1. /*
  2. Eyes and Random Blinking LEDs test
  3. 9/12/2025
  4. */
  5.  
  6.  
  7. #include "FastLED.h"
  8.  
  9. // How many leds in your strip?
  10. #define NUM_LEDS 36
  11. #define DATA_PIN 14
  12. #define LED_TYPE NEOPIXEL
  13. #define BRIGHTNESS 50  // 0 - 255, 8-bit
  14.  
  15. CRGBArray<NUM_LEDS> leds;
  16.  
  17. CRGBSet eyeLEDS(leds(6, 7));  // Eyes are last two in strand
  18. CRGBSet randomLeds(leds(0, 5));
  19. //CRGBSet allLEDs(leds(0, NUM_LEDS - 1));
  20.  
  21. // For random blinking effect
  22. #define NUM_RANDOM_LEDS 6
  23.  
  24. // Define base timing values for random blinking
  25. #define CHECK_INTERVAL 50    // How often we check for updates
  26. #define MIN_BLINK_TIME 300   // Minimum time in milliseconds
  27. #define MAX_BLINK_TIME 2000  // Maximum time in milliseconds
  28.  
  29. // Arrays to track state for each random blinking LED
  30. bool blinkState[NUM_RANDOM_LEDS];
  31. CRGB colors[NUM_RANDOM_LEDS];
  32. uint16_t nextChangeTime[NUM_RANDOM_LEDS];  // How many milliseconds until this LED changes state
  33.  
  34. // FadeUpDown stuff
  35. uint8_t hue = 0;
  36. uint8_t sat = 255;    // red
  37. uint8_t val = 0;
  38. boolean fadeDirection = 1;  // [1=fade up, 0=fade down]
  39.  
  40. void setup() {
  41.   Serial.begin(115200);
  42.   FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
  43.   FastLED.setBrightness(BRIGHTNESS);  // 0 - 255, 8-bit
  44.  
  45.   // Initialize random seed
  46.   random16_add_entropy(analogRead(35));
  47.  
  48.   // Initialize random blink timings, states, and colors
  49.   for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
  50.     // Randomize initial states with variation to avoid clusters
  51.     blinkState[i] = (random8(10) > 5 + i % 3) ? true : false;
  52.  
  53.     // Assign an initial delay before the first change
  54.     nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
  55.  
  56.     // Generate a random color for each LED
  57.     colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
  58.   }
  59. }
  60.  
  61. void loop() {
  62.  
  63.   // Fade eyes up/down
  64.   fadeUpDown();
  65.  
  66.   // Randomly blink the first set of LEDs
  67.   EVERY_N_MILLISECONDS(CHECK_INTERVAL) {
  68.  
  69.     for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
  70.       // Decrease the time until next change
  71.       if (nextChangeTime[i] > CHECK_INTERVAL) {
  72.         nextChangeTime[i] -= CHECK_INTERVAL;
  73.       } else {
  74.         // Time to change this LED's state
  75.         blinkState[i] = !blinkState[i];
  76.  
  77.         if (blinkState[i]) {
  78.           // Generate a new random color when turning on
  79.           colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
  80.           // Assign new time for how long to stay ON
  81.           nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
  82.         } else {
  83.           // Assign new time for how long to stay OFF (slightly different range)
  84.           nextChangeTime[i] = random16(MIN_BLINK_TIME + 100, MAX_BLINK_TIME - 200);
  85.         }
  86.       }
  87.  
  88.       // Set LED to either its color or black based on current state
  89.       randomLeds[i] = blinkState[i] ? colors[i] : CRGB::Black;
  90.     }
  91.  
  92.     // Update the LED strip
  93.     EVERY_N_MILLISECONDS(20) {
  94.       FastLED.show();
  95.     }
  96.   }
  97. }
  98.  
  99. void fadeUpDown() {
  100.   if (fadeDirection == 1) {  //fade up
  101.     EVERY_N_MILLISECONDS(3) {
  102.       fill_solid(eyeLEDS, 2, CHSV(hue, sat, val));
  103.       val = val + 1;
  104.       if (val == 255) {
  105.         fadeDirection = !fadeDirection;  //reverse direction
  106.         Serial.print("---fade up done---");
  107.         Serial.print("  fadeDirection: ");
  108.         Serial.println(fadeDirection);
  109.       }
  110.     }
  111.   }
  112.  
  113.   if (fadeDirection == 0) {  //fade down
  114.     EVERY_N_MILLISECONDS(9) {
  115.       fill_solid(eyeLEDS, 2, CHSV(hue, sat, val));
  116.       val = val - 1;
  117.       if (val == 0) {
  118.         fadeDirection = !fadeDirection;  //reverse direction
  119.         Serial.print("--fade down done--");
  120.         Serial.print("  fadeDirection: ");
  121.         Serial.println(fadeDirection);
  122.       }
  123.     }
  124.   }
  125.  
  126.   FastLED.show();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment