Advertisement
BurningWreck

Claude - FastLED random LEDs updated

Apr 23rd, 2025
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | Software | 0 0
  1. // This time Claude was told to use EVERY_N_MILLISECONDS
  2.  
  3. #include <FastLED.h>
  4.  
  5. // Define the number of LEDs in your strip
  6. #define NUM_LEDS 8
  7. #define DATA_PIN 6  // Connect your NeoPixel data line to this Arduino pin
  8.  
  9. // Create an array for the LEDs using CRGBArray
  10. CRGBArray<NUM_LEDS> leds;
  11.  
  12. // Number of random blinking LEDs (LEDs 2-7)
  13. #define NUM_RANDOM_LEDS 6
  14.  
  15. // Define base timing values for random blinking
  16. #define CHECK_INTERVAL 50      // How often we check for updates
  17. #define MIN_BLINK_TIME 300     // Minimum time in milliseconds
  18. #define MAX_BLINK_TIME 2000    // Maximum time in milliseconds
  19.  
  20. // Arrays to track state for each random blinking LED
  21. bool blinkState[NUM_RANDOM_LEDS];      
  22. CRGB colors[NUM_RANDOM_LEDS];          
  23. uint16_t nextChangeTime[NUM_RANDOM_LEDS];  // How many milliseconds until this LED changes state
  24.  
  25. void setup() {
  26.   // Initialize the FastLED library with your strip configuration
  27.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  28.   FastLED.setBrightness(64);  // Set to about 25% brightness
  29.  
  30.   // Clear all LEDs to start
  31.   FastLED.clear();
  32.   FastLED.show();
  33.  
  34.   // Initialize random seed
  35.   random16_seed(analogRead(0));
  36.  
  37.   // Initialize random blink timings, states, and colors
  38.   for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
  39.     // Randomize initial states with variation to avoid clusters
  40.     blinkState[i] = (random8(10) > 5 + i % 3) ? true : false;
  41.    
  42.     // Assign an initial delay before the first change
  43.     nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
  44.    
  45.     // Generate a random color for each LED
  46.     colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
  47.   }
  48.  
  49.   // Turn first two LEDs off
  50.   leds(0, 1) = CRGB::Black;
  51. }
  52.  
  53. void loop() {
  54.   // Check for LED updates on a regular interval
  55.   EVERY_N_MILLISECONDS(CHECK_INTERVAL) {
  56.     updateRandomLEDs();
  57.   }
  58.  
  59.   // Update the LED strip
  60.   EVERY_N_MILLISECONDS(20) {
  61.     FastLED.show();
  62.   }
  63. }
  64.  
  65. void updateRandomLEDs() {
  66.   // Using CRGBSet to address LEDs 2 through 7 (indexes 2-7)
  67.   CRGBSet randomLeds(leds(2, 7));
  68.  
  69.   // Check each LED to see if it's time to change state
  70.   for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
  71.     // Decrease the time until next change
  72.     if (nextChangeTime[i] > CHECK_INTERVAL) {
  73.       nextChangeTime[i] -= CHECK_INTERVAL;
  74.     } else {
  75.       // Time to change this LED's state
  76.       blinkState[i] = !blinkState[i];
  77.      
  78.       if (blinkState[i]) {
  79.         // Generate a new random color when turning on
  80.         colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
  81.         // Assign new time for how long to stay ON
  82.         nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
  83.       } else {
  84.         // Assign new time for how long to stay OFF (slightly different range)
  85.         nextChangeTime[i] = random16(MIN_BLINK_TIME + 100, MAX_BLINK_TIME - 200);
  86.       }
  87.     }
  88.    
  89.     // Set LED to either its color or black based on current state
  90.     randomLeds[i] = blinkState[i] ? colors[i] : CRGB::Black;
  91.   }
  92. }
Tags: FastLED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement