Arduino中的游戏功能

时间:2018-12-06 23:39:58

标签: loops for-loop while-loop arduino do-while

我正在尝试使用具有10个LED(摩尔)和10个按钮(Whack)的Arduino Mega制作Whack-A-Mole游戏。电子设备已完成并经过测试,一切正常。它还连接了BLE无线电,提示董事会开始我计划做的三场比赛之一。

我遇到的问题是:

在我的主循环中,我从BLE监听来自应用程序的命令。应用程序发送“ 1”(串行传递)时,我调用了函数

playGameOne();

最主要的是,主循环中的所有循环都很好,但是当我跳到游戏功能时,它会运行一次并再次返回主循环。在游戏结束之前,如何保持用户在游戏功能中循环播放?哦-我并不想尝试使用任何中断,但是我想那是一种方法。

游戏功能的最新版本如下:

    //////////////////////////////////////////////
//////////////////  GAME 1  //////////////////


void gameOne(){


  //var int eTime;



  // inform player game is about to begin
  playCountdown();
  Serial.println("Game Begun");

  // initialize var to count time
  elapsedMillis timeElapsed;

  if(gameState < 1){

  gameOneOne();

      Serial.print("Game finished - Your time: ");
      Serial.println(timeElapsed);
  }
}




void gameOneOne(){
  while(digitalRead(btPin31) == LOW){

    // turn on LED45

    digitalWrite(ledPin45, HIGH);
    // wait for user to push corresponding button
    if(digitalRead(btPin31) == HIGH){
      // turn off the LED and jump to next LED - gameOneTwo()
      digitalWrite(ledPin45, LOW);

      gameOneTwo();

    }

  }  
}



void gameOneTwo(){
    while(digitalRead(btPin39) == LOW){

    // turn on LED53

    digitalWrite(ledPin53, HIGH);

    if(digitalRead(btPin39) == HIGH){
      digitalWrite(ledPin53, LOW);

      // Finish the game, set state to 1
      gameState = 1;

    }

  } 
}

不仅这行不通,而且我高度怀疑这是编码游戏的正确方法。实际上,理想的情况是拥有一个可以动态上传的游戏脚本。游戏脚本如下所示:

Header {name of game}
45 {pin of first LED}
500 {how long to keep it on, in ms}
49 {pin of the second LED}
500 {how long to keep it on, in ms}
... and so on
End of File

正确学习此知识的最佳资源是什么?

编辑

这是我的主循环

// included header files
#include <elapsedMillis.h>    // Measuring Elapsed Time Library


// Constant - Piezzo Speaker Pin
const int beepPin = 11;       // the number of the Piezo Spkr pin

// Constants - Button Pins
const int btPin30 = 30;
const int btPin31 = 31;
const int btPin32 = 32;
const int btPin33 = 33;
const int btPin34 = 34;
const int btPin35 = 35;
const int btPin36 = 36;
const int btPin37 = 37;
const int btPin38 = 38;
const int btPin39 = 39;

// Constants - LED Pins
const int ledPin53 = 53;
const int ledPin52 = 52;
const int ledPin51 = 51;
const int ledPin50 = 50;
const int ledPin49 = 49;
const int ledPin48 = 48;
const int ledPin47 = 47;
const int ledPin46 = 46;
const int ledPin45 = 45;
const int ledPin44 = 44;

//Variable for storing BLUETOOTH received data
char data = 0;             


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

int btPin30State = 0;
int btPin31State = 0;
int btPin32State = 0;
int btPin33State = 0;
int btPin34State = 0;
int btPin35State = 0;
int btPin36State = 0;
int btPin37State = 0;
int btPin38State = 0;
int btPin39State = 0;


// declare game state variable
int gameState = 0;



