Chapter 7. Subtask 5: Program the Game

Now that you’ve got the mini breadboard circuit wired up properly and connected to the MintDuino, all that’s left is to upload the sketch that will allow two players to see who is the fastest button pusher on the planet.

The game will run as follows:

  1. Turn on the MintDuino—use either a 9V battery or USB power via the FTDI Adapter.

  2. Both player LEDs will light up and stay lit.

  3. Press the Player 2 button to start the game; both player LEDs will turn off.

  4. The Game Light (center LED on the mini breadboard) will blink three times.

  5. After the Game Light blinks three times, a random amount of time will pass before it lights again.

  6. When the Game Light blinks again, each player will try to push his or her button before the other player.

  7. The fastest player’s LED will light up to indicate the winner.

  8. Pressing a button before the delay is over will not light an LED.

  9. Press the Player 2 button to start a new game.

Figure 7-1 shows the diagram of the final circuit (all resistors are 100 ohm). The diagram was created using Fritzing, an open source tool for designing interactive electronics. See http://fritzing.org.

Circuit diagram for the MintDuino Reflex Game
Figure 7-1. Circuit diagram for the MintDuino Reflex Game

The Final Sketch

You can download the program for the MintDuino Reflex Game at http://examples.oreilly.com/0636920020882 or simply enter the code below into the Arduino IDE:

// MintDuino NoteBook 1 – Reflex Game version 7.0
int ledGameLight = 7;  //Digital Pin 7 for LED anode connection
int ledPlayer1 = 5;    //Digital Pin 1 for Player 1 LED
int ledPlayer2 = 6;    //Digital Pin 2 for Player 2 LED
int button1 = 4;       //Digital Pin 4 for Player 1 button
int button2 = 3;       //Digital Pin 5 for Player 2 button
int state2 = 0;
int state1 = 0;
int ledWait = 5000;    //Wait time will be a minimum of 5 seconds

void setup() {
  pinMode(ledGameLight, OUTPUT);
  pinMode(ledPlayer1, OUTPUT);
  pinMode(ledPlayer2, OUTPUT);  pinMode(button1, INPUT); // is this needed?
  pinMode(button2, INPUT); // is this needed?
  randomSeed(analogRead(1)); //use Analog Pin 1 to generate a random number
}

void loop(){
  state2 = digitalRead(button2);    // Read the state of the pushbutton value

  if (state2 == HIGH) {     // Check if the pushbutton is pressed
    digitalWrite(ledPlayer1, LOW);    // Turn LED off:
    digitalWrite(ledPlayer2, LOW);    // Turn LED off:
    delay (2000);
    beginGame();

  }
  else {
    // turn LED off:
    digitalWrite(ledPlayer1, HIGH);    // Turn LED on
    digitalWrite(ledPlayer2, HIGH);    // Turn LED on
  }
}
void beginGame(){     // Only called when the button state is HIGH (pressed)

  // three fast blinks
  for (int count = 0; count < 3; count++) {
    digitalWrite(ledGameLight, HIGH);
    delay(500);
    digitalWrite(ledGameLight, LOW);
    delay(500);
  }

  // Now generate a wait time before Game Light turns on
  ledWait = 5000;  //reset value to minimum of 5 seconds
  ledWait = ledWait + random(5000); // add random value 0-5000 milliseconds

  //Turn on Game Light after Wait Time expires
  delay(ledWait);
  digitalWrite(ledGameLight, HIGH);
  delay(100);
  digitalWrite(ledGameLight, LOW);

  int gameOver = 0;

  while (!gameOver) {
    //determine which player button was pressed first
    int button1State = digitalRead(button1);
    int button2State = digitalRead(button2);

    if (button1State != button2State) {

      delay(5); // pause, then take another reading
      if (button1State == HIGH && digitalRead(button1) == HIGH) {
        Player1Win();
        gameOver = 1;
      }

      if (button2State == HIGH && digitalRead(button2) == HIGH) {
        Player2Win();
        gameOver = 1;
      }
    }
    else {
      if (button1State == HIGH && button2State == HIGH) {
        // tie
        itsATie();
        gameOver = 1;
      }
    }

  }

  // Start game over

}

// Tie
void itsATie() {

  for (int i = 0; i < 3; i++) {
    digitalWrite(ledPlayer1, HIGH);
    digitalWrite(ledPlayer2, HIGH);
    delay(250);
    digitalWrite(ledPlayer1, LOW);
    digitalWrite(ledPlayer2, LOW);
    delay(250);
  }
}

// Player 1 won, light his/her LED
//
void Player1Win() {
  digitalWrite(ledPlayer1, HIGH);
  digitalWrite(ledPlayer2, LOW);
  delay(4000);
  digitalWrite(ledPlayer1, LOW);
}

// Player 2 won, light his/her LED
//
void Player2Win() {
  digitalWrite(ledPlayer2, HIGH);
  digitalWrite(ledPlayer1, LOW);
  delay(4000);
  digitalWrite(ledPlayer2, LOW);
}

After uploading the sketch to the MintDuino, run the program. When the game first starts, the two Player LEDs will light up and stay lit until Player 2 presses his button. The Player LEDs will turn off and the Game Light will blink three times. After the third blink, both players will wait until the Game Light blinks a fourth time and then try to be the first player to press their button. The player that wins will have her LED light up for four seconds before turning off. Press the Player 2 button to play another game.

Note

If you were able to successfully run Subtask 4, then any errors you encounter are most likely in the sketch for the game. Make certain you’ve uploaded the correct sketch, and check that all of your jumper wires are inserted properly in the mini breadboard and the MintDuino. Check that both pushbuttons are pushed securely into the mini breadboard and make certain that all LEDs are wired up correctly with respect to their anode and cathode leads.

Conclusion

There’s no arguing that the MintDuino Reflex Game is relatively simple in terms of gameplay. But think about what you’ve done for a moment—with two pushbuttons, three LEDs, three resistors, and a handful of jumper wires, you transformed the MintDuino and mini breadboard into a functional game. It’s not complex and it’s certainly not pretty to look at, but it works!

Hopefully you’re starting to see the power that resides in that small MintDuino tin and maybe pondering some of your own special projects. If you’re not yet ready to leave the MintDuino Reflex Game and want to dive a little deeper, here are some suggestions on ways to improve the game:

Turn it into a 3- or 4-player game

It might require a slightly larger breadboard, but since you already know how a 2-player game works, it’s not a big jump to program it for additional pushbuttons and LEDs.

Keep score

Consider adding to the sketch the required code to cause the MintDuino’s status light to blink the Player 1 score, pause three seconds, and then blink the Player 2 score. Or you could add another LED for each player that flashes their score before a new game begins.

Penalize early button pushing

Modify the sketch to check to see if a player pushes his or her button before the Game Light turns on. If the button is pressed before the Game Light blinks, that player automatically loses and the other player’s LED turns on.

Move the game to a project box

With a perf-board and a project box, you could easily give the game a permanent home. With a project box, you could even add an LCD screen to display the score or maybe a speaker that plays a series of beeps instead of lighting the Game LED.

Congratulations on completing the MintDuino Reflex Game! If you’re looking for more projects for your MintDuino, be sure to check out the Make: Projects website at www.makeprojects.com and look over all the Arduino projects for something that catches your eye. Your MintDuino might not look like the Arduinos you see online, but it’s got the same functionality and can easily be substituted. You can also find many more projects on the various Arduino-related websites, including the official Arduino site at www.arduino.org, the community-run www.arduino.cc, and instructables.com.

Special thanks to Brian Jepson and Will Price for their assistance with the MintDuino Reflex Game sketch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset