Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This time Claude was told to use EVERY_N_MILLISECONDS
- #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
- #define CHECK_INTERVAL 50 // How often we check for updates
- #define MIN_BLINK_TIME 300 // Minimum time in milliseconds
- #define MAX_BLINK_TIME 2000 // Maximum time in milliseconds
- // Arrays to track state for each random blinking LED
- bool blinkState[NUM_RANDOM_LEDS];
- CRGB colors[NUM_RANDOM_LEDS];
- uint16_t nextChangeTime[NUM_RANDOM_LEDS]; // How many milliseconds until this LED changes state
- 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_seed(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;
- // Assign an initial delay before the first change
- nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
- // 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() {
- // Check for LED updates on a regular interval
- EVERY_N_MILLISECONDS(CHECK_INTERVAL) {
- updateRandomLEDs();
- }
- // Update the LED strip
- EVERY_N_MILLISECONDS(20) {
- FastLED.show();
- }
- }
- void updateRandomLEDs() {
- // Using CRGBSet to address LEDs 2 through 7 (indexes 2-7)
- CRGBSet randomLeds(leds(2, 7));
- // Check each LED to see if it's time to change state
- for (int i = 0; i < NUM_RANDOM_LEDS; i++) {
- // Decrease the time until next change
- if (nextChangeTime[i] > CHECK_INTERVAL) {
- nextChangeTime[i] -= CHECK_INTERVAL;
- } else {
- // Time to change this LED's state
- blinkState[i] = !blinkState[i];
- if (blinkState[i]) {
- // Generate a new random color when turning on
- colors[i] = CHSV(random8(), random8(180, 255), random8(200, 255));
- // Assign new time for how long to stay ON
- nextChangeTime[i] = random16(MIN_BLINK_TIME, MAX_BLINK_TIME);
- } else {
- // Assign new time for how long to stay OFF (slightly different range)
- nextChangeTime[i] = random16(MIN_BLINK_TIME + 100, MAX_BLINK_TIME - 200);
- }
- }
- // Set LED to either its color or black based on current state
- randomLeds[i] = blinkState[i] ? colors[i] : CRGB::Black;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement