Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A set of random blinking Neopixels
- // Uses CRGBSet and FastLED internal timing with EVERY_N_MILLISECONDS_RANDOM
- // Updated version, Claude, 4/23/2025
- // Note that this took Claude seven tries to use EVERY_N_MILLISECONDS_RANDOM and match the timing of the previous code.
- // Most of the time was spent getting it to understand how to use EVERY_N_MILLISECONDS correctly.
- #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;
- // Number of random blinking LEDs (LEDs 2-7)
- #define NUM_RANDOM_LEDS 6
- // Define base timing values for random blinking (in milliseconds)
- #define MIN_BLINK_TIME 300
- #define MAX_BLINK_TIME 2000
- // Adjusted timing values for EVERY_N_MILLISECONDS_RANDOM
- // Using a longer base time to better match original behavior
- #define ADJUSTED_BLINK_TIME (MAX_BLINK_TIME * 1.25)
- // Arrays to track state for each random blinking LED
- 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
- random16_add_entropy(analogRead(0));
- // Initialize random blink timings, states, and colors
- for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
- // Randomize initial states with variation to avoid clusters
- blinkState[i] = (random8(10) > 5 + i % 3) ? true : false;
- // Generate a random color for each LED
- colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
- }
- // Turn first two LEDs off
- leds(0, 1) = CRGB::Black;
- }
- void loop() {
- // Using CRGBSet to address LEDs 2 through 7 (indexes 2-7)
- CRGBSet randomLeds(leds(2, 7));
- // Update each LED with randomized timing
- // Using different timer IDs (100-105) for each LED
- // Using longer base time to slow down the blinking
- EVERY_N_MILLISECONDS_RANDOM(100, ADJUSTED_BLINK_TIME) {
- toggleLED(0, randomLeds);
- }
- EVERY_N_MILLISECONDS_RANDOM(101, ADJUSTED_BLINK_TIME) {
- toggleLED(1, randomLeds);
- }
- EVERY_N_MILLISECONDS_RANDOM(102, ADJUSTED_BLINK_TIME) {
- toggleLED(2, randomLeds);
- }
- EVERY_N_MILLISECONDS_RANDOM(103, ADJUSTED_BLINK_TIME) {
- toggleLED(3, randomLeds);
- }
- EVERY_N_MILLISECONDS_RANDOM(104, ADJUSTED_BLINK_TIME) {
- toggleLED(4, randomLeds);
- }
- EVERY_N_MILLISECONDS_RANDOM(105, ADJUSTED_BLINK_TIME) {
- toggleLED(5, randomLeds);
- }
- // Update the LED strip
- EVERY_N_MILLISECONDS(20) {
- FastLED.show();
- }
- }
- // Helper function to toggle an LED's state
- void toggleLED(uint8_t index, CRGBSet &randomLeds) {
- // Toggle the blink state
- blinkState[index] = !blinkState[index];
- if (blinkState[index]) {
- // Generate a new random color when turning on
- colors[index] = CHSV(random8(), random8(180, 255), random8(200, 255));
- }
- // Set LED to either its color or black based on current state
- randomLeds[index] = blinkState[index] ? colors[index] : CRGB::Black;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement