Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Advertisement
BurningWreck

FastLED - CRGBSet with Claude

Apr 9th, 2025
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | Software | 0 0
  1. /* FastLED - Claude 2.5 experiment with CRGBSet
  2.  Prompt: For Arduino, using FastLED, use CRGBSet to control an 8-pixel strand of Neopixels. The first two pixels should be controlled by one function which blinks them red, and pixels 3-8 should cycle a rainbow effect
  3.  
  4. Then try:
  5. Explain how you are using CRGBSet in each function
  6. */
  7.  
  8. #include <FastLED.h>
  9.  
  10. // Define the number of LEDs in your strip
  11. #define NUM_LEDS 8
  12. #define DATA_PIN 6  // Connect your NeoPixel data line to this Arduino pin
  13.  
  14. // Create an array for the LEDs
  15. CRGB leds[NUM_LEDS];
  16.  
  17. // Define how fast the rainbow cycles (higher = faster)
  18. #define RAINBOW_SPEED 10
  19.  
  20. // Define the timing for the red blinking effect
  21. #define BLINK_INTERVAL 500  // Time in milliseconds for on/off
  22.  
  23. // Variables to track timing
  24. unsigned long previousBlinkMillis = 0;
  25. bool blinkState = false;
  26.  
  27. void setup() {
  28.   // Initialize the FastLED library with your strip configuration
  29.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  30.   FastLED.setBrightness(64);  // Set to about 25% brightness
  31.  
  32.   // Clear all LEDs to start
  33.   FastLED.clear();
  34.   FastLED.show();
  35. }
  36.  
  37. void loop() {
  38.   // Call our two functions to control different parts of the strip
  39.   blinkFirstTwoRed();
  40.   cycleRainbow();
  41.  
  42.   // Update the LED strip
  43.   FastLED.show();
  44. }
  45.  
  46. void blinkFirstTwoRed() {
  47.   // Get the current time
  48.   unsigned long currentMillis = millis();
  49.  
  50.   // Using CRGBSet to address the first two LEDs as a set
  51.   CRGBSet firstTwo(leds, 2);
  52.  
  53.   // Check if it's time to toggle the blink state
  54.   if (currentMillis - previousBlinkMillis >= BLINK_INTERVAL) {
  55.     previousBlinkMillis = currentMillis;
  56.     blinkState = !blinkState;
  57.    
  58.     // Set the LEDs based on the blink state
  59.     if (blinkState) {
  60.       firstTwo = CRGB::Red;  // Turn them red
  61.     } else {
  62.       firstTwo = CRGB::Black;  // Turn them off
  63.     }
  64.   }
  65. }
  66.  
  67. void cycleRainbow() {
  68.   // Using CRGBSet to address LEDs 3 through 8 as a set
  69.   CRGBSet rainbowLeds(leds+2, 6);  // Start at index 2 (third LED), for 6 LEDs
  70.  
  71.   // Create a changing hue for the rainbow effect
  72.   static uint8_t hue = 0;
  73.  
  74.   // Fill the rainbow LEDs with a rainbow gradient
  75.   fill_rainbow(rainbowLeds, 6, hue, 255/6);
  76.  
  77.   // Increment the hue for animation
  78.   hue += RAINBOW_SPEED;
  79. }
Tags: FastLED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement