top of page

Software

This software sends game mode triggers that changes game states from inactive, to game initialization, to game play, to game over. It tracks signals from the badges and wands to update the scoreboard when players are hit. When the game is over, it plays the HP theme on a piezo buzzer.

#include <Adafruit_LiquidCrystal.h>

#include <SoftwareSerial.h>

#include <string.h>

#include <stdio.h>


Adafruit_LiquidCrystal lcd(0);

SoftwareSerial xBee(2,3);

const int buzzer = 4;

const int g1LED = 5;

const int h1LED = 6;

const int r1LED = 7;

const int s1LED = 8;

const int g2LED = 9;

const int h2LED = 10;

const int r2LED = 11;

const int s2LED = 12;


//These trigger house selection mode and gameplay mode in the remotes

int housePickTrigger = 3;

int gameStartTrigger = 4;


bool inGame = false;

int score1;

int score2;

char p1house = '0';

char p2house = '0';


//global variable from harry potter song program

int tempo = 400;



void setup() {

 Serial.begin(9600);

 Serial.println("Setting up game...");


 pinMode(buzzer, OUTPUT);

 pinMode(g1LED, OUTPUT);

 pinMode(h1LED, OUTPUT);

 pinMode(r1LED, OUTPUT);

 pinMode(s1LED, OUTPUT);

 pinMode(g2LED, OUTPUT);

 pinMode(h2LED, OUTPUT);

 pinMode(r2LED, OUTPUT);

 pinMode(s2LED, OUTPUT);

 lcd.begin(16,2);

 xBee.begin(9600);

 initGame();

}


void loop() {

 int lastSpell1 = 0;

 int lastSpell2 = 0;

 int score = 0;


 //The game loop will continue until one player gets to 10 points

 while (inGame == true) {

   if (xBee.available()) {

     score = xBee.read();

   }


   //119 - 122 stores which type of spell was cast by the each player

   //1 is a weak spell (1pt), 2 is a strong spell (2pt)

   if (score ==  119) {

     lastSpell1 = 1;

   }

   if (score == 120) {

     lastSpell1 = 2;

   }

   if (score == 121) {

     lastSpell2 = 1;

   }

   if (score == 122) {

     lastSpell2 = 2;

   }


   //49 - 50 stores which player has scored

   //49 is player 1, 50 is player 2

   if (score == 49) {

     addPoints1(lastSpell1);

     delay(1000);

   }

   if (score == 50) {

     addPoints2(lastSpell2);

     delay(1000);

   }

   

   lcd.setCursor(0,0);

   score = 0;

   

   //End game when either player reaches 10 points

   if (score1 >= 10 || score2 >= 10) {

     endGame();

   }

 }

}


//Add points to P1's score and updates LCD

int addPoints1(int lastSpell1) {

 Serial.println("Player 1 scored!");

 if (lastSpell1 == 2) {

   score1 = score1 + 2;

 }

 else {

   score1++;

 }

 lcd.print("Player 1: "); lcd.print(score1); lcd.setCursor(0,1);

 lcd.print("Player 2: "); lcd.print(score2); lcd.setCursor(0,0);

}


//Add points to P2's score and updates LCD

int addPoints2(int lastSpell2) {

 Serial.println("Player 2 scored!");

 if (lastSpell2 == 2) {

   score2 = score2 + 2;

 }

 else {

   score2++;

 }

 lcd.print("Player 1: "); lcd.print(score1); lcd.setCursor(0,1);

 lcd.print("Player 2: "); lcd.print(score2); lcd.setCursor(0,0);

}


//Wait for house selection from both wands at the same time to start game

