2017-08-03 46 views
0

我正在努力創建和訪問存儲在Arduino Uno上的結構數組中的數據。訪問Arduino中的結構數組數據

我不太熟悉C++/Arduino,但在我的腦海中,下面應該可以工作。 燈光和東西都與其他代碼一起工作,但在嘗試用燈光[i]改變燈光時不起作用。

可能是與指針有關,但我仍然不確定何時/在哪裏使用它們。

struct light 
{ 
    byte k; 
    byte r; 
    byte g; 
    byte b; 
}; 

typedef struct light Light; 

Light l1; 
Light l2; 
Light l3; 

Light lights[3] = {l1, l2, l3}; 

void setup() { 
    l1 = {0, 0, 0, 0}; 
    l2 = {0, 0, 0, 0}; 
    l3 = {0, 0, 0, 0}; 
} 

void loop() 
{ 
    l1 = {0, 0, 0, 0}; 
    l2 = {0, 0, 0, 0}; 
    l3 = {0, 0, 0, 0}; 


    //turn on different light every 3 seconds 
    int i; 

    if (millis() % 9000 < 3000) { 
     i = 0; 
    } else if ((millis() % 9000 >= 3000) && (millis() % 9000 < 6000)) { 
     i = 1; 
    } else { 
     i = 2; 
    } 

    lights[i] = {255, 255,0, 0}; 

    // if this is uncommented l1 turns red 
    //l1 = {255,255,0,0} 

    //passes light data to DMX controller (works fine) 
    ~turnOnlight(l1) 
    ~turnOnLight(l2) 
    ~turnOnLight(l3) 
} 

在此先感謝。

+0

Arduino實際上是使用C++,而不是C。 –

+0

至於你的問題,你能否詳細說明一下?代碼有什麼問題?你有錯誤嗎?崩潰?意外的結果?還有別的嗎?請[閱讀關於如何提出好問題](http://stackoverflow.com/help/how-to-ask),如果你還沒有這樣做。 –

+0

@Someprogrammerdude oops,抱歉將編輯。 new to arduino/c/C++ –

回答

0

你可能希望你的陣列指向相應的燈,那麼你就需要燈*燈[3] = {& L1,& L2,& L3}和accesing你必須取消引用指針像這樣的數組成員時* lights [i] = {255,255,0,0};