2016-05-02 126 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