//***********************************
// Name: Rhys Miller *
// Program: Arduino Major Project *
// Date: April 20 2024 *
// Desc: Arduino Wire Game *
//***********************************
// Global variables to run the game
int startTheGame = 0;
int theWinnerIs = 0;
int p1PlayingTheGame = 0;
int p2PlayingTheGame = 0;
int P1AtStart = 0;
int P2AtStart = 0;
// Pin assignments for Player 1
const int P1GoHomeLed = 13;
const int P1StartLed = 12;
const int P1WinLed = 11;
const int P1Fail = 10;
const int P1Start = 9;
const int P1End = 8;
// Pin assignments for Player 2
const int P2GoHomeLed = 7;
const int P2StartLed = 6;
const int P2WinLed = 5;
const int P2Fail = 4;
const int P2Start = 3;
const int P2End = 2;
void setup() {
// Set pin modes for LEDs and buttons
pinMode(P1GoHomeLed, OUTPUT);
pinMode(P1StartLed, OUTPUT);
pinMode(P1WinLed, OUTPUT);
pinMode(P1Fail, INPUT_PULLUP);
pinMode(P1Start, INPUT_PULLUP);
pinMode(P1End, INPUT_PULLUP);
pinMode(P2GoHomeLed, OUTPUT);
pinMode(P2StartLed, OUTPUT);
pinMode(P2WinLed, OUTPUT);
pinMode(P2Fail, INPUT_PULLUP);
pinMode(P2Start, INPUT_PULLUP);
pinMode(P2End, INPUT_PULLUP);
// Initialize LED states
digitalWrite(P1StartLed, LOW);
digitalWrite(P1GoHomeLed, HIGH);
digitalWrite(P2StartLed, LOW);
digitalWrite(P2GoHomeLed, HIGH);
// Initialize random seed and play startup melody
randomSeed(analogRead(0));
tone(A0, 659.26, 100);
delay(250);
tone(A0, 659.26, 100);
delay(500);
tone(A0, 659.26, 100);
delay(500);
tone(A0, 523.25, 100);
delay(250);
tone(A0, 659.26, 250);
delay(250);
tone(A0, 783.99, 200);
}
void loop() {
// Call fairStart() to initialize the game
fairStart();
// Main game loop
while (startTheGame == 1) {
// Player 1 logic
if (digitalRead(P1Start) == 0) {
// Player 1 presses start
digitalWrite(P1GoHomeLed, LOW);
digitalWrite(P1StartLed, HIGH);
digitalWrite(P1WinLed, LOW);
P1AtStart = 1;
p1PlayingTheGame = 1;
}
if (digitalRead(P1Fail) == 0) {
// Player 1 fails
digitalWrite(P1GoHomeLed, HIGH);
digitalWrite(P1StartLed, LOW);
digitalWrite(P1WinLed, LOW);
P1AtStart = 0;
p1PlayingTheGame = 0;
}
if (digitalRead(P1End) == 0) {
// Player 1 reaches the end
digitalWrite(P1GoHomeLed, LOW);
digitalWrite(P1StartLed, LOW);
P1AtStart = 0;
if (p1PlayingTheGame == 1) {
// Player 1 wins
digitalWrite(P1WinLed, HIGH);
p1PlayingTheGame = 0;
p2PlayingTheGame = 0;
theWinnerIs = 1;
winner();
} else {
digitalWrite(P1GoHomeLed, HIGH);
}
}
// Player 2 logic
if (digitalRead(P2Start) == 0) {
// Player 2 presses start
digitalWrite(P2GoHomeLed, LOW);
digitalWrite(P2StartLed, HIGH);
digitalWrite(P2WinLed, LOW);
P2AtStart = 1;
p2PlayingTheGame = 1;
}
if (digitalRead(P2Fail) == 0) {
// Player 2 fails
digitalWrite(P2GoHomeLed, HIGH);
digitalWrite(P2StartLed, LOW);
digitalWrite(P2WinLed, LOW);
P2AtStart = 0;
p2PlayingTheGame = 0;
}
if (digitalRead(P2End) == 0) {
// Player 2 reaches the end
digitalWrite(P2GoHomeLed, LOW);
digitalWrite(P2StartLed, LOW);
P2AtStart = 0;
if (p2PlayingTheGame == 1) {
// Player 2 wins
digitalWrite(P2WinLed, HIGH);
p2PlayingTheGame = 0;
p1PlayingTheGame = 0;
theWinnerIs = 2;
winner();
} else {
digitalWrite(P2GoHomeLed, HIGH);
}
}
}
}
// fairStart method returns nothing and is used to prohibit cheating
void fairStart() {
// Wait for both players to press start simultaneously
while ((digitalRead(P1Start) != 0 || digitalRead(P2Start) != 0) && startTheGame == 0) {
// Blink start LEDs based on button status
digitalWrite(P1StartLed, digitalRead(P1Start) != 0 ? LOW : HIGH);
digitalWrite(P2StartLed, digitalRead(P2Start) != 0 ? LOW : HIGH);
// Check if both players have pressed start
if (digitalRead(P1Start) == 0 && digitalRead(P2Start) == 0) {
// Start the game
startTheGame = 1;
digitalWrite(P1GoHomeLed, LOW);
digitalWrite(P2GoHomeLed, LOW);
} else {
startTheGame = 0;
}
}
// Turn off start LEDs after game starts
digitalWrite(P1StartLed, LOW);
digitalWrite(P2StartLed, LOW);
// Play countdown sound
tone(A0, 440, 1000);
delay(2000);
tone(A0, 440, 1000);
delay(2000);
tone(A0, 440, 1000);
delay(random(1, 5) * 1088);
tone(A0, 3520, 200);
// Set players as active
p1PlayingTheGame = 1;
p2PlayingTheGame = 1;
// Blink start LEDs
digitalWrite(P1StartLed, HIGH);
digitalWrite(P2StartLed, HIGH);
delay(200);
digitalWrite(P1StartLed, LOW);
digitalWrite(P2StartLed, LOW);
}
// winner method returns nothing and is used to identify and play the win sequence
void winner() {
// Play victory sound and light show based on the winner
if (theWinnerIs == 1) {
// Player 1 wins
// Win Music
tone(A0, 261.6, 100); // C4
delay(250);
tone(A0, 329.63, 100); // E4
delay(250);
tone(A0, 392, 200); // G4
delay(250);
tone(A0, 329.63, 250); // E4
delay(250);
tone(A0, 392, 300); // G4
// Flash LEDs for Player 2
for (int x = 0; x <= 10; x++) {
digitalWrite(P2GoHomeLed, HIGH);
digitalWrite(P2StartLed, HIGH);
digitalWrite(P2WinLed, HIGH);
delay(100);
digitalWrite(P2GoHomeLed, LOW);
digitalWrite(P2StartLed, LOW);
digitalWrite(P2WinLed, LOW);
delay(100);
}
randomLightShow(); // Start random light show after victory
} else if (theWinnerIs == 2) {
// Player 2 wins
// Win Music
tone(A0, 261.6, 100); // C4
delay(250);
tone(A0, 329.63, 100); // E4
delay(250);
tone(A0, 392, 200); // G4
delay(250);
tone(A0, 329.63, 250); // E4
delay(250);
tone(A0, 392, 300); // G4
// Flash LEDs for Player 1
for (int x = 0; x <= 10; x++) {
digitalWrite(P1GoHomeLed, HIGH);
digitalWrite(P1StartLed, HIGH);
digitalWrite(P1WinLed, HIGH);
delay(100);
digitalWrite(P1GoHomeLed, LOW);
digitalWrite(P1StartLed, LOW);
digitalWrite(P1WinLed, LOW);
delay(100);
}
randomLightShow(); // Start random light show after victory
}
}
// Function to perform an infinite random light show using a while loop
void randomLightShow() {
int duration = 200; // Duration of each random flash
while (true) { // Infinite loop for continuous flashing
digitalWrite(P1GoHomeLed, random(2) == 0 ? HIGH : LOW);
digitalWrite(P1StartLed, random(2) == 0 ? HIGH : LOW);
digitalWrite(P1WinLed, random(2) == 0 ? HIGH : LOW);
digitalWrite(P2GoHomeLed, random(2) == 0 ? HIGH : LOW);
digitalWrite(P2StartLed, random(2) == 0 ? HIGH : LOW);
digitalWrite(P2WinLed, random(2) == 0 ? HIGH : LOW);
delay(duration);
}
}