//**********************************
//* *
//* Name: Caiden McCann *
//* Program: 2PlayerGame.ino *
//* Date: 2023-10-19 *
//* Desc: 2 player interactive maze*
//* *
//**********************************
const int speaker = A0;
const int p1GoHomeLED = 8;
const int p1PlayLED = 9;
const int p1WinnerLED = 10;
const int p1StartIn = 2;
const int p1WireIn = 3;
const int p1WinIn = 4;
const int p2GoHomeLED = 11;
const int p2PlayLED = 12;
const int p2WinnerLED = 13;
const int p2StartIn = 5;
const int p2WireIn = 6;
const int p2WinIn = 7;
int p1Start = 0;
int p2Start = 0;
int startTheGame = 0;
int theWinnerIs = 0;
int playerOnePlayingTheGame = 0;
int playerTwoPlayingTheGame = 0;
int p1AtStart = 0;
int p2AtStart = 0;
int p1Wire = 0;
int p2Wire = 0;
void setup() {
// put your setup code here, to run once:
pinMode(p1GoHomeLED, OUTPUT);
pinMode(p1PlayLED, OUTPUT);
pinMode(p1WinnerLED, OUTPUT);
pinMode(p1StartIn, INPUT_PULLUP);
pinMode(p1WireIn, INPUT_PULLUP);
pinMode(p1WinIn, INPUT_PULLUP);
pinMode(p2GoHomeLED, OUTPUT);
pinMode(p2PlayLED, OUTPUT);
pinMode(p2WinnerLED, OUTPUT);
pinMode(p2StartIn, INPUT_PULLUP);
pinMode(p2WireIn, INPUT_PULLUP);
pinMode(p2WinIn, INPUT_PULLUP);
randomSeed(analogRead(0));
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(p1StartIn) == 1){
p1AtStart = 1;
} //end if
if (digitalRead(p2StartIn) == 1){
p2AtStart = 1;
} //end if
fairstart(); //run preset function
if (digitalRead (p1AtStart) == 1){ //check if player one is at start
digitalWrite (p1GoHomeLED, LOW); //turn off player one's go home LED
digitalWrite (p1PlayLED, HIGH); //turn on player one's play LED
digitalWrite (p1WinnerLED, LOW); //turn off player one's winner LED
playerOnePlayingTheGame = 1; //player one is playing
} //end if
if (digitalRead (p1Wire) == 1){ //if player one touches the wire
digitalWrite (p1GoHomeLED, HIGH); //turn on player one's go home LED
digitalWrite (p1PlayLED, LOW); //turn off player one's play LED
digitalWrite (p1WinnerLED, LOW); //turn off player one's winner LED
playerOnePlayingTheGame = 0; //player one is not playing
} //end if
if (digitalRead (playerOnePlayingTheGame) == 1 && p1WinIn == 1){ //if player one wins
digitalWrite (p1GoHomeLED, LOW); //turn off player one's go home LED
digitalWrite (p1PlayLED, LOW); //turn off player one's play LED
digitalWrite (p1WinnerLED, HIGH); // turn on player one's winner LED
playerOnePlayingTheGame = 0; //player one is not playing
theWinnerIs = 1; //player one is the winner
} //end if
if (digitalRead (p2AtStart) == 1){ //check if player one is at start
digitalWrite (p2GoHomeLED, LOW); //turn off player two's go home LED
digitalWrite (p2PlayLED, HIGH); //turn on player two's play LED
digitalWrite (p2WinnerLED, LOW); //turn off player two's winner LED
playerTwoPlayingTheGame = 1; //player two is playing
} //end if
if (digitalRead (p2Wire) == 1){ //if player two touches the wire
digitalWrite (p2GoHomeLED, HIGH); //turn on player two's go home LED
digitalWrite (p2PlayLED, LOW); //turn off player two's play LED
digitalWrite (p2WinnerLED, LOW); //turn off player two's winner LED
playerTwoPlayingTheGame = 0; //player two is not playing
} //end if
if (digitalRead (playerTwoPlayingTheGame) == 1 && p2WinIn == 1){ //if player two wins
digitalWrite (p2GoHomeLED, LOW); //turn off player two's go home LED
digitalWrite (p2PlayLED, LOW); //turn off player two's play LED
digitalWrite (p2WinnerLED, HIGH); // turn on player two's winner LED
playerTwoPlayingTheGame = 0; //player two is not playing
theWinnerIs = 1; //player two is the winner
} //end if
}
//************************************FUNCTIONS BELOW************************************
void fairstart() {
while (startTheGame == 0) {
while (digitalRead(p1Start) != 0 || digitalRead(p2Start) != 0 ) {
if (digitalRead(p1Start) != 0) {
digitalWrite(p1PlayLED, HIGH);
} else {
digitalWrite(p1PlayLED,LOW);
}//end if()
if (digitalRead(p2Start) != 0) {
digitalWrite(p2PlayLED, HIGH);
} else {
digitalWrite(p2PlayLED,LOW);
}//end if()
}//end while
if (digitalRead (p1AtStart == 1) && p2AtStart == 1){
tone (A0,200,100);
tone (A0,250,100);
tone (A0,300,100);
delay ( (random (1,5) * 1000) );
tone (A0,350,250);
} //end if
if (digitalRead(p1AtStart == 0) && p2AtStart == 0){
startTheGame = 1;
} else {
startTheGame = 0;
} //end if()
}//end 2nd while()
}//end fairstart()