是否可以通过Arduino并行制作多个NEOPIXEL条纹的动画?

Is it possible to animate multiple NEOPIXEL stripes in parallel via Arduino?

提问人:Karbonaterol 提问时间:11/14/2023 更新时间:11/14/2023 访问量:16

问:

我正在为我的一个好朋友做一个正在进行的项目。我对这个编程的东西很陌生,所以我被迫寻求帮助。

他想用一些LED条纹来显示点亮效果。这是棘手的部分。他已经买了 4 个 24V 条纹,但就我而言,这些应该适用于该项目。我已经掌握了一段非常漂亮的代码,它类似于这个项目所需的大多数属性,但它也缺少一些东西,而且我太缺乏经验,无法自己处理它。

首先,他希望条纹一个接一个地动画化,但没有任何明显的顺序。我本来想投入一些RNG来做这项工作,但我惨遭失败。除了杀死整个代码之外,没有太多的选择,我真的不太了解它。所以这部分也是没问题的。

动画本身很棒,我真的很喜欢它。唯一让我感到不安的是那里只使用了 3 种颜色的限制。它们也遵循相同的顺序,我还没有找到一种方法来随机化它们

如果有人能帮我解决这个问题,我会非常高兴。

这是代码,如果有人需要它。

#include "FastLED.h"   //Make sure to install the FastLED library into your Arduino IDE

//The total number of LEDs being used is 144
#define NUM_LEDS 60

// The data pin for the NeoPixel strip is connected to digital Pin 6 on the Arduino
#define DATA_PIN 4

//Initialise the LED array, the LED Hue (ledh) array, and the LED Brightness (ledb) array.
CRGB leds[NUM_LEDS];
byte ledh[NUM_LEDS];
byte ledb[NUM_LEDS];

const int LEDSpeed = 30; //Speed of the LED animation effect. Make sure not to exceed the maxLEDSpeed value.
int maxLEDSpeed = 50;    //Identifies the maximum speed of the LED animation sequence
int LEDposition=0;       //Identifies the LED position in the strip that the comet is currently at. The maximum position is NUM_LEDS-1  (eg. 143)
int oldPosition=0;       //Holds the previous position of the comet.

byte hue = 0;            //Stores the Leading LED's hue value (colour)
byte sat = 255;          //Stores the Leading LED's saturation value
byte tailHue = 0;        //Stores the Comet tail hue value
int tailLength = 8;      //determines the length of the tail.
byte hueRange = 20;      //Colour variation of the tail (greater values have greater variation
byte intensity = 200;    //The default brightness of the leading LED
byte tailbrightness= intensity / 2;  //Affects the brightness and length of the tail
int animationDelay = 0;  //The greater the animation delay, the slower the LED sequence. Calculated from LEDSpeed and MaxSpeed.

unsigned long waitTime = random(50, 5000);  //The wait time for each comet
unsigned long endTime;   //The time when we no longer have to wait for the next comet

int cometNumber = 3;     //Used to choose which comet colour to show (***Don't change this variable***)


//===================================================================================================================================================
// setup() : Is used to initialise the LED strip
//===================================================================================================================================================
void setup() {
    
    FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);                            //initialise the LED strip     
    selectNextComet();                                                                  //Select the next comet colour
}


//===================================================================================================================================================
// loop() : 
//===================================================================================================================================================
void loop(){
    showLED(LEDposition, hue, sat, intensity);

    //Adjust the hue of the tail so that it is a different colour from the leading LED. To reduce variation in tail colour, reduce the hueRange.
    if(hue>(254-hueRange)){
      tailHue = random((hue-hueRange),hue);
    } else {
      tailHue = random(hue, (hue+hueRange)); 
    }
    
    tailbrightness = random(50, 100);                      //Randomly select the brightness of the trailing LED (provides sparkling tail)

    
    leds[LEDposition]=CHSV((tailHue),sat,tailbrightness);  //Set the colour, saturation and brightness of the trailing LED
    fadeLEDs(tailLength);                                  //Fade the tail so that the tail brightness dwindles down to nothingness.
    setDelay(LEDSpeed);                                    //

    LEDposition++;
    if(LEDposition>(NUM_LEDS-1)){
      
      for(int i=0; i<50; i++){
        showLED(LEDposition, hue, sat, intensity);
        fadeLEDs(tailLength);
        setDelay(LEDSpeed);
      } 
      LEDposition=0;
      selectNextComet();                                  //Select the next comet colour
    }
  
}


//===================================================================================================================================================
// showLED() : is used to illuminate the LEDs 
//===================================================================================================================================================
void showLED(int pos, byte LEDhue, byte LEDsat, byte LEDbright){
  leds[pos] = CHSV(LEDhue,LEDsat,LEDbright);
  FastLED.show();
}


//===================================================================================================================================================
// fadeLEDs(): This function is used to fade the LEDs back to black (OFF) 
//===================================================================================================================================================
void fadeLEDs(int fadeVal){
  for (int i = 0; i<NUM_LEDS; i++){
    leds[i].fadeToBlackBy( fadeVal );
  }
}


//===================================================================================================================================================
// setDelay() : is where the speed of the LED animation sequence is controlled. The speed of the animation is controlled by the LEDSpeed variable.
//              and cannot go faster than the maxLEDSpeed variable.
//===================================================================================================================================================
void setDelay(int LSpeed){
  animationDelay = maxLEDSpeed - abs(LSpeed);
  delay(animationDelay);
}


//===================================================================================================================================================
//selectNextComet() : This is where we select either the Blue, the Pink or the White comet         
//===================================================================================================================================================
void selectNextComet(){
  cometNumber++;
  if(cometNumber>3){
    cometNumber=1;
  }

  switch(cometNumber){
    case 1:  {    //Blue Comet
      hue = 160;
      sat = 255;
      hueRange=20;
      break;
    }
 
    case 2:  {   //Pink Comet
      hue = 224;
      sat = 120;
      hueRange=10;
      break;
    }
    
    default: {   //White Comet
      hue = 0;
      sat = 0;
      hueRange = 0;
      break;
    }  
  }
}


//===================================================================================================================================================
// waitForNextComet() : Is where we either wait for 60 seconds for another comet to come, or initiate another comet with a button press.
//                      The button will only be "ACTIVE" while we are waiting for the next comet. It has no effect while a comet is currently running         
//===================================================================================================================================================

上面都说了。

Arduino RGB Arduino-IDE Neopixel

评论


答: 暂无答案