2016-03-22 53 views
0
void setup() { 

    for (int i = 0; i < pinCount; thisPin++) { 
    pinMode(ledPins[thisPin], OUTPUT); 
    } 
} 

void loop() {  
     digitalWrite(1,HIGH); 
     digitalWrite(7,HIGH); 
     digitalWrite(11,HIGH); 
     delay(10000); 
     digitalWrite(1, LOW); 
     digitalWrite(2, HIGH); 
     digitalWrite(3, LOW); 
     delay(2500); 
     digitalWrite(1,LOW); 
     digitalWrite(2,LOW); 
     digitalWrite(3,HIGH); 
     digitalWrite(7,LOW); 
     digitalWrite(4,HIGH); 
     digitalWrite(5,HIGH); 
     delay(5000); 
     digitalWrite(4,LOW); 
     digitalWrite(9,HIGH); 
     digitalWrite(11,LOW); 
     delay(10000); 
     digitalWrite(5, LOW); 
     digitalWrite(6, HIGH); 
     delay(2500); 
     digitalWrite(6, LOW); 
     digitalWrite(7, HIGH); 
     digitalWrite(8, HIGH); 
     delay(5000); 
     digitalWrite(8, LOW); 
     digitalWrite(9, LOW); 
     digitalWrite(10, HIGH); 
     delay(2500); 
     digitalWrite(10, LOW); 
     digitalWrite(11, HIGH); 
     digitalWrite(3, LOW); 
    } 

我們正在做一個「高級剎車燈」,所以 在代碼中,我需要添加它,以便在任何時候按下按鈕,在週期結束,它會運行代碼,我該怎麼做?製作按鈕保持變量,並在循環運行代碼結束

我已經嘗試了幾件事情,但每次它都無法正常工作,對此會有什麼簡單的解決方案?

回答

0

您是否嘗試過按下按鈕時設置標誌,然後在循環結束時檢查標誌是否已設置。

當標誌設置完成後,您知道該按鈕已被按下,以便您可以執行該代碼。像這樣的東西。

boolean buttonPressed = false; 

void setup() {  
    for (int i = 0; i < pinCount; thisPin++) { 
    pinMode(ledPins[thisPin], OUTPUT); 
    } 
} 

void loop() {  
     digitalWrite(1,HIGH); 
     digitalWrite(7,HIGH); 
     digitalWrite(11,HIGH); 
     delay(10000); 
     digitalWrite(1, LOW); 
     digitalWrite(2, HIGH); 
     digitalWrite(3, LOW); 
     delay(2500); 
     digitalWrite(1,LOW); 
     digitalWrite(2,LOW); 
     digitalWrite(3,HIGH); 
     digitalWrite(7,LOW); 
     digitalWrite(4,HIGH); 
     digitalWrite(5,HIGH); 
     delay(5000); 
     digitalWrite(4,LOW); 
     digitalWrite(9,HIGH); 
     digitalWrite(11,LOW); 
     delay(10000); 
     digitalWrite(5, LOW); 
     digitalWrite(6, HIGH); 
     delay(2500); 
     digitalWrite(6, LOW); 
     digitalWrite(7, HIGH); 
     digitalWrite(8, HIGH); 
     delay(5000); 
     digitalWrite(8, LOW); 
     digitalWrite(9, LOW); 
     digitalWrite(10, HIGH); 
     delay(2500); 
     digitalWrite(10, LOW); 
     digitalWrite(11, HIGH); 
     digitalWrite(3, LOW); 
     if(buttonPressed) 
     { 
      //code goes here 
     } 
    } 

所有你需要上述補充的是當按下的按鈕設置buttonPressed =真

+0

我沒有嘗試過,因爲如果按鈕之前的任何時候按下它不會註冊它, – Kyle1264

0

如果我理解正確的話,你想不管按鈕是否被按下的整個週期中運行,則該特殊代碼運行(或不運行),具體取決於循環中按鈕是否被按下。

如果是這樣,您可以編寫一個函數my_delay(msec)並在週期內調用它,而不是延遲。裏面my_delay,你將有一個循環測試多次,如果按下按鈕,直到請求的時間已經過去了:

static int lpressed = 0 ; 

void my_repeat(int msec) 
{ 
    long start = millis() ; 
    for(; millis()-start < msec;) 
    { 
     if (digitalRead(button_pin)==PRESSED) 
     lpressed = 1 ; 
    } 
} 

然後在週期結束時,你會簡單測試(可能重置)變量lpressed和採取相應的行動:

void loop() 
{ 
    // your unconditional code goes here 
    if (lpressed) 
    { 
     lpressed = 0 ; // reset so we can check again in next cycle 
     // your conditional code goes here 
    } 
} 
相關問題