Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* FastLED - Claude 2.5 experiment with CRGBSet
- 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
- Then try:
- Explain how you are using CRGBSet in each function
- */
- #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
- CRGB leds[NUM_LEDS];
- // Define how fast the rainbow cycles (higher = faster)
- #define RAINBOW_SPEED 10
- // Define the timing for the red blinking effect
- #define BLINK_INTERVAL 500 // Time in milliseconds for on/off
- // Variables to track timing
- unsigned long previousBlinkMillis = 0;
- bool blinkState = false;
- 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();
- }
- void loop() {
- // Call our two functions to control different parts of the strip
- blinkFirstTwoRed();
- cycleRainbow();
- // Update the LED strip
- FastLED.show();
- }
- void blinkFirstTwoRed() {
- // Get the current time
- unsigned long currentMillis = millis();
- // Using CRGBSet to address the first two LEDs as a set
- CRGBSet firstTwo(leds, 2);
- // Check if it's time to toggle the blink state
- if (currentMillis - previousBlinkMillis >= BLINK_INTERVAL) {
- previousBlinkMillis = currentMillis;
- blinkState = !blinkState;
- // Set the LEDs based on the blink state
- if (blinkState) {
- firstTwo = CRGB::Red; // Turn them red
- } else {
- firstTwo = CRGB::Black; // Turn them off
- }
- }
- }
- void cycleRainbow() {
- // Using CRGBSet to address LEDs 3 through 8 as a set
- CRGBSet rainbowLeds(leds+2, 6); // Start at index 2 (third LED), for 6 LEDs
- // Create a changing hue for the rainbow effect
- static uint8_t hue = 0;
- // Fill the rainbow LEDs with a rainbow gradient
- fill_rainbow(rainbowLeds, 6, hue, 255/6);
- // Increment the hue for animation
- hue += RAINBOW_SPEED;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement