user3650687
user3650687

Reputation: 11

lighting two ends of LED strip using Arduino fastLED library

I have a LED strip(WS2812B) of 60 LED's. I have the following code which lights up a LED at the beginning of the strip and sends it down to the end, once it reaches the end it 'bounces back' and returns down the strip to the start.

What I'm trying to do is to have both ends of the LED strip light up with a led and a small trail behind it, these LED's then move down the strip to the opposite ends and cross over when they meet.

I'm trying to figure out how to run two lines of code at once, as currently it sends the lights down one way, then it runs the other code. Any help would be appreciated

Below is my code so far.

#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 57

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 4
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];
int end_led = 55;
void setup() { 
    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() { 

    // First slide the led in one direction
    for(int i = 0; i < NUM_LEDS; i++) {
        // Set the i'th led to
        leds[i] = CRGB::Red;
        // Show the leds
        FastLED.show();
        // now that we've shown the leds, reset the i'th led to black
        leds[i] = CRGB::Black;
        // Wait a little bit before we loop around and do it again
        delay(30);
    }

    // Now go in the other direction.  
    for(int i = NUM_LEDS-1; i >= 0; i--) {
        // Set the i'th led to red 
        leds[i] = CRGB::Red;
        // Show the leds
        FastLED.show();
        // now that we've shown the leds, reset the i'th led to black
        leds[i] = CRGB::Black;
        // Wait a little bit before we loop around and do it again
        delay(30);
    }

}

Upvotes: 1

Views: 12035

Answers (2)

Christopher K
Christopher K

Reputation: 73

Granted, this thread is over 2 years old, but I was working on something else when I came across it, but I believe this will get you what you want:

set a variable for the 1st LED in the strip

var startPos = 0;

specify the length of the tail

var tailLength = 5;

Then in your loop

function loop() {
for (var i=0; i<strip.numPixels(); i++){
//set all to black
strip.setPixelColor(i,0,0,0,0);
}

//creates the tail first, to keep main pixel from being overwritten on overlap
for (var j=tailLength;j>=1;j--){
    strip.setPixelColor(startPos-j, 255,0,0,255-((255/tailLength)*j));
    strip.setPixelColor(strip.numPixels()-startPos+j, 255,0,0,255-((255/tailLength)*j));
}   

strip.setPixelColor(startPos, 255,0,0,255);
strip.setPixelColor(strip.numPixels()-startPos,255,0,0,255);
FastLED.show();
startPos++;

if(startPos>=StripNum+tailLength) startPos = 0;
delay(30);
}

This creates a gradually fading tale (by way of brightness) behind the main pixel. This could probably be further simplified, but should be good for human readability sake.

Upvotes: 2

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

This is untested code - use at your own risk. The idea is to merge the two loops, so that a single loop works on both ends on each iteration. That requires changing the indexing for the far end led to use LEDS-1-i rather than i.

To leave a trail, you would have to change which LED is turned off on each iteration, leaving a displacement. I don't know exactly what you want to happen when the trails cross, so I have not attempted to code it.

for(int i = 0; i < NUM_LEDS; i++) {
    // Set the i'th led from start
    leds[i] = CRGB::Red;
    // Set the i'th led from end
    leds[NUM_LEDS - 1 - i] = CRGB::Red;
    // Show the leds
    FastLED.show();
    // now that we've shown the leds, reset the i'th led to black
    leds[i] = CRGB::Black;
    leds[NUM_LEDS - 1 - i] = CRGB::Red;
    // Wait a little bit before we loop around and do it again
    delay(30);
}

Upvotes: 1

Related Questions