void initGame() {

 Serial.println("Awaiting house selection...");

 xBee.print(housePickTrigger);

 while (inGame == false) {

   byte house;

   if (xBee.available()) {

     house = xBee.read();

   }

   else {

     house = 0;

   }

   if (house == 71 || house == 103) {

     p1house = 'g';

     digitalWrite(g1LED,HIGH);

     Serial.println("Player 1 has chosen Gryffindor!");

   }

   if (house == 83 || house == 115) {

     p1house = 's';

     digitalWrite(s1LED,HIGH);

     Serial.println("Player 1 has chosen Slytherin!");

   }

   if (house == 72 || house == 104) {

     p1house = 'h';

     digitalWrite(h1LED,HIGH);

     Serial.println("Player 1 has chosen Hufflepuff!");

   }

   if (house == 82 || house == 114) {

     p1house = 'r';

     digitalWrite(r1LED,HIGH);

     Serial.println("Player 1 has chosen Ravenclaw!");

   }

   if (house == 65 || house == 97) {

     p2house = 'g';

     digitalWrite(g2LED,HIGH);

     Serial.println("Player 2 has chosen Gryffindor!");

   }

   if (house == 66 || house == 98) {

     p2house = 's';

     digitalWrite(s2LED,HIGH);

     Serial.println("Player 2 has chosen Slytherin!");

   }

   if (house == 67 || house == 99) {

     p2house = 'h';

     digitalWrite(h2LED,HIGH);

     Serial.println("Player 1 has chosen Hufflepuff!");

   }

   if (house == 68 || house == 100) {

     p2house = 'r';

     digitalWrite(r2LED,HIGH);

     Serial.println("Player 2 has chosen Ravenclaw!");

   }

   if (p1house != '0' & p2house != '0') {

     inGame = true;

     Serial.println("Game start!");

     xBee.print(gameStartTrigger);

   }

 }

 Serial.println("Looking for points...");

}


//play HP theme

void endGame() {

 Serial.println("Game end!");

 sing();

 score1 = 0;

 score2 = 0;  

//  playAgain();

}


//Non-functional play again feature

/*

void playAgain() {

 Serial.println("Play again?");

 int p1again = digitalRead(teamP1);

 int p2again = digitalRead(teamP2);

 if (p1again == 0 & p2again == 0) {

     score1 = 0;

     score2 = 0;

     inGame = false;

     initGame();

 }

}

*/



//HP THEME CODE////////////////////////////////////////////

//code from http://www.instructables.com/id/Arduino-Harry-Potter-Theme-Song/


struct MusicStruct {

 int A = 550;

 int As = 582;

 int B = 617;

 int C = 654;

 int Cs = 693;

 int D = 734;

 int Ds = 777;

 int E = 824;

 int F = 873;

 int Fs = 925;

 int G = 980;

 int Gs = 1003;

 int A2 = 1100;

 int A2s = 1165;

 int B2 = 1234;

 int C3 = 1308;

 int C3s = 1385;

 int D3 = 1555;

}Music;


struct LengthStruct {

 float half = 0.5;

 float one = 1.0;

 float one_half = 1.5;

 float two = 2.0;

 float two_half = 2.5;

}Length;


void setTone(int buzzer, int note, int duration) {

 tone(buzzer, note, duration);

 delay(duration);

 noTone(buzzer);

}


void sing() {

 setTone(buzzer, Music.B, tempo * Length.one);

 setTone(buzzer, Music.E, tempo * Length.one_half);

 setTone(buzzer, Music.G, tempo * Length.half);

 setTone(buzzer, Music.Fs, tempo * Length.one);

 setTone(buzzer, Music.E, tempo * Length.two);

 setTone(buzzer, Music.B2, tempo * Length.one);

 setTone(buzzer, Music.A2, tempo * Length.two_half);

 setTone(buzzer, Music.Fs, tempo * Length.two_half);


 setTone(buzzer, Music.E, tempo * Length.one_half);

 setTone(buzzer, Music.G, tempo * Length.half);

 setTone(buzzer, Music.Fs, tempo * Length.one);

 setTone(buzzer, Music.Ds, tempo * Length.two);

 setTone(buzzer, Music.Fs, tempo * Length.one);

 setTone(buzzer, Music.B, tempo * Length.two_half);

 delay(10000);

}

Software: Homepage_about
bottom of page