void setup() {

  // initialize Serial Comms for Debug (0) and BT(3) 
  Serial.begin(9600);     //Sets the buad for Serial (debug port)
  Serial3.begin(9600);   //Sets the baud for Serial3 data transmission                               

  // initialize all button pins as INPUTs
  for (int x = 30; x <= 39; x++){
    pinMode(x, INPUT);
  }

  // initialize LED pins as OUTPUTs:
  for (int i = 44; i <= 53; i++){
    pinMode(i, OUTPUT);
  }

  // lightup all LEDs
  for (int j = 44; j <= 53; j++){
    digitalWrite(j, HIGH);
    delay(100);
  }

  // prompt device is ready
  Serial.println("Device Ready");

}








void loop() {

  // Read Data from Serial3 -- This is to get initial game status/command from app/bluetooth
   if(Serial3.available() > 0)   {   // Send data only when you receive data:

    data = Serial3.read();



    // See what the command is
    if(data == '1') {
      Serial.write("Send it to GameOne");
      Serial.write("\n");
      gameOne();
    }

    // See what the command is
    if(data == '2') {
      Serial.write("Send it to GameTwo");
      Serial.write("\n");
      gameTwo();
    }

    if(data == '3') {
      Serial.write("Send it to GameThree");
      Serial.write("\n");
      gameThree();
    }

    if(data == '4') {
      Serial.write("Test");
      Serial.write("\n");
    }


   }



btPin30State = digitalRead(btPin30);
btPin31State = digitalRead(btPin31);
btPin32State = digitalRead(btPin32);
btPin33State = digitalRead(btPin33);
btPin34State = digitalRead(btPin34);
btPin35State = digitalRead(btPin35);
btPin36State = digitalRead(btPin36);
btPin37State = digitalRead(btPin37);
btPin38State = digitalRead(btPin38);
btPin39State = digitalRead(btPin39);


// check if the pushbutton is pressed. If it is, the buttonState is HIGH:


if (btPin30State == HIGH) {
  digitalWrite(ledPin44, HIGH);
  tone(11, 1000, 500);
  Serial.println("BTN 30 / LED 44");
  gameOne();
} else {
  digitalWrite(ledPin44, LOW);
}

if (btPin31State == HIGH) {
  digitalWrite(ledPin45, HIGH);
  Serial.println("BTN 31 / LED 45");
} else {
  digitalWrite(ledPin45, LOW);
}

if (btPin32State == HIGH) {
  digitalWrite(ledPin46, HIGH);
  Serial.println("BTN 32 / LED 46");
} else {
  digitalWrite(ledPin46, LOW);
} 

if (btPin33State == HIGH) {
  digitalWrite(ledPin47, HIGH);
  Serial.println("BTN 33 / LED 47");
} else {
  digitalWrite(ledPin47, LOW);
}

if (btPin34State == HIGH) {
  digitalWrite(ledPin48, HIGH);
  Serial.println("BTN 34 / LED 48");
} else {
  digitalWrite(ledPin48, LOW);
}

if (btPin35State == HIGH) {
  digitalWrite(ledPin49, HIGH);
  Serial.println("BTN 35 / LED 49");
} else {
  digitalWrite(ledPin49, LOW);
}

if (btPin36State == HIGH) {
  digitalWrite(ledPin50, HIGH);
  Serial.println("BTN 36 / LED 50");
} else {
  digitalWrite(ledPin50, LOW);
}

if (btPin37State == HIGH) {
  digitalWrite(ledPin51, HIGH);
  Serial.println("BTN 37 / LED 51");
} else {
  digitalWrite(ledPin51, LOW);
}

if (btPin38State == HIGH) {
  digitalWrite(ledPin52, HIGH);
  Serial.println("BTN 38 / LED 52");
} else {
  digitalWrite(ledPin52, LOW);
}

if (btPin39State == HIGH) {
  digitalWrite(ledPin53, HIGH);
  Serial.println("BTN 39 / LED 53");
} else {
  digitalWrite(ledPin53, LOW);
}




}

1 个答案:

答案 0 :(得分:1)

我认为可以将其构造成这样。

play_a_game:
    for (some_number_of_times)
    {
        show_a_mole_and_let_them_wackit();
    }

setup:
    set up pins
    say the game is starting

loop:
    do
    {
        play_a_game();
    }
    while(game ending button not pressed and less then 10 moles have died);

    say the game is over