Software

The software that we designed relies on communicating via xBee with the command center designed by our team members.  Our software first waits on a signal from the command center to signify the "House Select" stage of gameplay.  Once this signal is received, the player presses and holds a button to select their house, releasing the button when the LED of the house they want to select is on.  The software sees the button press and cycles the house LEDs until the button is released.  After this step, the software waits for a second signal from the command center to begin the "game play" mode.

During the gameplay mode, a player can press the "shield" button at any time.  When the software detects that the shield button has been pressed, it will turn all four house LEDs on for 5 seconds.  During this time, the badge software will not be searching for an IR signal.  When the "shield" button has not been pressed, the software is constantly checking if the IR receiver module has received a signal.  Once a signal has been received, our software turns on the vibrational mini-motor and cycles once through the four house LEDs, signifying that the player has been "hit."  Our software also sends a signal to the command center when an IR signal has been received, which tells the command center to increment a player's score by one.  Once a player has scored 10 points, the command center sends a code to the badge software to signal that the game has ended, and the badge will no longer be constantly checking for an IR signal.  The game can be restarted at any point by reseting the software.

//xBee codes that are sent among our local xBees.


//player 1 hit == 1

//player 2 hit == 2

//house select == 3

//game start == 4

//game over == 5

//Gryffindor 1 == G

//Gryffindor 2 == A

//Slytherin 1 == S

//Slytherin 2 == B

//Hufflepuff 1 == H

//Hufflepuff 2 == C

//Ravenclaw 1 == R

//Ravenclaw 2 == D

//Simple Spell 1 == w

//Simple Spell 2 == y

//Complex 1 == x

//Complex 2 == z


//Including libraries.

#include <Button.h>

#include <IRremote.h>

#include <SoftwareSerial.h>


//Declaring all local variables before setup() to ensure the proper scope.

const int houseBtnPin = 10;

Button houseSelect (houseBtnPin);

const int sheildButPin = 11;

Button sheild (sheildButPin);

const int Gryfindor = 7;

const int Slythrin = 4;

const int Hufflpuff = 5;

const int Ravenclaw = 6;

const int vibMotor = 8;

const int RECV_PIN = 9;


int gameState = 0;

int selectedHouse;

int i;

char xBeeVariable;


//Setting up the IR Receiver and preparing it to read in results.

IRrecv irrecv(RECV_PIN);

decode_results results;


//Setting up the software serial communication ports in pins 2 and 3 for the xBee to plug in to.

SoftwareSerial xBee(2, 3);


void setup() {

  Serial.begin(9600);


  //Setting up house LED that represents the house that a player selects.

  pinMode(Gryfindor, OUTPUT);

  pinMode(Slythrin, OUTPUT);

  pinMode(Hufflpuff, OUTPUT);

  pinMode(Ravenclaw, OUTPUT);

  pinMode(vibMotor, OUTPUT);


  //Initializing the IR receiver and the xBee.

  irrecv.enableIRIn();

  xBee.begin(9600);

}


