如何在我的Arduino游戏中设置读取输入的ceratin时间间隔?

时间:2015-12-16 09:55:52

标签: arduino

我和我的Arduino Uno一起玩游戏。 面包板上的4个LED显示随机二进制数。 玩家需要按下按钮才能输入他看到的号码 (例如,如果指示灯为0011,则按下按钮3次)。 如果他猜对了,他就赢了(另一个领先的眨眼)。如果没有,他就会失败(领导并继续前进)。

但是如果玩家没有按下按钮两秒钟,我希望玩家自动失败。

但我真的是个初学者所以请耐心等待。下面我会发布到目前为止我所得到的内容。但它并没有完全奏效。当我打开它,没有我按下按钮时,它直接进入"你赢了"和下一轮。我做错了什么?

int led1 = 9;
int led2 = 6;
int led3 = 5;
int led4 = 3;

int ledResult = 13; //will blink when you won, stay on when you lost

int buttonPin = 2;
int val = 0; // variable for reading the pin status
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;


long interval = 2000; 

long randomNumber;


void setup() {
  Serial.begin(9600); //starts serial communication

  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
  pinMode (led4, OUTPUT);

  pinMode (ledResult, OUTPUT);

  pinMode (buttonPin, INPUT);

  randomSeed(analogRead(A0)); //the pin is unconnected so it'll return something random (0-1023)


}

void loop() {

randomNumber = random(1, 16);

  Serial.println("Random Numbers sequence"); //just for self-check
  Serial.println(randomNumber);

  if (randomNumber >= 8)
  {
    digitalWrite (led1, HIGH);
    randomNumber - 8;
  }
  else
  {
    digitalWrite (led1, LOW);
  }

  if (randomNumber >= 4)
  {
    digitalWrite (led2, HIGH);
    randomNumber - 4;
  }
  else
  {
    digitalWrite (led3, LOW);
  }

  if (randomNumber >= 2)
  {
    digitalWrite (led4, HIGH);
    randomNumber - 2;
  }
  else
  {
    digitalWrite (led1, LOW);
  }

  if (randomNumber = 1)
  {
    digitalWrite (led2, HIGH);
  }
  else
  {
    digitalWrite (led1, LOW);
  }



unsigned long currentMillis = millis();

if (currentMillis > interval) {
    Serial.println("You lost.");
    digitalWrite(ledResult, HIGH);

}else{

  //READ BUTTON STATE
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH)
    {
      buttonPushCounter++;
      Serial.println("Button push counter:");
      Serial.println(buttonPushCounter);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  if (buttonPushCounter = randomNumber) {
    Serial.println("You won!");
    digitalWrite(ledResult, HIGH);
    delay(700);
    digitalWrite(ledResult, LOW);
    delay(700);
  }
  else
  {
    Serial.println("You lost.");
    digitalWrite(ledResult, HIGH);
  }
}
}

1 个答案:

答案 0 :(得分:0)

基本上,您希望有一个主循环来探测/轮询用户输入,如果用户没有在探测中输入,那么请检查当前millis时间你打电话给它开始。这将为您提供自未检测到输入以来经过的时间。如果经过的时间大于阈值,则跳出循环并检查超时标志以查看是否继续按钮验证代码或分支到"丢失"代码。

没有写完整篇文章,这里有一个C伪代码模拟了这个想法(评论解释):

void loop() {
    while (is_running) { // main loop
        unsigned long start = millis(); // get starting probe time
        bool timeout = false;
        while ((buttonState = probe_input()) == no_input_val) {
            /* probe_input function returned no input,
            so check for timeout. If the probe_input function
            did return a 'value', then this loop won't iterate
            and you can check the buttonState below */
            if ((millis() - start) >= interval) {
                /* millis is monotonic time, so calling it, then
                subtracting the value from the start value will give
                us elapsed time since user did not give input. */ 
                timeout = true;
                break;
            }
        }
        if (!timeout) {
            /* button state/level code */
        } else {
            // lose code
        }
    }
}

同样,这只是伪代码并且不会编译,因为您需要实现probe_input函数来轮询输入,但它更多地用于说明您和#39; d需要做的是让你朝着你想要的方向前进。

我希望可以提供帮助。