如何以最佳方式移动大型阵列和入射次数



我正在创建自己的音乐可视化工具版本,以响应音乐的频率;一个常见的项目。 我正在使用 2 条新像素,每个带有 300 个 LED,总共 600 个 LED。

我编写了如下所示的函数,这些函数可以产生所需的效果,即光脉冲独立地沿着条带传播。 但是,当与音乐实时运行时,每秒的更新速度太慢,无法创建漂亮的脉冲;它看起来断断续续。

我认为问题是调用函数时必须执行的操作数。对于对该函数的每次调用,每个条带的 300 值数组必须移动 5 个索引并添加 5 个新值。

以下是该函数当前工作方式的说明:

-使用任意数字填充数组

-显示 2 个指数的偏移

-X 表示未分配值的索引

-N 表示函数添加的新值

Initial array: [1][3][7][2][9]
Shifted array: [X][X][1][3][7]
New array:     [N][N][1][3][7]

这里如果我的代码。 循环()下面的函数声明。 我正在使用 random() 触发脉冲以进行测试;为简洁起见,未包含其他函数。

#include <FastLED.h>
// ========================= Define setup parameters =========================
#define NUM_LEDS1 300                    // Number of LEDS in strip 1
#define NUM_LEDS2 300                    // Number of LEDS in strip 1
#define STRIP1_PIN 6                     // Pin number for strip 1
#define STRIP2_PIN 10                    // Pin number for strip 2
#define s1Band 1                         // String 1 band index
#define s2Band 5                         // String 2 band index
#define numUpdate 5                      // Number of LEDs that will be used for a single pulse  
// Colors for strip 1: Band 2 (Index 1)
#define s1R 255
#define s1G 0
#define s1B 0
// Colors for strip 2: Band 6 (Index 5)
#define s2R 0
#define s2G 0
#define s2B 255

// Create the arrays of LEDs
CRGB strip1[NUM_LEDS1];
CRGB strip2[NUM_LEDS2];

void setup() {
FastLED.addLeds<NEOPIXEL, STRIP1_PIN>(strip1, NUM_LEDS1);
FastLED.addLeds<NEOPIXEL, STRIP2_PIN>(strip2, NUM_LEDS2);
FastLED.setBrightness(10);
FastLED.clear();
FastLED.show();
}
void loop() {
int num = random(0, 31);
// Pulse strip based on random number for testing
if (num == 5) {
pulseDownStrip1();
}
pulseBlack1();
}


// ======================= FUNCTION DECLARATIONS =======================
// Pulse a set of colored LEDs down the strip
void pulseDownStrip1() {
// Move all current LED states by n number of leds to be updated
for (int i = NUM_LEDS1 - 1; i >= 0; i--) {
strip1[i] = strip1[i - numUpdate];
}
// Add new LED values to the pulse
for (int j = 0; j < numUpdate; j++) {
strip1[j].setRGB(s1R, s1G, s1B);
}
FastLED.show();
}
// Pulse a set of black LEDs down the strip
void pulseBlack1(){
// Move all current LED states by n number of leds to be updated
for (int i = NUM_LEDS1 - 1; i >= 0; i--) {
strip1[i] = strip1[i - numUpdate];
}
// Add new LED values to the pulse
for (int j = 0; j < numUpdate; j++) {
strip1[j].setRGB(0, 0, 0);
}
FastLED.show();
}

我正在寻找有关优化此操作的任何建议。 通过我的研究,将所需的值复制到新数组而不是移动现有数组似乎是一种更快的操作。

如果您对优化此过程有任何建议,或制作相同动画的替代方法,我将不胜感激。

秘诀是不要改变它。 改为从您开始阅读它的位置。 跟踪一个单独的变量,该变量保持起始位置,并将数组的读数更改为从那里开始,当它到达数组长度时回滚到零,并在它开始的位置停止一个。

谷歌术语"循环缓冲区" 查看Arduino HardwareSerial类以获取一个不错的实现示例。

最新更新