使用Arduino fastLED库照亮LED灯条的两端

时间:2014-05-18 21:36:33

标签: loops arduino light led

我有一个60 LED的LED灯条(WS2812B)。我有以下代码,它会在条带的开头点亮一个LED并将其发送到最后,一旦到达结束它会“反弹”并从条带返回到开始。

我要做的是让LED灯条的两端点亮,背后有一条LED和一条小线,这些LED随后向下移动到相反的两端并在它们相遇时交叉。 / p>

我正在试图弄清楚如何一次运行两行代码,因为它目前以一种方式发送灯光,然后运行其他代码。任何帮助将不胜感激

到目前为止,我的代码如下。

#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);
    }

}

2 个答案:

答案 0 :(得分:2)

当然,这个帖子已经超过2年了,但是当我遇到它的时候我正在研究别的东西,但我相信这会让你得到你想要的东西:

为条带中的第一个LED设置变量

var startPos = 0;

指定尾部的长度

var tailLength = 5;

然后在你的循环中

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);
}

这会在主像素后面创建逐渐消失的故事(通过亮度)。这可能会进一步简化,但应该有利于人类的可读性。

答案 1 :(得分:1)

这是未经测试的代码 - 使用风险由您自行承担。我们的想法是合并两个循环,以便在每次迭代时单个循环在两端都有效。这需要更改远端的索引导致使用LEDS-1-i而不是i

要留下痕迹,您必须在每次迭代时更改哪个LED关闭,留下位移。当小道交叉时,我不确切地知道你想要发生什么,所以我没有尝试编码。

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);
}