void loop() {


  //Checking if xBee is receiving any transmission, if it is, the transmission is

  //read and stored as "xBeeVariable."

  if (xBee.available()) {

    xBeeVariable = xBee.read();

    Serial.println(xBeeVariable);

    Serial.println(gameState);


    //Series of "if" statements that check the received xBee transmission

    //and compares it to one of the variables listed at the top of the page.


    //Sets variable "gameState" to 3, indicating that the player can now

    //select their house of choice

    if (xBeeVariable == 51) {

      gameState = 3;

      xBeeVariable = 0;

      Serial.println(gameState);

    }


    //Sets "gameState" to 4, indicating that the game is now being played

    //(the badge will receive IR and send "HIT" to command center).

    if (xBeeVariable == 52) {

      gameState = 4;

      xBeeVariable = 0;

      Serial.println(gameState);

    }


    //Sets "gameState" to 5, indicating that gameplay has ended.

    if (xBeeVariable == 53) {

      gameState = 5;

      xBeeVariable = 0;

      Serial.println(gameState);

    }

  }


  //Code for selecting player house.

  //Used the Button Library by Andy Davidson to check if the house select

  //button is being held, if so, cycles through the house LEDs until the button is

  //released.  The HouseLED that is lit when the button is released is

  //the house that the player selects.

  int action1 = houseSelect.checkButtonAction();

  if (action1 == Button::HELD_CLICKED && gameState == 3) {

    for (i = 0; i < 5; i++) {


      //Turning on Gryfindor LED.

      digitalWrite(Gryfindor, HIGH);

      delay(1500);

      digitalWrite(Gryfindor, LOW);

      if (digitalRead(houseBtnPin) == 0) {

        selectedHouse = 1;

        Serial.println("You are a brave Gryfindor");

        xBee.print("G");

        gameState = 0;

        Serial.println(gameState);

        break;

      }


      //Turning on Slytherin LED.

      digitalWrite(Slythrin, HIGH);

      delay(1500);

      digitalWrite(Slythrin, LOW);

      if (digitalRead(houseBtnPin) == 0) {

        selectedHouse = 2;

        Serial.println("You are a cunning Slythrin");

        xBee.print("S");

        gameState = 0;

        break;

      }


      //Turning on Hufflpuff LED.

      digitalWrite(Hufflpuff, HIGH);

      delay(1500);

      digitalWrite(Hufflpuff, LOW);

      if (digitalRead(houseBtnPin) == 0) {

        selectedHouse = 3;

        Serial.println("You are a hardworking Hufflpuff");

        xBee.print("H");

        gameState = 0;

        break;

      }


      //Turning on Ravenclaw LED.

      digitalWrite(Ravenclaw, HIGH);

      delay(1500);

      digitalWrite(Ravenclaw, LOW);

      if (digitalRead(houseBtnPin) == 0) {

        selectedHouse = 4;

        Serial.println("You are an intelegent Ravenclaw");

        xBee.print("R");

        gameState = 0;

        break;

      }

    }

  } else {

  }


  //Turning on the player's selected house LED.

  HouseSelectLED();


  //Main game play

  //Double check that the shield is not activated and that the gameState

  //variable is == 4 (gameplay mode.)

  if (digitalRead(sheildButPin) == LOW && gameState == 4) {


    //Checks to see if the IR receiver is receiving an IR signal.  If it has,

    //then the if statement is executed, also indicating that the player has been

    //"hit."

    if (irrecv.decode(&results)) {

      LEDsOff();

      Serial.println("You have been hit");


      //sends a "2" to the command center, indicating that play two has been hit.

      //Insert "1" here for the other badge to indicate that player one has been hit.

      xBee.print(1);


      //Vibrational Mini-Motor is turned on to give the player a tactile notification

      //of when they have been hit.

      digitalWrite(vibMotor, HIGH);


      //When a player is hit, the House LEDs cycle around in a circular pattern

      //to show the other player when they have successfully hit their opponent.

      digitalWrite(Gryfindor, HIGH);

      delay(500);

      digitalWrite(Gryfindor, LOW);

      digitalWrite(Slythrin, HIGH);

      delay(500);

      digitalWrite(Slythrin, LOW);

      digitalWrite(Hufflpuff, HIGH);

      delay(500);

      digitalWrite(Hufflpuff, LOW);

      digitalWrite(Ravenclaw, HIGH);

      delay(500);

      digitalWrite(Ravenclaw, LOW);

      digitalWrite(vibMotor, LOW);


      //Telling the IR Receiver to continue to look for IR signal after it has

      //already detected a signal.

      irrecv.resume();


      //Turning back on the player's selected house LED.

      HouseSelectLED();

    }

    delay(100);


    //Used the Button Library by Andy Davidson to check the state of the shield

    //button on the badge.  If it is pressed, then all LEDs are turned on

    //for 5 seconds, and then off.  During this 5 seconds, the player cannot

    //get "hit" and the IR sensor will not trigger the "HIT" sequence

    //until the shield is turned off.

    int action2 = sheild.checkButtonAction();

    if (action2 == Button::CLICKED) {

      Serial.println("Sheild Activated");

      LEDsOn();

      delay(5000);

      LEDsOff();

      digitalWrite(vibMotor, HIGH);

      delay(500);

      digitalWrite(vibMotor, LOW);

      HouseSelectLED();

    }

  }


  //Checks the gameState variable, if it is 5, then it prints "Game Over" to

  //the Serial Output Monitor.

  if (gameState == 5) {

    Serial.println("Game Over");

  }


}


//A method that turns all LEDs on when a player activates the shield.

void LEDsOn() {

  digitalWrite(Ravenclaw, HIGH);

  digitalWrite(Hufflpuff, HIGH);

  digitalWrite(Slythrin, HIGH);

  digitalWrite(Gryfindor, HIGH);

}


//A method that turns all LEDs off after a shield activation.

void LEDsOff() {

  digitalWrite(Ravenclaw, LOW);

  digitalWrite(Hufflpuff, LOW);

  digitalWrite(Slythrin, LOW);

  digitalWrite(Gryfindor, LOW);

}


//A method that checks what the player's selected house is, and then turns on

//the corresponding house LED for the entirety of gameplay.

void HouseSelectLED() {

  if (selectedHouse == 1) {

    digitalWrite(Gryfindor, HIGH);

  }

  if (selectedHouse == 2) {

    digitalWrite(Slythrin, HIGH);

  }

  if (selectedHouse == 3) {

    digitalWrite(Hufflpuff, HIGH);

  }

  if (selectedHouse == 4) {

    digitalWrite(Ravenclaw, HIGH);

  }

}