我尝试用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()有效,之后的所有colorwipes无效。

PLS帮助

非常感谢你

最佳答案

您是否尝试过使用引用?将colorWipe更改为

void colorWipe(uint8_t wait, Adafruit_NeoPixel & strip, int led_count, int r, int g, int b)

我的猜测是,当您正在复制strip对象时,就无法再访问setPixelColorshow了。您应该使用在代码开头声明的对象,可以使用引用来完成。

关于c++ - 新像素卡住的Arduino控制LED灯条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57099084/

10-09 05:57