Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Claude's code for a set of randomly blinking LEDs using CRGBArray / CRGBSet
- // 8 strip Neopixel, first two are excluded from the blinking effect
- #include <FastLED.h>
- // Define the number of LEDs in your strip
- #define NUM_LEDS 8
- #define DATA_PIN 6 // Connect your NeoPixel data line to this Arduino pin
- // Create an array for the LEDs using CRGBArray
- CRGBArray<NUM_LEDS> leds;
- // Define timing for random color blinking
- #define RANDOM_BLINK_MIN 300 // Minimum time a random LED stays in its state
- #define RANDOM_BLINK_MAX 2000 // Maximum time a random LED stays in its state
- // Number of random blinking LEDs (LEDs 2-7)
- #define NUM_RANDOM_LEDS 6
- // Arrays to track timing and state for each random blinking LED
- unsigned long previousMillis[NUM_RANDOM_LEDS];
- unsigned long intervals[NUM_RANDOM_LEDS];
- bool blinkState[NUM_RANDOM_LEDS];
- CRGB colors[NUM_RANDOM_LEDS];
- void setup() {
- // Initialize the FastLED library with your strip configuration
- FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
- FastLED.setBrightness(64); // Set to about 25% brightness
- // Clear all LEDs to start
- FastLED.clear();
- FastLED.show();
- // Initialize random seed
- randomSeed(analogRead(0));
- // Initialize random blink timings, states, and colors
- for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
- // Stagger the starting times
- previousMillis[i] = millis() - random(0, 1000);
- // Randomize initial states with variation to avoid clusters
- blinkState[i] = (random(10) > 5 + i % 3) ? true : false;
- // Assign random intervals
- intervals[i] = random(RANDOM_BLINK_MIN, RANDOM_BLINK_MAX);
- // Generate a random color for each LED
- colors[i] = CHSV(random8(), random(180, 255), random(200, 255));
- }
- // Turn first two LEDs off
- leds(0, 1) = CRGB::Black;
- }
- void loop() {
- // Run the random blinking function
- randomBlink();
- // Update the LED strip
- FastLED.show();
- // Small delay to prevent flickering
- delay(10);
- }
- void randomBlink() {
- // Using CRGBSet to address LEDs 2 through 7 (indexes 2-7)
- CRGBSet randomLeds(leds(2, 7));
- // Get the current time
- unsigned long currentMillis = millis();
- // Check each LED in the random set
- for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
- // Check if it's time to update this LED based on its custom interval
- if (currentMillis - previousMillis[i] >= intervals[i]) {
- previousMillis[i] = currentMillis;
- // Toggle state
- blinkState[i] = !blinkState[i];
- if (blinkState[i]) {
- // Generate a new random color when turning on
- colors[i] = CHSV(random8(), random(180, 255), random(200, 255));
- // Generate a new random interval for on state
- intervals[i] = random(RANDOM_BLINK_MIN, RANDOM_BLINK_MAX);
- } else {
- // Generate a new random interval for off state
- intervals[i] = random(RANDOM_BLINK_MIN + 100, RANDOM_BLINK_MAX - 200);
- }
- }
- // Set LED to either its color or black based on current state
- if (blinkState[i]) {
- randomLeds[i] = colors[i];
- } else {
- randomLeds[i] = CRGB::Black;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement