2016-05-02 108 views
0

我想使用neoPixel和atm 8 led灯带(会更长时间后)闪烁颜色。我想要做的是给出一个像素信息的列表,并通过列表循环,并按照“脚本数组”的说法闪烁灯光。Arduino Uno阵列失败

这是到目前为止,我已经做了代码:

#include <Adafruit_NeoPixel.h> 
#ifdef __AVR__ 
    #include <avr/power.h> 
#endif 

#define PIN 6 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800); 

void setup() { 

    strip.begin(); 
    strip.show(); 
    int array[2][8][3] = { 
    {{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40}}, 
    {{50, 90, 200}, {50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200}} 
    }; // flashing two colors on all leds 
} 

void loop() { 
    fromArray(50); 
} 

void fromArray(uint8_t wait){ 
    for(int i=0; i<2; i++){ 
    for (int j=0; j<8; j++){ 
     strip.setPixelColor(j, strip.Color(array[i][j][0],array[i][j][1],array[i][j][2])) 
    } 
    strip.show(); 
    delay(wait) 
    } 
} 

当我检查这个代码,我从线strip.setPixelColor(j, strip.Color(array[i][j][0],array[i][j][1],array[i][j][2]))得到错误'array' was not declared in this scope

回答

0

您的array变量在setup函数内声明,且仅在此函数内可用。你只需要的array申报进入全球范围内(在setup功能之外。

#include <Adafruit_NeoPixel.h> 
#ifdef __AVR__ 
    #include <avr/power.h> 
#endif 

#define PIN 6 

Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800); 

int array[2][8][3] = { 
    {{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40}}, 
    {{50, 90, 200}, {50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200}} 
    }; // flashing two colors on all leds 

void setup() { 

    strip.begin(); 
    strip.show(); 
} 

void loop() { 
    fromArray(50); 
} 

void fromArray(uint8_t wait){ 
    for(int i=0; i<2; i++){ 
    for (int j=0; j<8; j++){ 
     strip.setPixelColor(j, strip.Color(array[i][j][0],array[i][j][1],array[i][j][2])); 
    } 
    strip.show(); 
    delay(wait); 
    } 
} 

您还缺少你fromArray功能,我在我的版本增加了一些分号。

+0

谢谢你的快速回答,你们俩。不知何故,那里滑倒了,好吧。时间继续与相框:) – Duzzz

0

因为你数组在setup()函数声明,而不是你的代码的其余部分可见您收到此错误。

你应该把它移动到顶端。

int array[2][8][3] = { 
    {{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40}}, 
    {{50, 90, 200}, {50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200}} 
    }; // flashing two colors on all leds 

void setup() { 

    strip.begin(); 
    strip.show(); 

} 

void loop() { 
    fromArray(50); 
} 
+0

谢谢你的快速答案,你们俩。不知何故,那里滑倒了,好吧。时间继续相框:) – Duzzz