Advertisement
BurningWreck

Claude - FastLED random LEDs - EVERY_N_MILLISECONDS

Apr 24th, 2025
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | Software | 0 0
  1. // A set of random blinking Neopixels
  2. // Uses CRGBSet and FastLED internal timing with EVERY_N_MILLISECONDS_RANDOM
  3. // Updated version, Claude, 4/23/2025
  4.  
  5. // Note that this took Claude seven tries to use EVERY_N_MILLISECONDS_RANDOM and match the timing of the previous code.
  6. // Most of the time was spent getting it to understand how to use EVERY_N_MILLISECONDS correctly.
  7.  
  8. #include <FastLED.h>
  9.  
  10. // Define the number of LEDs in your strip
  11. #define NUM_LEDS 8
  12. #define DATA_PIN 6  // Connect your NeoPixel data line to this Arduino pin
  13.  
  14. // Create an array for the LEDs using CRGBArray
  15. CRGBArray<NUM_LEDS> leds;
  16.  
  17. // Number of random blinking LEDs (LEDs 2-7)
  18. #define NUM_RANDOM_LEDS 6
  19.  
  20. // Define base timing values for random blinking (in milliseconds)
  21. #define MIN_BLINK_TIME 300
  22. #define MAX_BLINK_TIME 2000
  23.  
  24. // Adjusted timing values for EVERY_N_MILLISECONDS_RANDOM
  25. // Using a longer base time to better match original behavior
  26. #define ADJUSTED_BLINK_TIME (MAX_BLINK_TIME * 1.25)
  27.  
  28. // Arrays to track state for each random blinking LED
  29. bool blinkState[NUM_RANDOM_LEDS];      
  30. CRGB colors[NUM_RANDOM_LEDS];          
  31.  
  32. void setup() {
  33.   // Initialize the FastLED library with your strip configuration
  34.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  35.   FastLED.setBrightness(64);  // Set to about 25% brightness
  36.  
  37.   // Clear all LEDs to start
  38.   FastLED.clear();
  39.   FastLED.show();
  40.  
  41.   // Initialize random seed
  42.   random16_add_entropy(analogRead(0));
  43.  
  44.   // Initialize random blink timings, states, and colors
  45.   for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
  46.     // Randomize initial states with variation to avoid clusters
  47.     blinkState[i] = (random8(10) > 5 + i % 3) ? true : false;
  48.    
  49.     // Generate a random color for each LED
  50.     colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
  51.   }
  52.  
  53.   // Turn first two LEDs off
  54.   leds(0, 1) = CRGB::Black;
  55. }
  56.  
  57. void loop() {
  58.   // Using CRGBSet to address LEDs 2 through 7 (indexes 2-7)
  59.   CRGBSet randomLeds(leds(2, 7));
  60.  
  61.   // Update each LED with randomized timing
  62.   // Using different timer IDs (100-105) for each LED
  63.   // Using longer base time to slow down the blinking
  64.   EVERY_N_MILLISECONDS_RANDOM(100, ADJUSTED_BLINK_TIME) {
  65.     toggleLED(0, randomLeds);
  66.   }
  67.  
  68.   EVERY_N_MILLISECONDS_RANDOM(101, ADJUSTED_BLINK_TIME) {
  69.     toggleLED(1, randomLeds);
  70.   }
  71.  
  72.   EVERY_N_MILLISECONDS_RANDOM(102, ADJUSTED_BLINK_TIME) {
  73.     toggleLED(2, randomLeds);
  74.   }
  75.  
  76.   EVERY_N_MILLISECONDS_RANDOM(103, ADJUSTED_BLINK_TIME) {
  77.     toggleLED(3, randomLeds);
  78.   }
  79.  
  80.   EVERY_N_MILLISECONDS_RANDOM(104, ADJUSTED_BLINK_TIME) {
  81.     toggleLED(4, randomLeds);
  82.   }
  83.  
  84.   EVERY_N_MILLISECONDS_RANDOM(105, ADJUSTED_BLINK_TIME) {
  85.     toggleLED(5, randomLeds);
  86.   }
  87.  
  88.   // Update the LED strip
  89.   EVERY_N_MILLISECONDS(20) {
  90.     FastLED.show();
  91.   }
  92. }
  93.  
  94. // Helper function to toggle an LED's state
  95. void toggleLED(uint8_t index, CRGBSet &randomLeds) {
  96.   // Toggle the blink state
  97.   blinkState[index] = !blinkState[index];
  98.  
  99.   if (blinkState[index]) {
  100.     // Generate a new random color when turning on
  101.     colors[index] = CHSV(random8(), random8(180, 255), random8(200, 255));
  102.   }
  103.  
  104.   // Set LED to either its color or black based on current state
  105.   randomLeds[index] = blinkState[index] ? colors[index] : CRGB::Black;
  106. }
Tags: FastLED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement