Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FastLED experiment with Gemini 2.5
- // Uses 8 pixel strand with CRGBSet to control two sections
- // 4/7/2025
- #include <FastLED.h>
- // -- Configuration --
- #define NUM_LEDS 8 // Total number of LEDs in the strip
- #define DATA_PIN 6 // Arduino pin connected to the Neopixel data line
- #define LED_TYPE WS2812B // Type of LED strip (WS2811, WS2812B, etc.)
- #define COLOR_ORDER GRB // Color order for the LED strip (GRB, RGB, etc.)
- #define BRIGHTNESS 60 // Set initial brightness (0-255). Start low!
- // -- Constants for Effects --
- // Blinking Section (LEDs 0-1)
- const int BLINK_INTERVAL = 3000; // milliseconds for first two LEDs
- // Effect Section (LEDs 2-7) - Slower, Longer Blinks
- const uint8_t TURN_ON_CHANCE = 3; // Chance (out of 256) per frame for an OFF LED to turn ON. LOW value = infrequent blinks.
- const unsigned long MIN_ON_TIME = 800; // Minimum time (ms) an LED stays ON
- const unsigned long MAX_ON_TIME = 2500; // Maximum time (ms) an LED stays ON
- // -- Global Variables --
- CRGB leds[NUM_LEDS]; // Array to hold LED color data
- // State for LEDs 2-7
- struct EffectLedState {
- bool isOn = false;
- unsigned long turnOffTime = 0;
- CRGB color = CRGB::Black;
- };
- // Create state array only for the effect LEDs (indices 2-7)
- // Map physical index (2-7) to state index (0-5)
- EffectLedState effectLedStates[NUM_LEDS - 2]; // Size 6 for 6 LEDs
- // Timing for blinking section (LEDs 0-1)
- unsigned long lastBlinkTime = 0;
- bool blinkState = false; // Current state: true = ON (Red), false = OFF (Black)
- // -- Setup Function --
- void setup() {
- Serial.begin(9600);
- delay(2000); // Give serial time to connect
- Serial.println("\n\nFastLED: Slow/Long Random Blinks");
- randomSeed(analogRead(0)); // Seed random number generator
- FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
- .setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- FastLED.clear(); // Initialize all LEDs to off
- FastLED.show();
- delay(50);
- Serial.println("Setup complete. Starting loop...");
- }
- // -- Main Loop --
- void loop() {
- updateBlinkingSection(); // Update state for LEDs 0, 1
- updateSlowRandomBlinkSection(); // Update state for LEDs 2-7
- // Send the combined state of the 'leds' array to the strip
- FastLED.show();
- // Keep delay for overall frame rate control
- FastLED.delay(16); // Roughly 60 frames per second
- }
- // -- Update Functions --
- // Function to handle the first two LEDs (blinking red) - UNCHANGED
- void updateBlinkingSection() {
- unsigned long currentTime = millis();
- if (currentTime - lastBlinkTime >= BLINK_INTERVAL) {
- lastBlinkTime = currentTime;
- blinkState = !blinkState;
- }
- if (blinkState) {
- leds[0] = CRGB::Red;
- leds[1] = CRGB::Red;
- } else {
- leds[0] = CRGB::Black;
- leds[1] = CRGB::Black;
- }
- }
- // Function to handle LEDs 2-7 (slow, long random blinking colors) - NEW LOGIC
- void updateSlowRandomBlinkSection() {
- unsigned long currentTime = millis();
- // Loop through effect section LEDs (indices 2 to NUM_LEDS-1)
- for (int i = 2; i < NUM_LEDS; i++) {
- int stateIndex = i - 2; // Map LED index (2-7) to state array index (0-5)
- // --- Check LED state ---
- if (effectLedStates[stateIndex].isOn) {
- // --- LED is currently ON ---
- // Check if its time to turn OFF
- if (currentTime >= effectLedStates[stateIndex].turnOffTime) {
- effectLedStates[stateIndex].isOn = false; // Update state
- leds[i] = CRGB::Black; // Set physical LED off
- effectLedStates[stateIndex].color = CRGB::Black; // Clear stored color
- } else {
- // Still ON - ensure the physical LED has the correct color
- // (This handles cases where it might have been accidentally turned off)
- leds[i] = effectLedStates[stateIndex].color;
- }
- } else {
- // --- LED is currently OFF ---
- // Give it a very small chance to turn ON
- if (random8() < TURN_ON_CHANCE) {
- // Calculate ON duration
- unsigned long onDuration = random(MIN_ON_TIME, MAX_ON_TIME + 1); // random() excludes upper bound
- // Update state
- effectLedStates[stateIndex].isOn = true;
- effectLedStates[stateIndex].turnOffTime = currentTime + onDuration;
- effectLedStates[stateIndex].color = CHSV(random8(), 220, 255); // New random color (slightly less saturated)
- // Set physical LED ON
- leds[i] = effectLedStates[stateIndex].color;
- }
- // else: LED remains OFF (leds[i] should be Black)
- }
- } // End loop through effect LEDs
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement