//*****************************************************************************************
//* Name: Gabrielle Myers *
//* Program: GM_MADWIRE.ino *
//* Date: April 19, 2024 *
//* Description: A game in which two player try to get their wand to the end of the wire. *
//* To start, both player must be at their starting point. Once the light *
//* turns green and the speaker goes off, both players will try to get their *
//* wand to the end. If they touch the wire, they must go back to the start *
//* before they can continue to play. However gets to the end first wins and *
//* the winner light goes on. Then a tine for the winner will play and then *
//* the losers light will blink ten times before a colorful lightshow begins *
//* on the leds on both sides. *
//*****************************************************************************************
const int speaker = A0; // Sets speaker to analog pin zero
// player 1 pins
const int P1GoToStart = 8; // Sets P1GoToStart led as pin 8
const int P1PlayGame = 9; // Sets P1AGoToStart led as pin 9
const int P1WonGame = 10; // Sets P1WonGame led as pin 10
const int P1Start = 2; // Sets P1Start as pin 2
const int P1TheWire= 3; // Sets P1TheWire as pin 3
const int P1Finish = 4; // Sets P1finish as pin 4
// player 2 pins
const int P2GoToStart = 11; // Sets P12GoToStart led as pin 11
const int P2PlayGame = 12; // Sets P2AGoToStart led as pin 12
const int P2WonGame = 13; // Sets P2WonGame led as pin 13
const int P2Start = 5; // Sets P2Start as pin 5
const int P2TheWire = 6; // Sets P2TheWire as pin 6
const int P2Finish = 7; // Sets P2finish as pin 7
// Game variables
int exitLoop = 0; // Sets exitLoop value as zero
int startGame = 0;// Sets startGame value as zero
int P1GoStart = 0; // Sets P1GoStart value as zero
int P1Play = 0; // Sets P1Play value as zero
int P1Winner = 0; // Sets P1Winner value as zero
int P2GoStart = 0; // Sets P2GoStart value as zero
int P2Play = 0; // Sets P2Play value as zero
int P2Winner = 0; // Sets P2Winner value as zero
void setup() {
pinMode(P1GoToStart, OUTPUT); // Sets P1GoToStart as an output
pinMode(P1PlayGame, OUTPUT); // Sets P1PlayGMe as an output
pinMode(P1WonGame, OUTPUT); // Sets P1WonGame as an output
pinMode(P1Start, INPUT_PULLUP); // Sets P1Start as an input pullup
pinMode(P1TheWire, INPUT_PULLUP); // Sets P1TheWire as an input pullup
pinMode(P1Finish, INPUT_PULLUP); // Sets P1Finish as an inout pullup
pinMode(P2GoToStart, OUTPUT); // Sets P2GoToStart as an output
pinMode(P2PlayGame, OUTPUT); // Sets P2PlayGame as an output
pinMode(P2WonGame, OUTPUT); // Sets P2WonGame as an output
pinMode(P2Start, INPUT_PULLUP); // Sets P2Start as an input pullup
pinMode(P2TheWire, INPUT_PULLUP); // Sets P2TheWire as an input pullup
pinMode(P2Finish, INPUT_PULLUP); // SetsP2Finish as an input pullup
pinMode(speaker,OUTPUT); // Sets the speaker as an output
tone(speaker, 255,100);
delay(1000);
tone(speaker, 193,100);
delay(1000);
tone(speaker, 150,100);
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop()
{
fairStart();
// Player 1 code
if (digitalRead(P1Start) == 0) // Checks if player 1 is at the start
{
digitalWrite(P1GoToStart, LOW); // Turns off P1GoToStart led
digitalWrite(P1PlayGame, HIGH); // Turns on P1PlayGame led
digitalWrite(P1WonGame, LOW); // Turns off the P1WonGame led
P1Play = 1; // Sets the P1Play value as 1 (player 1 can play)
}// end if
if (digitalRead(P1TheWire) == 0) // Checks if player 1 had hit the wire
{
digitalWrite(P1GoToStart, HIGH); // Turns on P1GoToStart led
digitalWrite(P1PlayGame, LOW); // Turns off P1PlayGame led
digitalWrite(P1WonGame, LOW); // Turns off P1WonGame led
P1Play = 0; // Sets P1Play value to zero (player 1 cannot play)
}// end if
if (digitalRead(P1Finish) == 0 && P1Play == 1) // Checks to see if player 1 is at the end and was playing
{
digitalWrite(P1GoToStart, LOW); // Turns off P1GoToStart led
digitalWrite(P1PlayGame, LOW); // Turns off P1PlayGame led
digitalWrite(P1WonGame, HIGH); // Turns on P1WonGame led
P1Play = 0; // Sets P1Play value as zero (player 1 can no longer play)
P1Winner = 1; // Sets P1Winner value as 1 (player 1 is the winner)
winTune(); // The Tune for the winner
for (int x = 0; x < 10; x++)
{
analogWrite(P2GoToStart, 255); // Turns on P2GoToStart led
analogWrite(P2PlayGame, 255); // Turns on P2PlayGame led
analogWrite(P2WonGame, 255); // Turns on P2WonGame led
delay(100); // Waits 100 miliseconds
analogWrite(P2GoToStart, 0); // Turns off P2GoToStart led
analogWrite(P2PlayGame, 0); // Turns off P2PlayGame led
analogWrite(P2WonGame, 0); // Turns off P2WonGame led
delay(100); // Waits 100 miliseconds
}
while (true) // Checks if infinite value is 1
{
PORTB = random(1,63); // Turns on all led in a random fashion
delay(100); // Waits 100 miliseconds
}
}
// Player 2 code
if (digitalRead(P2Start) == 0) // Checks if player 2 is at the start
{
digitalWrite(P2GoToStart, LOW); // Turns off P2GoToStart led
digitalWrite(P2PlayGame, HIGH); // Turns on P2PlayGame led
digitalWrite(P2WonGame, LOW); // Turns off the P2WonGame led
P2Play = 1; // Sets P2Play value to one (player 1 can play)
}
if (digitalRead(P2TheWire) == 0) // Checks if player 2 had hit the wire
{
digitalWrite(P2GoToStart, HIGH); // Turns on P2GoToStart led
digitalWrite(P2PlayGame, LOW); // Turns off P2PlayGame led
digitalWrite(P2WonGame, LOW); // Turns off the P2WonGame led
P2Play = 0; // Sets P2Play value to zero (player 2 cannot play)
}
if (digitalRead(P2Finish) == 0 && P2Play == 1) // Checks to see if player 2 is at the end and was playing
{
digitalWrite(P2GoToStart, LOW); // Turns off P2GoToStart led
digitalWrite(P2PlayGame, LOW); // Turns off P2PlayGame led
digitalWrite(P2WonGame, HIGH); // Turns on the P2WonGame led
P2Play = 0; // Sets P2Play value to zero (player 2 cannot play)
P2Winner = 1; // Sets P2Winner value as 1 (player 2 is the winner)
winTune(); // The Tune for the winner
for (int x = 0; x <= 10; x++)
{
analogWrite(P1GoToStart, 255); // Turns on P2GoToStart led
analogWrite(P1PlayGame, 255); // Turns on P2PlayGame led
analogWrite(P1WonGame, 255); // Turns on P2WonGame led
delay(100); // Waits 100 miliseconds
analogWrite(P1GoToStart, 0); // Turns off P2GoToStart led
analogWrite(P1PlayGame, 0); // Turns off P2PlayGame led
analogWrite(P1WonGame, 0); // Turns off P2WonGame led
delay(100); // Waits 100 miliseconds
}
while (true) // Checks if the condition is true
{
PORTB = random(1,63); // Turns on all led in a random fashion
delay(100); // Waits 100 miliseconds
}
}
}
//**************************************************************
//******************FUNCTIONS BELOW*****************************
//**************************************************************
//******************************************************************************************************************
// Name: fairStart *
// INPUT: None *
// OUTPUT: Leds that turn on when players are not at start and continues until they are. *
// Description: A line of code that makes both players have to go to the starting position before the game begins. *
//******************************************************************************************************************
void fairStart ()
{
while(startGame == 0) // Makes it so that while the startGame value is zero, the following code will run
{
while(digitalRead(P1Start) != 0 || digitalRead(P2Start) != 0) // Checks if the P1Start value or the P2Start value is not zero ( Checks to is if both players are not at the start)
{
if (digitalRead(P1Start) != 0) // Checks if the P1Start value is not zero ( checks if player 1 is not at start)
{
digitalWrite(P1GoToStart,HIGH); // Turns on P1GoToStart led
}
else
{
digitalWrite(P1GoToStart,LOW); // Turns off P1GoToStart led
}// end if
if (digitalRead(P2Start) != 0) // Checks if P2Start value is not zero ( checks if player 2 is not at the start)
{
digitalWrite(P2GoToStart,HIGH); // Turns on P2GoToStart led
}
else
{
digitalWrite(P2GoToStart,LOW); // Turns off P2GoToStart led
}// end if
}// end while
// Plays a tune
tone(speaker, 155,100);
delay(1000); // Delays for 1 second
tone(speaker, 300,100);
delay(1000); // Delays for 1 second
tone(speaker, 155,100);
delay( (random(1,5)*1000) ); // delays for random smount of tiem between 1-5 seconds
tone(speaker,275,1000);
if(digitalRead(P1Start) == 0 && digitalRead(P2Start)== 0 ) // Checks if both players are at the start
{
startGame = 1; // Sets startGame value as one ( lets the game begin)
}
else
{
startGame = 0; // Keeps startGame value as zero and restarts fairStart program
}// end if
} // end while 2
}
//******************************************************************************************************************
// Name: winTune *
// INPUT: None *
// OUTPUT: Plays a tune. *
// Description: A line of code that plays a tune for the winner *
//******************************************************************************************************************
void winTune()
{
// Plays a tune
tone(A0,932,1000);
delay(1000); // Delays for 1 second
tone(A0,783,1000);
delay(1000); // Delays for 1 second
tone(A0,587,3000);
delay(1000); // Delays for 1 second
tone(A0,932,1000);
delay(1000); // Delays for 1 second
tone(A0,783,1000);
delay(1000); // Delays for 1 second
tone(A0,554,3000);
delay(1000); // Delays for 1 second
tone(A0,932,1000);
delay(1000); // Delays for 1 second
tone(A0,783,1000);
delay(1000); // Delays for 1 second
tone(A0,523,3000);
}