2017-02-19 193 views
1

我有一个arduino使用fastLED与600 ws2812b灯条。我正在运行下面的代码:意想不到的变化行为arduino fastLED ws2812b

#include "FastLED.h" 
#define DATA_PIN 5  // change to your data pin 
#define COLOR_ORDER GRB  // if colors are mismatched; change this 
#define NUM_LEDS 600  // change to the number of LEDs in your strip 
#define BRIGHTNESS 32 
#define WRAP_NUM 55 
#define LED_TYPE WS2812B 

CRGB leds[NUM_LEDS]; 
int startIndex=0; 
int bottom=1; 

void setup() 
{ 
    delay(3000); 

    FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); 
    FastLED.setBrightness(BRIGHTNESS); 
} 

void loop() 
{ 
    shapeTwirl(); 
} 

void shapeTwirl() 
{ 
    FastLED.clear(); 

    static int heart[]={bottom*WRAP_NUM}; 
    for(int i=0;i<sizeof(heart);i++) 
    { 
     leds[(heart[i]+startIndex)]=CRGB::Red; 
    } 
    FastLED.show(); 
    delay(70); 

    startIndex=(startIndex+1)%WRAP_NUM; 
} 

我有我的灯在一个圆圈,这使得围绕圆圈红点。然而,一个大约100盏灯的蓝点也在旋转。我的代码中没有任何东西可以制造蓝光。我已经跟踪到使用

int bottom=1; 

如果我用代码中的数字替换底部,我摆脱了蓝点,它正常工作。如果我#define底部1;问题也解决了。如果我现在或在shapeTwirl中定义底部,则无关紧要。这导致我相信有一个错误的底部使用变量,但我已经尝试使用int,static int,unsigned int无济于事。

为什么指示灯不亮?

我使用一个Arduino UNO来控制灯光和外部电源为其供电。

回答

1

注:你应该检查Arduino的IDE配置为打印所有警告


你的代码调用未定义行为

这条线:

static int heart[]={bottom*WRAP_NUM}; 

导致具有的bottom * WRAP_NUM值初始化一种元素的阵列,独立地从bottom值。 我这样评论,因为这可能是也可能不是你想要的。

这里是你的问题:

for(int i=0; i < sizeof(heart); i++) 

sizeof(heart)返回字节您的数组,这是2,因为这是一个int的Arduino大小数量。因此,在循环体

leds[(heart[i]+startIndex)]=CRGB::Red; 

heart[i]的指令访问在第二循环迭代(i == 1)无效的内存位置,这意味着一些其他随机位置可能与你的肤色覆盖。

如果你想知道多少int存储阵列中,那么你需要用sizeof(heart)/sizeof(int)来替代它:

for(int i=0; i < (sizeof(heart)/sizeof(int)); i++) 

至于原因为什么你看到一个蓝光 ,我会检查以下几件事:

  • 确认#define COLOR_ORDER GRB真的是你想要的:我怀疑CRGB::Red将需要RGB作为COLOR_ORDER
  • 验证接线是否正确