2017-09-05 59 views
0

我有一個基本上有兩個功能的按鈕。其目標是:在for循環中按住按鈕3秒

  • 第一次按下會使LED閃爍15次。
  • 如果有人在LED閃爍時按住3秒以上的按鈕,應該停止並返回到初始狀態。

所以我檢查了Arduino的按鈕頁,並提出了這個代碼。問題是我無法正常停止閃爍。行Serial.println("DONE!!");從來沒有工作。
我應該在哪裏檢查按鈕是否被保持,是否應該使用中斷來結束for循環?

這裏是我的代碼:

int inPin = 2; // the pin number for input (for me a push button) 
int ledPin = 9; 
int current;   // Current state of the button 
// (LOW is pressed b/c I'm using the pullup resistors) 
long millis_held; // How long the button was held (milliseconds) 
long secs_held;  // How long the button was held (seconds) 
long prev_secs_held; // How long the button was held in the previous check 
byte previous = LOW; 
unsigned long firstTime; // how long since the button was first pressed 
long millis_held2; // How long the button was held (milliseconds) 
long secs_held2;  // How long the button was held (seconds) 
long prev_secs_held2; // How long the button was held in the previous check 
byte previous2 = LOW; 
unsigned long firstTime2; // how long since the button was first pressed 

void setup() { 
    Serial.begin(9600); // Use serial for debugging 
    pinMode(ledPin, OUTPUT); 
    digitalWrite(inPin, INPUT); // Turn on 20k pullup resistors to simplify switch input 
} 

void loop() { 
    current = digitalRead(inPin); 
    // if the button state changes to pressed, remember the start time 
    if (current == HIGH && previous == LOW && (millis() - firstTime) > 200) { 
    firstTime = millis(); 
    } 
    millis_held = (millis() - firstTime); 
    secs_held = millis_held/1000; 
    // This if statement is a basic debouncing tool, the button must be pushed for at least 
    // 100 milliseconds in a row for it to be considered as a push. 
    if (millis_held > 50) { 
    // check if the button was released since we last checked 
    if (current == LOW && previous == HIGH) { 
     // HERE YOU WOULD ADD VARIOUS ACTIONS AND TIMES FOR YOUR OWN CODE 
     // =============================================================================== 

     //////////////////////////////////////////////////////// Button pressed Blink 15 times. 
     if (secs_held <= 0) { 
     for (int i = 0; i < 15; i++) { 
      ledblink(1, 750, ledPin); 
      current = digitalRead(inPin); 
      if (current == HIGH && previous == LOW && (millis() - firstTime2) > 200) { 
      firstTime2 = millis(); 
      } 
      millis_held2 = (millis() - firstTime2); 
      secs_held2 = millis_held2/1000; 
      if (millis_held2 > 50) { 
      Serial.print("previousA "); 
      Serial.print(previous); 
      Serial.print(" currentA "); 
      Serial.println(current); 
      current = digitalRead(inPin); 
      if (current == HIGH && previous2 == HIGH) { 
       Serial.println("ALMOST! "); 
       Serial.println(secs_held2); 
       if (secs_held2 >= 2 && secs_held2 < 6) { 
       Serial.println("DONE!!"); 
       } 
      } 
      } 
      previous2 = current; 
      prev_secs_held2 = secs_held2; 
     } 
     } 
    } 
    } 
    previous = current; 
    prev_secs_held = secs_held; 
} 

void ledblink(int times, int lengthms, int pinnum) { 
    for (int x = 0; x < times; x++) { 
    digitalWrite(pinnum, HIGH); 
    delay (lengthms); 
    digitalWrite(pinnum, LOW); 
    delay(lengthms); 
    } 
} 
+0

delay()使連續輪詢輸入變得更加困難。嘗試僅在代碼中的某一點輪詢您的輸入也是一種很好的做法。 –

+0

您將需要使用「break();」聲明,當你注意到按鈕被保持太長時間。這將允許您立即退出for循環。可能將它添加到「Serial.println(」DONE !!「)下方的一行中;」如果我正確理解你的代碼。 –

+0

我的代碼中的問題是行「Serial.println(」DONE !!「);」從來沒有工作 – erondem

回答

0

這怎麼能不使用delay()來完成:

int inPin = 2; 
int ledPin = 9; 

long pressTimer; 

long blinkTimer; 
long blinkHalfDelay = 500; //milliseconds 
int blinkCount = 0; 
int blinkLimit = 15; 
bool blinkFlag = false; 
bool blinkLatch = true; 


bool currentBtnState = false; 
bool lastBtnState = false; 

void setup() { 
} 

void loop() { 
    lastBtnState = currentBtnState; 
    currentBtnState = digitalRead(inPin); 

    //on rising edge, start timers 
    if(currentBtnState==true && lastBtnState==false) 
    { 
     pressTimer = millis(); 
    } 

    //debounce activation 
    if(currentBtnState==true && !blinkFlag && millis()-pressTimer > 50) 
    { 
     blinkTimer = millis(); 
     blinkCount = 0; 
     blinkLatch = true; 
     blinkFlag = true; 

     //functional execution would go here 
    } 

    if(currentBtnState = false || (millis()-pressTimer>3000 && currentBtnState == true)) 
    { 
     blinkCount=blinkLimit; 
     blinkLatch = false; 
    } 

    if(blinkFlag == true) 
    { 
     if(millis()-blinkTimer > blinkHalfDelay) 
     { 
      blinkTimer = millis(); 
      if(blinkLatch) 
      { 
       digitalWrite(pinnum, HIGH); 
      } 
      else 
      { 
       digitalWrite(pinnum, LOW); 
       blinkCount+=1; 
      } 
     } 
     if(blinkCount>blinkLimit) 
     { 
      blinkFlag = false; 
      digitalWrite(pinnum, LOW); 
     } 
    } 
} 

PS:抱歉,沒有直接糾正你的代碼,但它是有點難以讀取...

+0

更具體一些,我在這裏查看了解如何使用arduino中的按鈕:https://playground.arduino.cc/Code/HoldButton。在這個頁面(如果你可以看一下),它完美的工作但是我不能成功的部分,我希望它應該結束閃爍,而我按住按鈕的時間超過3秒。在for循環中。我試圖檢查所有的過程,就像它在for循環中顯示的頁面一樣。但我沒有幫助 – erondem