行动正在进行三次

时间:2015-04-24 21:46:43

标签: button arduino monitor

我正在创建一个播放Rock Paper Scissors的Arduino项目。 我有一部分代码读取按钮的值,每当我只点击一个按钮时,它会输出三次(查看代码和串行监视器的图像以了解我的意思,我无法解释它)

这是我的代码:

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin1 = A0;     // the number of the pushbutton pin
const int buttonPin2 = A1;
const int buttonPin3 = A2;
const int ledPin =  12;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0; 
int buttonState3 = 0; 

char * choices[3] = {"Rock", "Paper", "Scissors"};
char * finalResult[3] = {"You Lost", "You Won!", "It's a Tie"};

byte Human = 0, Computer = 0, FR = 0;

char result[25];

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  digitalWrite(ledPin,LOW);
  Serial.begin(9600);
  unsigned long R;
  randomSeed(R);
}

void loop(){
  int choice = random(1,4);
  int pick;
  // read the state of the pushbutton value:


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  Serial.println("Entering Reading Loop...");
  while(true)
  {
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
    buttonState3 = digitalRead(buttonPin3);
    if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW) {     
      // turn LED on:    
      Throw('1');

    } else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW) {     
      // turn LED on:    
      pick = 2; //Paper
      Throw('2');

    }else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH) {     
      // turn LED on:    
      pick = 3; //Scissor
      Throw('3');

    }

  }
}

void Throw(char H)
{
  bool thrown = false;
  H -= '0'; //convert ascii to decimal
  H -= 1; // instead of  1,2,3, H is now 0,1,2

  byte C = random(0, 3);

  sprintf(result, "The Computer chose: %s, You chose: %s", choices[C], choices[H]);
  Serial.println(result);

  if ( C == H)
    Serial.println(F("Its a TIE"));
  else
  {
    switch (C)
    {
      case 0:
        switch (H)
        {
          case 1:
            Serial.println(F("Paper wraps Rock, You WIN!"));
            Human++;
            break;

          case 2:
            Serial.println(F("Rock crushes Scissors, You LOSE!"));
            Computer++;
            break;
        }
        break;

      case 1:
        switch (H)
        {
          case 0:
            Serial.println(F("Paper wraps Rock, You LOSE!"));
            Computer++;
            break;

          case 2:
            Serial.println(F("Scissors cuts Paper, You WIN!"));
            Human++;
            break;
        }
        break;

      case 2:
        switch (H)
        {
          case 0:
            Serial.println(F("Rock crushes Scissors, You WIN!"));
            Human++;
            break;

          case 1:
            Serial.println(F("Scissors cuts Paper, You LOSE!"));
            Computer++;
            break;
        }
        break;
    }
  }
}

当我只点击一个按钮(摇滚按钮)

时,这就是我在串行监视器上得到的内容

http://gyazo.com/ceb5c8329993339368cf5d52181ed4d7

正如您所看到的,它为按钮选择了正确的选项,但它调用了throw函数3次。

http://playground.arduino.cc/Main/RockPaperScissors

3 个答案:

答案 0 :(得分:0)

从Arduino文档中,他们建议使用Debounce策略来处理输入。

  

如果没有去抖动,按下按钮一次就会在代码中显示为   多次按下。

http://www.arduino.cc/en/Tutorial/Debounce

这是另一个说明去抖技术的例子:

http://danthompsonsblog.blogspot.com/2011/12/arduino-push-button-onoff-example.html

答案 1 :(得分:0)

arduino内置了上拉电阻,可以滤除按下按钮时产生的不稳定电信号。请参阅INPUT_PULLUP here

部分

答案 2 :(得分:0)

我不知道究竟是什么问题但是,我在throw函数中添加了一个延迟,它停止输出串行打印语句3次!

这是我更新的功能:

void Throw(char H)
{
  bool thrown = false;
  H -= '0'; //convert ascii to decimal
  H -= 1; // instead of  1,2,3, H is now 0,1,2

  byte C = random(0, 3);

  sprintf(result, "The Computer chose: %s, You chose: %s", choices[C], choices[H]);
  Serial.println(result);
  delay(500);


  if ( C == H)
    Serial.println(F("Its a TIE"));
  else
  {
    switch (C)
    {
      case 0:
        switch (H)
        {
          case 1:
            Serial.println(F("Paper wraps Rock, You WIN!"));

            Human++;
            break;
            return;

          case 2:
            Serial.println(F("Rock crushes Scissors, You LOSE!"));

            Computer++;
            break;
        }
        break;

      case 1:
        switch (H)
        {
          case 0:
            Serial.println(F("Paper wraps Rock, You LOSE!"));

            Computer++;
            break;

          case 2:
            Serial.println(F("Scissors cuts Paper, You WIN!"));

            Human++;
            break;
        }
        break;

      case 2:
        switch (H)
        {
          case 0:
            Serial.println(F("Rock crushes Scissors, You WIN!"));

            Human++;
            break;

          case 1:
            Serial.println(F("Scissors cuts Paper, You LOSE!"));

            Computer++;
            break;
        }
        break;
    }
  }
}