Advertisement
BurningWreck

FastLED - CRGBSet attempt

Apr 8th, 2025 (edited)
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | Software | 0 0
  1. // FastLED experiment with Gemini 2.5
  2. // Uses 8 pixel strand with CRGBSet to control two sections
  3. // 4/7/2025
  4.  
  5. #include <FastLED.h>
  6.  
  7. // -- Configuration --
  8. #define NUM_LEDS 8        // Total number of LEDs in the strip
  9. #define DATA_PIN 6        // Arduino pin connected to the Neopixel data line
  10. #define LED_TYPE WS2812B  // Type of LED strip (WS2811, WS2812B, etc.)
  11. #define COLOR_ORDER GRB   // Color order for the LED strip (GRB, RGB, etc.)
  12. #define BRIGHTNESS 60     // Set initial brightness (0-255). Start low!
  13.  
  14. // -- Constants for Effects --
  15. // Blinking Section (LEDs 0-1)
  16. const int BLINK_INTERVAL = 3000;          // milliseconds for first two LEDs
  17.  
  18. // Effect Section (LEDs 2-7) - Slower, Longer Blinks
  19. const uint8_t TURN_ON_CHANCE = 3;        // Chance (out of 256) per frame for an OFF LED to turn ON. LOW value = infrequent blinks.
  20. const unsigned long MIN_ON_TIME = 800;   // Minimum time (ms) an LED stays ON
  21. const unsigned long MAX_ON_TIME = 2500;  // Maximum time (ms) an LED stays ON
  22.  
  23. // -- Global Variables --
  24. CRGB leds[NUM_LEDS];      // Array to hold LED color data
  25.  
  26. // State for LEDs 2-7
  27. struct EffectLedState {
  28.   bool isOn = false;
  29.   unsigned long turnOffTime = 0;
  30.   CRGB color = CRGB::Black;
  31. };
  32. // Create state array only for the effect LEDs (indices 2-7)
  33. // Map physical index (2-7) to state index (0-5)
  34. EffectLedState effectLedStates[NUM_LEDS - 2]; // Size 6 for 6 LEDs
  35.  
  36. // Timing for blinking section (LEDs 0-1)
  37. unsigned long lastBlinkTime = 0;
  38. bool blinkState = false;       // Current state: true = ON (Red), false = OFF (Black)
  39.  
  40. // -- Setup Function --
  41. void setup() {
  42.   Serial.begin(9600);
  43.   delay(2000); // Give serial time to connect
  44.   Serial.println("\n\nFastLED: Slow/Long Random Blinks");
  45.  
  46.   randomSeed(analogRead(0)); // Seed random number generator
  47.  
  48.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
  49.          .setCorrection(TypicalLEDStrip);
  50.   FastLED.setBrightness(BRIGHTNESS);
  51.   FastLED.clear(); // Initialize all LEDs to off
  52.   FastLED.show();
  53.   delay(50);
  54.   Serial.println("Setup complete. Starting loop...");
  55. }
  56.  
  57. // -- Main Loop --
  58. void loop() {
  59.   updateBlinkingSection();     // Update state for LEDs 0, 1
  60.   updateSlowRandomBlinkSection(); // Update state for LEDs 2-7
  61.  
  62.   // Send the combined state of the 'leds' array to the strip
  63.   FastLED.show();
  64.  
  65.   // Keep delay for overall frame rate control
  66.   FastLED.delay(16); // Roughly 60 frames per second
  67. }
  68.  
  69. // -- Update Functions --
  70.  
  71. // Function to handle the first two LEDs (blinking red) - UNCHANGED
  72. void updateBlinkingSection() {
  73.   unsigned long currentTime = millis();
  74.   if (currentTime - lastBlinkTime >= BLINK_INTERVAL) {
  75.     lastBlinkTime = currentTime;
  76.     blinkState = !blinkState;
  77.   }
  78.   if (blinkState) {
  79.     leds[0] = CRGB::Red;
  80.     leds[1] = CRGB::Red;
  81.   } else {
  82.     leds[0] = CRGB::Black;
  83.     leds[1] = CRGB::Black;
  84.   }
  85. }
  86.  
  87. // Function to handle LEDs 2-7 (slow, long random blinking colors) - NEW LOGIC
  88. void updateSlowRandomBlinkSection() {
  89.   unsigned long currentTime = millis();
  90.  
  91.   // Loop through effect section LEDs (indices 2 to NUM_LEDS-1)
  92.   for (int i = 2; i < NUM_LEDS; i++) {
  93.     int stateIndex = i - 2; // Map LED index (2-7) to state array index (0-5)
  94.  
  95.     // --- Check LED state ---
  96.     if (effectLedStates[stateIndex].isOn) {
  97.       // --- LED is currently ON ---
  98.       // Check if its time to turn OFF
  99.       if (currentTime >= effectLedStates[stateIndex].turnOffTime) {
  100.          effectLedStates[stateIndex].isOn = false; // Update state
  101.          leds[i] = CRGB::Black;                   // Set physical LED off
  102.          effectLedStates[stateIndex].color = CRGB::Black; // Clear stored color
  103.       } else {
  104.          // Still ON - ensure the physical LED has the correct color
  105.          // (This handles cases where it might have been accidentally turned off)
  106.          leds[i] = effectLedStates[stateIndex].color;
  107.       }
  108.  
  109.     } else {
  110.       // --- LED is currently OFF ---
  111.       // Give it a very small chance to turn ON
  112.       if (random8() < TURN_ON_CHANCE) {
  113.           // Calculate ON duration
  114.           unsigned long onDuration = random(MIN_ON_TIME, MAX_ON_TIME + 1); // random() excludes upper bound
  115.  
  116.           // Update state
  117.           effectLedStates[stateIndex].isOn = true;
  118.           effectLedStates[stateIndex].turnOffTime = currentTime + onDuration;
  119.           effectLedStates[stateIndex].color = CHSV(random8(), 220, 255); // New random color (slightly less saturated)
  120.  
  121.           // Set physical LED ON
  122.           leds[i] = effectLedStates[stateIndex].color;
  123.       }
  124.        // else: LED remains OFF (leds[i] should be Black)
  125.     }
  126.   } // End loop through effect LEDs
  127. }
Tags: FastLED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement