//*********************************************************
//* *
//* Name: Logan Howlett *
//* Program Name: 2PlayerGame.ino *
//* Date: 2023-04-11 *
//* Program Desc: 2 player wire game were the players *
//* have to get their ring to the other *
//* side without touching the wire the *
//* player to do so wins but if any *
//* player touchs the wire they have to *
//* restart at the beginning *
//* *
//*********************************************************
const int speaker = A0;
const int P1RLed = 8; //set the Payer 1 red LED to pin 8
const int P1GLed = 9; //set the Payer 1 red LED to pin 9
const int P1YLed = 10; //set the Payer 1 red LED to pin 10
const int P1ButtonSTART = 2; //set the Payer START button to pin 2
const int P1ButtonWIRE = 3; //set the Payer WIRE button to pin 3
const int P1ButtonWIN = 4; //set the Payer WIN button to pin 4
const int P2RLed = 11; //set the Payer 2 red LED to pin 11
const int P2GLed = 12; //set the Payer 2 red LED to pin 12
const int P2YLed = 13; //set the Payer 2 red LED to pin 13
const int P2ButtonSTART = 5; //set the Payer START button to pin 5
const int P2ButtonWIRE = 6; //set the Payer WIRE button to pin 6
const int P2ButtonWIN = 7; //set the Payer WIN button to pin 7
int startTheGame = 0; //set the variable to start the game to 0
int P1playGame = 0; //set variable for Player 1 if playing game or not
int P2playGame = 0; //set variable for Player 2 if playing game or not
int theWinner = 0; //set variable for who is the winner
int flashLed = 0; //set variable for the Led to flash
int showLed = 0; //set variable for the random light show
void setup() {
randomSeed(analogRead(0));
DDRB = B111111; // 1 = Output 0 = Input
//Speacker's pin out and tones
pinMode(speaker, OUTPUT); //set speaker to a OUTPUT
tone(speaker, 500, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 700, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 250, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 1000, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 250, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 700, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 500, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
//Player 1's pin outs
pinMode(P1RLed, OUTPUT); //set P1RLed to an OUTPUT
pinMode(P1GLed, OUTPUT); //set P1GLed to an OUTPUT
pinMode(P1YLed, OUTPUT); //set P1YLed to an OUTPUT
pinMode(P1ButtonSTART, INPUT_PULLUP); //set P1ButtonSTART to an INPUT_PULLUP
pinMode(P1ButtonWIRE, INPUT_PULLUP); //set P1ButtonWIRE to an INPUT_PULLUP
pinMode(P1ButtonWIN, INPUT_PULLUP); //set P1ButtonWIN to an INPUT_PULLUP
//Player 2's pin outs
pinMode(P2RLed, OUTPUT); //set P1RLed to an OUTPUT
pinMode(P2GLed, OUTPUT); //set P1GLed to an OUTPUT
pinMode(P2YLed, OUTPUT); //set P1YLed to an OUTPUT
pinMode(P2ButtonSTART, INPUT_PULLUP); //set P1ButtonSTART to an INPUT_PULLUP
pinMode(P2ButtonWIRE, INPUT_PULLUP); //set P1ButtonWIRE to an INPUT_PULLUP
pinMode(P2ButtonWIN, INPUT_PULLUP); //set P1ButtonWIN to an INPUT_PULLUP
} //end void setup()
void loop() {
fairStart(); //run fairStart()
//Player 1's if statemants
if ( digitalRead(P1ButtonSTART) == 0 ) { //check if P1ButtonSTART was pressed
digitalWrite(P1RLed, LOW); //turn off P1 red Led
digitalWrite(P1GLed, HIGH); //turn on P1 green Led
digitalWrite(P1YLed, LOW); //turn off P1 yellow Led
P1playGame = 1; //P1 is playing
}//end if()
if ( digitalRead(P1ButtonWIRE) == 0 ) { //check if P1ButtonWIRE was pressed
digitalWrite(P1RLed, HIGH); //turn on P1 red Led
digitalWrite(P1GLed, LOW); //turn off P1 green Led
digitalWrite(P1YLed, LOW); //turn off P1 yellow Led
P1playGame = 0; //P1 is not playing
}// end if()
if ( digitalRead(P1ButtonWIN) == 0 && P1playGame == 1 && theWinner != 2) { //check if P1ButtonWIN was pressed, P1 is playing
digitalWrite(P1RLed, LOW); //turn on P1 red Led
digitalWrite(P1GLed, LOW); //turn off P1 green Led
digitalWrite(P1YLed, HIGH); //turn off P1 yellow Led
P2playGame = 0; //P1 is not playing
theWinner = 1; //P1 wins the game
flashLed = 1; //set flashLed to 1
}// end if()
//Player 2's if statemants
if ( digitalRead(P2ButtonSTART) == 0 ) { //check if P2ButtonSTART was pressed
digitalWrite(P2RLed, LOW); //turn off P2 red Led
digitalWrite(P2GLed, HIGH); //turn off P2 green Led
digitalWrite(P2YLed, LOW); //turn off P2 yellow Led
P2playGame = 1; //P2 is playing
}//end if()
if (digitalRead(P2ButtonWIRE) == 0 ) { //check if P2ButtonWIRE was pressed
digitalWrite(P2RLed, HIGH); //turn on P1 red Led
digitalWrite(P2GLed, LOW); //turn off P1 green Led
digitalWrite(P2YLed, LOW); //turn off P1 yellow Led
P2playGame = 0; //P1 is not playing
}// end if()
if ( digitalRead(P2ButtonWIN) == 0 && P2playGame == 1 && theWinner != 1 ) { //check if P2ButtonWIN was pressed, P2 is playing
digitalWrite(P2RLed, LOW); //turn on P2 red Led
digitalWrite(P2GLed, LOW); //turn off P2 green Led
digitalWrite(P2YLed, HIGH); //turn off P2 yellow Led
P1playGame = 0; //P2 is not playing
theWinner = 2; //P2 wins the game
flashLed = 1; //set flashLed to 1
}//end if()
if ( theWinner != 0 ) { //the speacker plays a song after a player wins
tone(A0, 174.61, 100);
delay(250);
tone(A0, 174.61, 100);
delay(225);
tone(A0, 146.83, 100);
delay(200);
tone(A0, 174.61, 100);
delay(200);
tone(A0, 196, 100);
delay(200);
tone(A0, 220, 250);
delay(400);
tone(A0, 220, 50);
delay(200);
tone(A0, 220, 50);
delay(200);
tone(A0, 196, 50);
delay(200);
tone(A0, 196, 50);
delay(200);
tone(A0, 174.61, 150);
delay(250);
tone(A0, 164.81, 100);
delay(200);
tone(A0, 196, 150);
delay(300);
tone(A0, 196, 100);
delay(200);
tone(A0, 174.61, 100);
delay(200);
tone(A0, 174.61, 100);
delay(200);
tone(A0, 164.81, 250);
delay(300);
tone(A0, 146.83, 100);
delay(500);
while (flashLed == 1 && theWinner == 1) { //check if flashLed is 1 and if theWinner is 1
int counting;
digitalWrite(P2RLed, HIGH); //turn on P2 red Led
digitalWrite(P2GLed, HIGH); //turn on P2 green Led
digitalWrite(P2YLed, HIGH); //turn on P2 yellow Led
delay(100); //a delay for 100ms
digitalWrite(P2RLed, LOW); //turn on P2 red Led
digitalWrite(P2GLed, LOW); //turn on P2 green Led
digitalWrite(P2YLed, LOW); //turn on P2 yellow Led
delay(100); //a delay for 100ms
counting = counting + 1; //counting up but 1
if ( counting == 11 ) { //check if count is 0
flashLed = 0; //set flashLed to
counting = 0; //set counting
}//end if()
}//end while ()
while (flashLed == 1 && theWinner == 2) { //check if flashLed is 1 and if theWinner is 2
int counting;
digitalWrite(P1RLed, HIGH); //turn on P1 red Led
digitalWrite(P1GLed, HIGH); //turn on P1 green Led
digitalWrite(P1YLed, HIGH); //turn on P1 yellow Led
delay(100); //a delay for 100ms
digitalWrite(P1RLed, LOW); //turn on P1 red Led
digitalWrite(P1GLed, LOW); //turn on P1 green Led
digitalWrite(P1YLed, LOW); //turn on P1 yellow Led
delay(100); //a delay for 100ms
counting = counting + 1; //counting up but 1
if ( counting == 11 ) { //check if count is 0
flashLed = 0; //set flashLed to
counting = 0; //set counting
}//end if()
}//end while
theWinner = 0; //set theWinner to 0
showLed = 1; //set showLed to 0
}//end if()
while (showLed == 1 ) { //check if showLed is 1
PORTB = random(1, 64); //randomizing port b
delay(150); //set a daley of 150ms
}//end while
}// end void loop()
//*********************************************************
//** ALL FUNCTIONS ARE BELOW **
//*********************************************************
//*********************************************************
//* *
//* Name: fairStart() *
//* Input: NONE *
//* Output: NONE *
//* Desc: will start the game when both players are at *
//* the start *
//*********************************************************
void fairStart() {
while (startTheGame == 0) {
while (digitalRead(P1ButtonSTART) != 0 || digitalRead(P2ButtonSTART) != 0 ) {
if ( digitalRead(P1ButtonSTART) != 0 ) {
digitalWrite(P1RLed, HIGH); //Turn on P1's red Led
} else {
digitalWrite(P1RLed, LOW); //Turn on P1's red Led
} // end if()
if ( digitalRead(P2ButtonSTART) != 0 ) {
digitalWrite(P2RLed, HIGH); //Turn on P2's red Led
} else {
digitalWrite(P2RLed, LOW); //Turn on P2's red Led
} // end if()
}// end 2nd While
tone(speaker, 500, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 400, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
tone(speaker, 300, 200); //set the tone of the speaker
delay(200); //a delay for 200ms
delay(random(1, 5) * 1000); //a random delay to play the next tone
tone(speaker, 500, 200); //play the tone of the speaker after the random delay
if (digitalRead(P1ButtonSTART) == 0 && digitalRead(P2ButtonSTART) == 0) { //check if P1ButtonSTART and P2ButtonSTART are 0
startTheGame = 1; // set startTheGame to 1
} // end if()
else {
startTheGame = 0; // set startTheGame to 0
}// end else if()
}// end while ()
} // end fairStart()