2017-01-23 134 views
0

我目前正在使用Raspberry Pi進行鍛鍊。Arduino-uno項目

我與實驗電路板下面的工作:

  • 5個LED的
  • 2開關

我下面的代碼應該如下方式工作:

隨着第一個按鈕可以選擇下一個LED,第二個按鈕可以將選定的LED打開/關閉。當你在最後的LED它給出了一個真或假(或關閉),使用以下的輸出:

Circuits 我看不出有什麼錯我的代碼:

//const variables 
const int leds[] = {3, 5, 6, 9, 11}; 
const int buttons[] = {12, 13}; 

//variables that will change 
int buttonState[] = {false, false}; 
int lastButtonState[] = {false, false}; 
int values[] = {false, false, false, false}; 

void setup() { 
    //init LEDs 
    for(int i = 0; i < sizeof(leds); i++){ 
     pinMode(leds[i], OUTPUT); 
    } 

    //init buttons 
    for(int i = 0; i < sizeof(buttons); i++){ 
     pinMode(buttons[i], INPUT); 
    } 
} 

void loop() { 
    //fade when game starts 
    fade(); 

    //start game 
    start(); 

    //output of game 
    output(); 
} 

void output(){ 
    bool t1 = !values[0]; 
    bool t2 = t1 && values[1]; 
    bool t3 = values[2] || values[3]; 
    bool Q = !(t2 || t3); 
    if(!Q){ 
     digitalWrite(leds[4], true); 
    }else{ 
     digitalWrite(leds[4], false); 
    } 
} 

void start(){ 
    //total of leds 
    int j = 0; 
    //check if user is not at 5th led 
    while(j < 4){ 
     //loop through buttons 
     for(int i = 0; i < 2; i++){ 
      // Read button 
      buttonState[i] = digitalRead(buttons[i]); 
      // check button state 
      if (buttonState[i] != lastButtonState[i]) { 
       // if the state has changed 
       if (buttonState[i] == HIGH) { 
        //check if button 1 
        if(i == 0){ 
         //select next LED 
         j++; 
        } 
        //else button 2 
        else{ 
         // if the current state of the 2nd button is HIGH 
         while(i == 1){ 
          //if current value of led is false, put it true 
          if(values[j] == false){ 
           //put led on 
           digitalWrite(leds[j], true); 
           values[j] = true; 
           delay(50); 
          }else{ 
           //put led off 
           digitalWrite(leds[j], false); 
           delay(50); 
           values[j] = false; 
          } 
          //go back to button 1? 
          i = 0; 
         } 
        } 
        //go back to button 1? 
        i = 0; 
       } 
      } 
      // save the current state as the last state, 
      // for next time through the loop 
      lastButtonState[i] = buttonState[i]; 

      // wait a little 
      delay(50); 
     } 
    } 
} 

void fade(){ 
    //put every led on half-on 
    for(int i = 0; i < sizeof(leds); i++){ 
     analogWrite(leds[i], 100); 
    } 
} 
+2

當然,這不會建立沒有警告?你爲什麼不提到任何問題? pinMode(leds,OUTPUT);'語句必須是錯誤的,它應該是'pinMode(leds [i],OUTPUT);',畢竟這是循環的重點(和輸入相同)。 – unwind

+0

當我驗證它,它運行時沒有給我錯誤,但我現在更新它。所以這不再是問題。 所以目前第二個按鈕,應該把LED打開/關閉只能關閉LED而不能開啓。 – DiceOfDoom

+0

只要你的問題純粹是關於軟件,這裏就是主題。但只是爲了記錄,應該向http://electronics.stackexchange.com/(他們需要除代碼之外的一些示意圖)詢問可能與軟件或硬件相關的問題。有關Ardunio的具體問題,請訪問http://arduino.stackexchange.com/。還有http://raspberrypi.stackexchange.com/。 – Lundin

回答

1

在C和C++中,如果要比較兩個值以檢查它們是否相等,則必須使用==(比較運算符)而不是=(賦值運算符)。您accidentily使用了錯誤的操作在這裏:

while(i = 1){ 

在這裏:

if(values[j] = false){ 

改變那些==

+0

感謝提及我更新了它,但可悲的是,這並沒有解決我的問題 – DiceOfDoom