Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Happy to explain how CRGBSet is being used in each function!
- ### CRGBSet in the `blinkFirstTwoRed()` function
- CRGBSet firstTwo(leds, 2);
- Here, I'm creating a CRGBSet called `firstTwo` that references the first 2 LEDs from the main `leds` array. The constructor takes two parameters:
- 1. A pointer to the starting LED (`leds` - the beginning of the array)
- 2. The number of LEDs to include in the set (2)
- Then when it's time to change the state of these LEDs, I use a single assignment:
- if (blinkState) {
- firstTwo = CRGB::Red; // Turn them red
- } else {
- firstTwo = CRGB::Black; // Turn them off
- }
- This is the real power of CRGBSet - I can set all LEDs in the set to the same color with a single assignment. Without CRGBSet, I would need to iterate through each LED and set them individually.
- ### CRGBSet in the `cycleRainbow()` function
- CRGBSet rainbowLeds(leds+2, 6); // Start at index 2 (third LED), for 6 LEDs
- In this case, I'm creating a CRGBSet called `rainbowLeds` that:
- 1. Starts at `leds+2` - this is pointer arithmetic that points to the third LED (index 2)
- 2. Includes 6 LEDs (so LEDs at positions 2, 3, 4, 5, 6, and 7)
- Then I use the FastLED function `fill_rainbow()` to create the rainbow effect:
- fill_rainbow(rainbowLeds, 6, hue, 255/6);
- The `fill_rainbow()` function is designed to work with a CRGB array, but since CRGBSet can be used as if it were an array, I can pass it directly. This creates a smooth rainbow gradient across LEDs 3-8.
- The advantage of using CRGBSet here is that I can work with a subset of LEDs without having to create temporary variables or use offset calculations in each loop iteration. It makes the code cleaner and more focused on the specific set of LEDs I want to manipulate.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement