【发布时间】:2020-11-23 09:31:42
【问题描述】:
我在尝试找出如何循环遍历 Adafruit_NeoPixel 对象数组时遇到了问题。 但我一辈子都无法弄清楚出了什么问题。我在 google 上查找了这个问题,并通过 Ardrino 和 stack over flow 尝试调整代码以适应我看到的其他人所做的事情(例如,不是在数组中列出 adafruit_neopixles 对象,而是在数组中创建 neopixles)以获得它可以工作,但我仍然没有运气。
这里有一个简单的例子:这个脚本应该让前 6 个 LED 亮绿蓝
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strips[] = {
Adafruit_NeoPixel(32, 5, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(32, 6, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(32, 7, NEO_GRB + NEO_KHZ800),
};
#define NUMSTRIPS (sizeof(strips)/sizeof(strips[0]))
void setup() {
//Edit2
Serial.begin(115200);
//end Edit2
for(int i=0; i<NUMSTRIPS; i++)
{
strips[i].begin();
strips[i].setBrightness(255); //adjust brightness here
/*This is code that ive added in AFTER i made the orgional post to see if it had any difference
*/
for (int j=0; j<10;j++){
strips[0].setPixelColor(j, 0,100,0);
}
/* End of edited code
*/
strips[i].show(); // Initialize all pixels to 'off'
}
//Edit2
Serial.println("Loop end");
//End Edit2
//strips[0].begin();
strips[0].setBrightness(255);
strips[0].setPixelColor(0, 0,100,255);
strips[0].setPixelColor(1, 0,100,255);
strips[0].setPixelColor(2, 0,100,255);
strips[0].setPixelColor(3, 0,100,255);
strips[0].setPixelColor(4, 0,100,255);
strips[0].setPixelColor(5, 0,100,255);
strips[0].show();
}
但是,这根本没有任何 LED 亮起。 -为什么!?
然而,当我注释掉 For 循环并取消注释 strips[0].begin 时,它确实有效。
那么这是为什么呢?我有什么不明白的?
编辑:所以我尝试在我的代码中更改一些内容以测试它是否有任何影响 我添加了
for (int j=0; j<10;j++){
strips[0].setPixelColor(j, 0,100,0);
}
它在循环内工作,但现在循环后的任何东西都不再工作了。
编辑 2:所以在使用串行后,我发现设备在循环结束时崩溃。那为什么会这样呢?
编辑 3: 首先,我想对 ocrdu 说声抱歉,他们试图编辑此内容,但仍然没有完全正确的想法。
其次我想说的是,问题已经解决了一半,异常行为是由于条带文盲时微控制器崩溃引起的。至于为什么这仍然是我需要帮助理解的。
所以我现在在它崩溃之前完成了 1 个循环,所以进展了吗?我接受了 Botje 的建议,我使用了strips[i].numPixels(); 而不是使用固定值
但是我决定再做一些测试,看看我是否已经解决了这个问题,而且它在测试 2 开始之前就崩溃了。这是新代码。
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strips[] = {
Adafruit_NeoPixel(32, 5, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(32, 6, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(32, 7, NEO_GRB + NEO_KHZ800),
};
#define NUMSTRIPS (sizeof(strips)/sizeof(strips[0]))
void setup() {
Serial.begin(115200);
Serial.println("1st Test Do a loop with out crashing over all the strips");
for(int i=0; i<NUMSTRIPS; i++)
{
strips[i].begin();
strips[i].setBrightness(255);
for (int j=0; j<strips[i].numPixels();j++){
strips[i].setPixelColor(j, 0,100,0);
Serial.println(j);
}
Serial.println("b4 show");
strips[i].show();
}
Serial.println("Loop end");
Serial.println("If you are reading this then the microcontroller did not crash on 1st Test");
delay(150);
Serial.println("2nd Test Do a loop with out crashing over 1 strip");
for (int k=0; k<strips[0].numPixels();k++)
{
strips[0].setPixelColor(k, 0,100,255);
strips[0].show();
Serial.println(k);
}
Serial.println("Loop end");
Serial.println("If you are reading this then the microcontroller did not crash the 2nd Test");
delay(150);
Serial.println("3rd Test Do a loop changing onley 6 pixles on 1 strip with out crashing");
for (int l=0; l<=5; l++)
{
strips[0].setPixelColor(l, 200,0,0);
strips[0].show();
Serial.println(l);
}
Serial.println("Loop end");
Serial.println("If you are reading this then the microcontroller did not crash the 3rd Test");
}
【问题讨论】:
-
您不会在 for 循环中调用 setPixelColor。如果我正确理解了文档,那么当您调用 show 时,所有设置的 Brightness 都会将存储的像素颜色相乘。我敢打赌默认像素颜色是黑色。
-
因为它将颜色设置在循环之外。但我在循环中对其进行了测试只是为了检查并且仍然有同样的问题
-
这不在您显示的代码中。请显示实际代码。
-
我应该发布什么版本的代码?自从您的第一篇帖子以来,我尝试了至少 7 种不同的方式在循环内设置 setpixle,最有趣的是
for (int j=0; j<10;j++){ strips[0].setPixelColor(j, 0,100,0); },因为这种方式有效,但循环外的任何内容都不再有效 -
你不应该循环到
strips[i].numPixels()吗?如果可用像素少于 10 个,j<10循环会在其他内存上乱涂乱画,并可能导致崩溃。
标签: c++ arrays arduino adafruit