无法在arduino中读取按钮

时间:2018-01-21 06:09:28

标签: button arduino

我在arduino的循环中使用do while条件 我把while条件放在里面继续循环,而我的问题是当我按下按钮它没有做任何事情时,我试图拆分这个按钮代码并且它工作正常,它赢了;如果我放在这段代码中。

void loop() 
{
  do {
    successRead = getID();
    if (programMode) {
      cycleLeds();              // Program Mode cycles through Red Green Blue waiting to read a new card
    }
    else {
      normalModeOn();     // Normal mode, blue Power LED is on, all others are off
    }
  }

  while (!successRead);
  {   
  buttonState = digitalRead(button); //Read the button --> main problem

  if(buttonState != lastButtonState)
  {
    if(buttonState == LOW){
      buttonPushCounter ++;
      Serial.println("High");
      Serial.print("Number of button pushes : ");
      Serial.println(buttonPushCounter);
      }
    else{
      Serial.println("Low");
      Serial.println(buttonPushCounter);
      }
      delay(100);
  }  
  lastButtonState = buttonState;

  if (programMode) {
    if ( buttonPushCounter % 2 == 0 ) { //When in program mode check First If master card scanned again to exit program mode
      Serial.println(F("Master Card Scanned"));
      Serial.println(F("Exiting Program Mode"));
      Serial.println(F("-----------------------------"));
      programMode = false;
      return;
    }
    else {
      if (findID(readCard) ) { // If scanned card is known delete it
        Serial.println(F("I know this PICC, removing..."));
        deleteID(readCard);
        Serial.println("-----------------------------");
        Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      }
      else {                    // If scanned card is not known add it
        Serial.println(F("I do not know this PICC, adding..."));
        writeID(readCard);
        Serial.println(F("-----------------------------"));
        Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      }
    }
  } 

1 个答案:

答案 0 :(得分:0)

设置电容器以避免触摸按钮。enter image description here