Arduino记忆游戏

时间:2014-11-27 07:56:49

标签: arduino arduino-uno

这个游戏有4个LED和4个按钮。游戏正在逐渐转向那些LED开启和关闭。

玩家应该能够在他看到一个开启时按下右按钮。

LED应该以接收速度打开和关闭,因此玩家的反应时间越来越短

我有这段代码,但我知道如何添加更多LED和按钮。

const int BUTTON1 = A0; 
const int BUTTON2 = A1;
const int BUTTON3 = A2; 
const int BUTTON4 = A3;
int LED1 = 2; 
int LED2 = 3; 
int LED3 = 4; 
int LED4 = 5; 
int ran;
int right = 0;
int ledOrder[9];
int guessOrder[9];

void setup() 
{
  Serial.begin(9600); 
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);
  pinMode(LED4,OUTPUT);
  pinMode(BUTTON1,INPUT);
  pinMode(BUTTON2,INPUT);
  pinMode(BUTTON3,INPUT);
  pinMode(BUTTON4,INPUT);
}
void randomLed() {
  for (int i = 0; i < 9; i++) { 
    ran = random(1,20);
    if (ran < 11) {
      digitalWrite(LED1,HIGH);
      delay(500);
      digitalWrite(LED1,LOW);
      ledOrder[i] = 1;

    }
    else {
      digitalWrite(LED2,HIGH);
      delay(500);
      digitalWrite(LED2,LOW);
      ledOrder[i] = 2;
    }
    delay(500);
  }
}
void btnClick() {
  int ans = 0;
  while (ans < 9) { 
    if (digitalRead(BUTTON1) == HIGH) {
      guessOrder[ans] = 1;
      ans++;
      while (digitalRead(BUTTON1) == HIGH) {
      }
    }
    else if (digitalRead(BUTTON2) == HIGH) {
      guessOrder[ans] = 2;
      ans++;
      while (digitalRead(BUTTON2) == HIGH) {
      }
    }
  }
}
void loop() {
    Serial.print("Press button1 to start \n");
    while (digitalRead(BUTTON1) == LOW) {
    }
    randomLed();
    btnClick();
    for (int i = 0; i < 9; i = i + 1) {
      Serial.print("Guess: ");
      Serial.print(guessOrder[i]);
      Serial.print(" Answer: ");
      Serial.print(ledOrder[i]);
      if (guessOrder[i] == ledOrder[i]) {
        Serial.print(" Right");
        right++;
      } else {
        Serial.print(" Wrong");
      }
      Serial.print("\n ");
    }
    Serial.print(right);
    Serial.print("/9\n");
    delay(2000);

}

1 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题,你想在每次用户获得前一个游戏时增加LED闪烁率(当LED亮起时)。如果这是真的你应该尝试将循环放入/如果用户得到前一个(游戏),它将通过简单的增量(I ++;)增加闪烁率。评论,如果你觉得这已经足够澄清或者你要求别的东西