首页 > 解决方案 > Arduino控制ledstrip与neopixel卡住

问题描述

我尝试用我的 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() 有效,之后的所有颜色擦除都无效。

请帮助

非常感谢

标签: c++arduinoarduino-ideledneopixel

解决方案


您是否尝试过使用参考资料?更改colorWipe

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

我的猜测是,当您正在制作strip对象的副本时,访问setPixelColor并且show不再可能。您应该使用在代码开头声明的对象,这可以使用引用来完成。


推荐阅读