1

私は 60 個の LED の LED ストリップ (WS2812B) を持っています。ストリップの先頭にある LED を点灯させて最後まで送信する次のコードがあります。最後に到達すると、「跳ね返って」ストリップを最初に戻します。

私がやろうとしているのは、LED ストリップの両端を LED で点灯させ、その後ろに小さな道を作ることです。これらの LED はストリップを下って反対側の端まで移動し、交差すると交差します。

現在、ライトを一方向に送信してから、他のコードを実行するため、一度に2行のコードを実行する方法を見つけようとしています。どんな助けでもいただければ幸いです

以下はこれまでの私のコードです。

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

}

4

2 に答える 2

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

これにより、メイン ピクセルの背後に (明るさによって) 徐々に消えていく物語が作成されます。これはおそらくさらに単純化される可能性がありますが、人間の読みやすさのためには良いはずです。

于 2016-11-29T21:33:04.940 に答える
1

これはテストされていないコードです。自己責任で使用してください。アイデアは、2 つのループをマージして、反復ごとに 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);
}
于 2014-05-19T03:29:04.120 に答える