我尝试用pi 3控制rgb ws2812b led灯带。这很好用。现在我想用我的Arduino Nano来做。控件本身起作用。如果我将一些代码放入循环函数,则一切正常。但是,如果我想通过一个函数来调用代码,比如说void colorWipe(){change color},然后在循环中调用colorWipe(),它就不会再更改颜色了。为什么?
代码如下:
#include <Adafruit_NeoPixel.h>
#define shortStrip 2
#define longStrip 3
#define led_count_short 23
#define led_count_long 277
Adafruit_NeoPixel strip_short = Adafruit_NeoPixel(led_count_short, shortStrip, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip_long = Adafruit_NeoPixel(led_count_long, longStrip, NEO_GRB + NEO_KHZ800);
void setup(){
Serial.begin(9600);
strip_short.begin();
strip_short.setBrightness(50);
strip_short.show();
Serial.println("Short strip is running!");
delay(50);
strip_long.begin();
strip_long.setBrightness(50);
strip_long.show();
Serial.println("Long strip is running!");
delay(50);
}
void loop(){
colorWipe(10, strip_long, led_count_long, 255, 255, 255);
Serial.println("Finished Long");
delay(1000);
colorWipe(10, strip_long, led_count_long, 255, 0, 0);
Serial.println("This too Long");
delay(1000);
}
void colorWipe(uint8_t wait, Adafruit_NeoPixel strip, int led_count, int r, int g, int b){
Serial.print("1");
for(int i = 0; i < led_count; i++){
strip.setPixelColor(i, strip.Color(r,g,b));
strip.show();
delay(wait);
}
Serial.print("2");
return;
}
是的,我确实有2个LED灯带,但在循环中仅调用了1个。我的串行监视器可以完美打印所有内容,但颜色不会改变。我尝试了多种颜色。第一个colorWipe()有效,之后的所有colorwipe无效。
PLS帮助
非常感谢您
答案 0 :(得分:0)
您是否尝试过使用引用?将colorWipe
更改为
void colorWipe(uint8_t wait, Adafruit_NeoPixel & strip, int led_count, int r, int g, int b)
我的猜测是,当您复制strip
对象时,无法再访问setPixelColor
和show
。您应该使用在代码开头声明的对象,可以使用引用来完成。