Advertisement
BurningWreck

Claude - FastLED random LEDs

Apr 23rd, 2025
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | Software | 0 0
  1. // Claude's code for a set of randomly blinking LEDs using CRGBArray / CRGBSet
  2. // 8 strip Neopixel, first two are excluded from the blinking effect
  3.  
  4. #include <FastLED.h>
  5.  
  6. // Define the number of LEDs in your strip
  7. #define NUM_LEDS 8
  8. #define DATA_PIN 6  // Connect your NeoPixel data line to this Arduino pin
  9.  
  10. // Create an array for the LEDs using CRGBArray
  11. CRGBArray<NUM_LEDS> leds;
  12.  
  13. // Define timing for random color blinking
  14. #define RANDOM_BLINK_MIN 300    // Minimum time a random LED stays in its state
  15. #define RANDOM_BLINK_MAX 2000   // Maximum time a random LED stays in its state
  16.  
  17. // Number of random blinking LEDs (LEDs 2-7)
  18. #define NUM_RANDOM_LEDS 6
  19.  
  20. // Arrays to track timing and state for each random blinking LED
  21. unsigned long previousMillis[NUM_RANDOM_LEDS];
  22. unsigned long intervals[NUM_RANDOM_LEDS];      
  23. bool blinkState[NUM_RANDOM_LEDS];              
  24. CRGB colors[NUM_RANDOM_LEDS];                  
  25.  
  26. void setup() {
  27.   // Initialize the FastLED library with your strip configuration
  28.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  29.   FastLED.setBrightness(64);  // Set to about 25% brightness
  30.  
  31.   // Clear all LEDs to start
  32.   FastLED.clear();
  33.   FastLED.show();
  34.  
  35.   // Initialize random seed
  36.   randomSeed(analogRead(0));
  37.  
  38.   // Initialize random blink timings, states, and colors
  39.   for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
  40.     // Stagger the starting times
  41.     previousMillis[i] = millis() - random(0, 1000);
  42.    
  43.     // Randomize initial states with variation to avoid clusters
  44.     blinkState[i] = (random(10) > 5 + i % 3) ? true : false;
  45.    
  46.     // Assign random intervals
  47.     intervals[i] = random(RANDOM_BLINK_MIN, RANDOM_BLINK_MAX);
  48.    
  49.     // Generate a random color for each LED
  50.     colors[i] = CHSV(random8(), random(180, 255), random(200, 255));
  51.   }
  52.  
  53.   // Turn first two LEDs off
  54.   leds(0, 1) = CRGB::Black;
  55. }
  56.  
  57. void loop() {
  58.   // Run the random blinking function
  59.   randomBlink();
  60.  
  61.   // Update the LED strip
  62.   FastLED.show();
  63.  
  64.   // Small delay to prevent flickering
  65.   delay(10);
  66. }
  67.  
  68. void randomBlink() {
  69.   // Using CRGBSet to address LEDs 2 through 7 (indexes 2-7)
  70.   CRGBSet randomLeds(leds(2, 7));
  71.  
  72.   // Get the current time
  73.   unsigned long currentMillis = millis();
  74.  
  75.   // Check each LED in the random set
  76.   for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
  77.     // Check if it's time to update this LED based on its custom interval
  78.     if (currentMillis - previousMillis[i] >= intervals[i]) {
  79.       previousMillis[i] = currentMillis;
  80.      
  81.       // Toggle state
  82.       blinkState[i] = !blinkState[i];
  83.      
  84.       if (blinkState[i]) {
  85.         // Generate a new random color when turning on
  86.         colors[i] = CHSV(random8(), random(180, 255), random(200, 255));
  87.         // Generate a new random interval for on state
  88.         intervals[i] = random(RANDOM_BLINK_MIN, RANDOM_BLINK_MAX);
  89.       } else {
  90.         // Generate a new random interval for off state
  91.         intervals[i] = random(RANDOM_BLINK_MIN + 100, RANDOM_BLINK_MAX - 200);
  92.       }
  93.     }
  94.    
  95.     // Set LED to either its color or black based on current state
  96.     if (blinkState[i]) {
  97.       randomLeds[i] = colors[i];
  98.     } else {
  99.       randomLeds[i] = CRGB::Black;
  100.     }
  101.   }
  102. }
Tags: FastLED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement