//********************************************
//********************************************
//*** Name: Asad Saad ***
//*** Date: 10/18/23 ***
//*** Program: 2PlayerGame.ino ***
//*** Desc.:a program in which we use int ***
//*** errupts to cycle through different ***
//*** menus and includes an LED. ***
//********************************************
//********************************************
const int speaker = A0; //create constant variable for the speaker (analog pin)
const int p1GoHomeLED = 8; //create a constant variable for the P1 go home LED
const int p1WinnerLED = 10; //create a constant variable for the P1 winner LED
const int p1PlayLED = 9; //create a constant variable for the P1 play LED
const int p1Start = 2; //create a constnat variable for the P1 start contact
const int p1Wire = 3; //create a constant variable for the P1 wire
const int p1Win = 4; //create a constant variable for the P1 win contact
const int p2GoHomeLED = 11; //create a constant variable for the P2 go home LED
const int p2WinnerLED = 13; //create a constant variable for the P1 winner LED
const int p2PlayLED = 12; //create a constant variable for the P1 play LED
const int p2Start = 5; //create a constnat variable for the P1 start contact
const int p2Wire = 6; //create a constant variable for the P1 wire
const int p2Win = 7; //create a constant variable for the P1 win contact
int startTheGame = 0; //create a variable for if the game should be started or not
int theWinnerIs = 0; //create a variable for who the winner is after the winner is determined
int p1PlayingTheGame = 0; //create a variable that says if P1 is playing
int p2PlayingTheGame = 0; //create a variable that says if P2 is playing
int exitLoop = 0; //create a variable to exit a loop
void setup() {
//Play "Super Mario Bros. theme song"
delay(1000);
tone(speaker, 660, 100);
delay(150);
tone(speaker, 660, 100);
delay(300);
tone(speaker, 660, 100);
delay(300);
tone(speaker, 510, 100);
delay(150);
tone(speaker, 660, 100);
delay(300);
tone(speaker, 770, 100);
delay(550);
tone(speaker, 380, 100);
delay(2000);
pinMode(speaker, OUTPUT); //set speaker pinmode to an output
//set LEDs to outputs
pinMode(p1GoHomeLED, OUTPUT);
pinMode(p1WinnerLED, OUTPUT);
pinMode(p1PlayLED, OUTPUT);
pinMode(p2GoHomeLED, OUTPUT);
pinMode(p2WinnerLED, OUTPUT);
pinMode(p2PlayLED, OUTPUT);
//set all contacts to Input pullups, meaning they will give a value of 0 when grounded
pinMode(p1Start, INPUT_PULLUP);
pinMode(p1Wire, INPUT_PULLUP);
pinMode(p1Win, INPUT_PULLUP);
pinMode(p2Start, INPUT_PULLUP);
pinMode(p2Wire, INPUT_PULLUP);
pinMode(p2Win, INPUT_PULLUP);
//turn on go home LEDs
digitalWrite(p1GoHomeLED, HIGH);
digitalWrite(p2GoHomeLED, HIGH);
//create a do loop which will loop until both players are touching their start contacts
do {
if ((digitalRead(p1Start) == 0) && (digitalRead (p2Start) == 0)) {
exitLoop = 1;
}// end if()
} while (exitLoop == 0);
randomSeed(analogRead(0)); //initailizes the pseudo-random number generator, causing it to start at a random point
}// end setup()
void loop() {
fairStart(); //jumps to the fairStart function
if (digitalRead(p1Start) == 0) { //if P1 start contact is being contacted, turn on P1 play LED and turn other LEDs off
digitalWrite(p1PlayLED, HIGH);
digitalWrite(p1GoHomeLED, LOW);
digitalWrite(p1WinnerLED, LOW);
p1PlayingTheGame = 1; //set P1 playing the game variable to 1
}// end if()
if (digitalRead(p1Wire) == 0) { //if P1 wire is being touched, turn on P1 go home LED, telling the player to go home and turn off other LEDs
digitalWrite(p1PlayLED, LOW);
digitalWrite(p1GoHomeLED, HIGH);
digitalWrite(p1WinnerLED, LOW);
p1PlayingTheGame = 0; //set P1 playing the game variable to 0 as P1 has stopped playing the game
}// end if()
if ((digitalRead(p1Win) == 0) && (p1PlayingTheGame == 1)) { //if P1 win contact is being contacted and P1 is still playing the game, turn on P1 Win LED
digitalWrite(p1PlayLED, LOW);
digitalWrite(p1GoHomeLED, LOW);
digitalWrite(p1WinnerLED, HIGH);
p1PlayingTheGame = 0; //set P1 playing the game variable back to 0 as P1 has already won the game and can't keep winning
theWinnerIs = 1; //set the winner is variable to 1, indicating that P1 has won
}// end if()
if (digitalRead(p2Start) == 0) { //if P2 start contact is being contacted, turn on P1 play LED and turn other LEDs off
digitalWrite(p2PlayLED, HIGH);
digitalWrite(p2GoHomeLED, LOW);
digitalWrite(p2WinnerLED, LOW);
p2PlayingTheGame = 1; //set P2 playing the game variable to 1
}// end if()
if (digitalRead(p2Wire) == 0) { //if P2 wire is being touched, turn on P2 go home LED, telling the player to go home and turn off other LEDs
digitalWrite(p2PlayLED, LOW);
digitalWrite(p2GoHomeLED, HIGH);
digitalWrite(p2WinnerLED, LOW);
p2PlayingTheGame = 0; //set P2 playing the game variable to 0 as P2 has stopped playing the game
}// end if()
if ((digitalRead(p2Win) == 0) && (p2PlayingTheGame == 1)) { //if P2 win contact is being contacted and P2 is still playing the game, turn on P2 Win LED
digitalWrite(p2PlayLED, LOW);
digitalWrite(p2GoHomeLED, LOW);
digitalWrite(p2WinnerLED, HIGH);
p2PlayingTheGame = 0; //set P2 playing the game variable back to 0 as P2 has already won the game and can't keep winning
theWinnerIs = 2; //set the winner is variable to 2, indicating that P2 has won
}// end if()
if (theWinnerIs == 1) {
//play "We Wish You A Merry Christmas" song
tone (speaker, 523, 250);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 784, 125);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 659, 125);
delay(250);
tone (speaker, 587, 250);
delay(250);
tone (speaker, 587, 250);
delay(250);
tone (speaker, 587, 250);
delay(250);
for (int x = 0; x <= 10; x++) { //for loop that uses variable to loop 10 times. Makes P2's LEDs blink 10 times on and off in unison when P1 wins
//turn on all P2's LEDs
digitalWrite(p2PlayLED, HIGH);
digitalWrite(p2GoHomeLED, HIGH);
digitalWrite(p2WinnerLED, HIGH);
delay(150); //delay 150ms
//turn off all P2's LEDs
digitalWrite(p2PlayLED, LOW);
digitalWrite(p2GoHomeLED, LOW);
digitalWrite(p2WinnerLED, LOW);
delay(150); //delay 150ms
}// end for()
delay(1000); //delay 1000ms to separate light blinking from light show
while (theWinnerIs == 1) { //when the winnner is 1 make a cool light show, attracting new players. looping essentially forever
PORTB = random(1, 63); //sends random values to PORTB which includes all the LEDs making them blink in a random fashion
delay (150); //delay 150ms
}
}// end if()
if (theWinnerIs == 2) {
//play "We Wish You A Merry Christmas" song
tone (speaker, 523, 250);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 784, 125);
delay(250);
tone (speaker, 698, 125);
delay(250);
tone (speaker, 659, 125);
delay(250);
tone (speaker, 587, 250);
delay(250);
tone (speaker, 587, 250);
delay(250);
tone (speaker, 587, 250);
delay(250);
for (int x = 0; x <= 10; x++) { //for loop that uses variable to loop 10 times. Makes P2's LEDs blink 10 times on and off in unison when P1 wins
//turn on all P1's LEDs
digitalWrite(p1PlayLED, HIGH);
digitalWrite(p1GoHomeLED, HIGH);
digitalWrite(p1WinnerLED, HIGH);
delay(150); //delay 150ms
//turn off all P1's LEDs
digitalWrite(p1PlayLED, LOW);
digitalWrite(p1GoHomeLED, LOW);
digitalWrite(p1WinnerLED, LOW);
delay(150); //delay 150ms
}// end for()
delay(1000); //delay 1000ms to separate light blinking from light show
while (theWinnerIs == 2) { //when the winnner is 2 make a cool light show, attracting new players. looping essentially forever
PORTB = random(1, 63); //sends random values to PORTB which includes all the LEDs making them blink in a random fashion
delay (150); //delay 150ms
}
}// end if()
}// end loop()
//@@@@@@@@@@@@@@@@@@@@@@@@@ Functions Here @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//**************************************************************************
//** Function Name: fairstart() **
//** Inputs: void **
//** Returns: void (nothing) **
//** Desc: when reset interrupt is pressed, resetGame variable set to one **
//** and P1-3Stopped are reset to 0 **
//**************************************************************************
void fairStart() {
while (startTheGame == 0) {
while ((digitalRead (p1Start)) != 0 || (digitalRead (p2Start)) != 0) { //loop as long as one of the start contacts is not being pressed
if (digitalRead (p1Start) != 0) { //when P1 start contact is not being pressed, turn on P1 go home LED
digitalWrite (p1GoHomeLED, HIGH);
} else { //otherwise turn off P1 go home LED
digitalWrite (p1GoHomeLED, LOW);
}// end if else()
if (digitalRead (p2Start) != 0) { //when P2 start contact is not being pressed, turn on P2 go home LED
digitalWrite (p2GoHomeLED, HIGH);
} else { //otherwise turn off P2 go home LED
digitalWrite (p2GoHomeLED, LOW);
}// end if else()
}// end while()
for (int x = 0; x < 3; x++) { //for loop loops 3 times buzzer sounding 3 times, 1 second each 1 second apart
tone(speaker, 500, 1000);
delay(1000);
tone(speaker, 0, 1000);
delay(1000);
}// end for()
delay (random((1, 6) * 1000)); //add random delay between 1 and 5 seconds, preventing either player from predicting when the game will start
if (digitalRead (p1Start) == 0 && digitalRead (p2Start) == 0) { //check if both players are at the start once agian using if statement. if both start contacts are being contacted, set start the game variable to 1. otherwise one player is cheating and dont start the game
startTheGame = 1; //set start the game variable to 1
} else {
startTheGame = 0; //otherwise set start the game variable to 0
}
}// end while()
}// end fairStart()