我在试图解决如何循环遍历Adafruit_NeoPixel对象数组时遇到了问题。但我不能用我的生命来理解出了什么问题。我已经在谷歌上查过这个问题,并尝试着通过Ardrino和堆栈流来调整代码,以适应我看到的其他人所做的事情(例如,不是列出数组中的adafruit_neopixles对象,而是在数组中创建neopixles ),以使其正常工作,但我仍然没有运气。
下面是一个简单的例子:这个脚本应该让最初的6个Leds亮起绿色的蓝色
#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();
}然而,这没有任何作用,没有一个领导的灯在任何。-WHY!?然而,当我注释掉For循环和取消注释strips[0].begin时,它确实工作了。那这是为什么?我有什么不懂的?
编辑:所以我尝试更改代码中的一些内容,以测试它是否对我添加的任何影响。
for (int j=0; j<10;j++){
strips[0].setPixelColor(j, 0,100,0);
}它在循环中工作,但是现在循环之后的任何东西都不再工作了。
编辑2:所以在使用了串行之后,我发现当循环结束时,设备就崩溃了。那为什么呢?
编辑3:首先,我想对格拉杜说声对不起,他们试着编辑这个,但仍然没有完全正确的想法。
第二,我想说,一半的问题已经解决了,不正常的行为是由微控制器的崩溃引起的,而这些条子都是文盲。至于为什么这仍然是我需要帮助才能理解的。
所以在它崩溃之前,我已经通过了一个循环,所以进展如何?我接受了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");
}发布于 2020-11-29 23:20:26
因此,我已经解决了这个问题,通过换掉adafruit的谎言,没有更多的崩溃或不正常的行为。
https://stackoverflow.com/questions/64965780
复制相似问题