/*
//****************************************************
//****************************************************
//** **
//** name : Liam Dutot **
//** program : 2player game with weird sstuf **
//** date : 27/10/2024 **
//** desc : 2 player race game if you touch **
//** the grey wire restart. When starting small **
//** random amount of delay will **
//** occur to prevent cheating. if you win lights **
//** will flash **
//** **
//****************************************************
//****************************************************
*/
int startGame = 0; //0 = dont start 1 = start
int winner = 0; //winner 1 = winner
int p1Play = 0; //if p1 touched wire 0 = touched
int p2Play = 0; //if p1 touched wire 0 = touched 1 = havent touched
int prevNum[] = { 0 , 0 }; // lights function [0] = player 1 [1] = player 2
byte speaker = A0; //speaker1
byte p1GoHomeLED = 8; //p1 red led
byte p1PlayLED = 9; //p1 grn led
byte p1WinLED = 10; //p1 yel led
byte p1StartInp = 2; //p1 button
byte p1WireInp = 3; //p1 grey button
byte p1WinInp = 4; //p1 yel button
byte p2GoHomeLED = 11; //p2 red led
byte p2PlayLED = 12; //p2 grn led
byte p2WinLED = 13; //p2 yel led
byte p2StartInp = 5; //p2 button
byte p2WireInp = 6; //p2 grey button
byte p2WinInp = 7; //p2 yel button
void setup() {
//set all led to output
pinMode(p1GoHomeLED, OUTPUT);
pinMode(p1PlayLED, OUTPUT);
pinMode(p1WinLED, OUTPUT);
pinMode(p2GoHomeLED, OUTPUT);
pinMode(p2PlayLED, OUTPUT);
pinMode(p2WinLED, OUTPUT);
//set start, wire, and win to input pullup
pinMode(p1StartInp, INPUT_PULLUP);
pinMode(p1WireInp, INPUT_PULLUP);
pinMode(p1WinInp, INPUT_PULLUP);
pinMode(p2StartInp, INPUT_PULLUP);
pinMode(p2WireInp, INPUT_PULLUP);
pinMode(p2WinInp, INPUT_PULLUP);
delay(1000);
//randomizes the random number outcomes
randomSeed( analogRead(0) );
//run fairstart function
fairStart();
} //end setup()
void loop() {
//check if p1 is at start and turns on grn led turns off other leds
if (startGame == 0 ) {
fairStart();
}
if ( digitalRead(p1StartInp) == 0 ) {
digitalWrite( p1GoHomeLED, LOW );
digitalWrite( p1PlayLED, HIGH );
digitalWrite( p1WinLED, LOW );
//set p1playing to 1 (can win)
p1Play = 1;
}//end if
//check if p1 is touching the grey wire
if ( digitalRead(p1WireInp) == 0 ) {
//turn on go to start led (turns off leds)
digitalWrite( p1GoHomeLED, HIGH );
digitalWrite( p1PlayLED, LOW );
digitalWrite( p1WinLED, LOW );
// sets p1playing to 0 (can't win)
p1Play = 0;
}//end if()
//check if p1 won
if ( digitalRead(p1WinInp) == 0 && p1Play == 1 ) {
//turn win led on and set winner to p1 (turns off leds)
digitalWrite( p1GoHomeLED, LOW );
digitalWrite( p1PlayLED, LOW );
digitalWrite( p1WinLED, HIGH );
p1Play = 0;
winner = 1;
//jump to win function (input other player leds)
won( p2GoHomeLED, p2PlayLED, p2WinLED );
}//end if()
//check if p2 is at start and turns on play led (turn off leds)
if ( digitalRead(p2StartInp) == 0) {
digitalWrite( p2GoHomeLED, LOW );
digitalWrite( p2PlayLED, HIGH );
digitalWrite( p2WinLED, LOW );
//sets p2playing to 1 (they can win)
p2Play = 1;
}//end if()
if ( digitalRead(p2WireInp) == 0 ) {
//turn on go to start led and set p2Play to 0 (turns off leds)
digitalWrite( p2GoHomeLED, HIGH );
digitalWrite( p2PlayLED, LOW );
digitalWrite( p2WinLED, LOW );
p2Play = 0;
}//end if()
//check if p2 won
if ( (digitalRead(p2WinInp) == 0) && (p2Play == 1 )) {
//winner is set to p2 (turns off other leds)
digitalWrite( p2GoHomeLED, LOW );
digitalWrite( p2PlayLED, LOW );
digitalWrite( p2WinLED, HIGH );
p2Play = 0;
winner = 2;
//won function (input other player leds)
won( p1GoHomeLED, p1PlayLED, p1WinLED );
}//end if()
}//end loop()
//************************************* FUNCTIONS BELOW *************************************
//*********************************************
//* *
//* NAME :theme *
//* INPUT :() *
//* OUTPUT :() *
//* DESC :play the intro theme *
//* *
//*********************************************
void Theme() {
// array to hold the hertz values of the notes on the treble clef (in order)
int notesTreble[] = {
587, 784, 880, 988, 988, 988, 988, 988, 988, 988, 932, 988, 784, 784, 784, 784, 784, 784, 784, 880, 988, 1047, 1047, 1319, 1319, 1319, 1319, 1175, 1047, 988, 988, 988, 784, 880, 988, 1047, 1047, 1319, 1319, 1319, 1319, 1175, 1047, 988, 988, 784, 784, 784, 784, 880, 988, 988, 988, 1047, 880, 880, 880, 988, 784, 784, 784, 784
}; // end notes treble
// array to hold the note durations (in order)
int noteDurations[] = {
125, 125, 125, 125, 63, 187, 250, 125, 125, 125, 125, 63, 187, 250, 125, 125, 125, 125, 63, 187, 250, 63, 62, 125, 63, 62, 125, 63, 62, 125, 250, 125, 125, 120, 62, 63, 150, 187, 187, 125, 125, 125, 125, 125, 125, 125, 125, 187, 125, 165, 234, 123, 123, 123, 123, 125, 125, 125, 187, 123, 62, 62, 62, 125, 90, 90, 90, 90, 125
};//end noteDurations[]
// Plays the pacman theme
for (int n = 0; n < 31; n++) { //runs until the 31 notes are played (thus n < 32)
tone(speaker, notesTreble[n] / 1.3, noteDurations[n]); //plays the note (divided because I was too lazy to lower all the hz vals in the array)
delay(noteDurations[n] * 1.1); //delay x 1.1 so it's slightly longer
}//end for()
}//end Theme()
//*********************************************
//* *
//* NAME :fairStart *
//* INPUT :none *
//* OUTPUT :none *
//* DESC :makes both players start at *
//* the starting position, then *
//* waits between 1 to 5 seconds *
//* to start game to prevent *
//* cheating. *
//* *
//*********************************************
void fairStart() {
//run while startGame is 0
while ( startGame == 0 ) {
//runs both or p1 isn't at starting position
//check if p1 is at start if not it will turn on go home led
if ( digitalRead(p1StartInp) != 0 ) {
digitalWrite( p1GoHomeLED, HIGH );
} else {
digitalWrite( p1GoHomeLED, LOW );
}// end if()
//check if p2 is at start if not will turn on go home led
if ( digitalRead(p2StartInp) != 0 ) {
digitalWrite( p2GoHomeLED, HIGH );
} else {
digitalWrite( p2GoHomeLED, LOW );
}// end if()
if ( (digitalRead ( p1StartInp ) == LOW) && (digitalRead ( p2StartInp ) == LOW) ) {
startGame = 1;
}
}//end while startgame = 0()
//turn both lights off so theres no go to start light on when they are at start
digitalWrite( p1GoHomeLED, LOW );
digitalWrite( p2GoHomeLED, LOW );
//3 beep
tone ( speaker, 500, 100 );
delay(1000);
tone ( speaker, 500, 100 );
delay(1000);
tone ( speaker, 500, 100 );
//delay for a random amount of time, 1-5 seconds
delay( random( 1, 5 ) * 1000 );
//check if both ps are still at starting position
if ( (digitalRead(p1StartInp) == LOW) && (digitalRead(p2StartInp) == LOW) ) {
digitalWrite(p1PlayLED, HIGH);
digitalWrite(p2PlayLED, HIGH);
//play high pitch beep & exit loop()
startGame = 1;
tone ( speaker, 1000, 200 );
} else {
//play low pitch beep and repeats loop()
startGame = 0;
tone ( speaker, 100, 100 );
}//end if()
}//end while startGame is equal to 0()
//end fairStart()
//**********************************************
//* *
//* NAME :won *
//* INPUT :3 byte *
//* OUTPUT :none *
//* DESC :blink opponents leds and *
//* plays sound *
//* *
//**********************************************
//inputs are the enemy's leds
void won( byte red, byte green, byte yellow ) {
//play win theme
winTheme();
//delay
delay(100);
//blink the oponents lights 10 times
for ( int n = 0 ; n < 10 ; n++ ) {
//turns the leds on
digitalWrite( red, HIGH );
digitalWrite( green, HIGH );
digitalWrite( yellow, HIGH );
//delay
delay(300);
//turn all leds off
digitalWrite( red, LOW );
digitalWrite( green, LOW );
digitalWrite( yellow, LOW );
//delay
delay(300);
}//end for()
//loop that runs forever
while ( 1 == 1 ) {
//blink everyones lights
blinkLights( p2GoHomeLED, p2PlayLED, p2WinLED, 1 ); //p2 lights blink
blinkLights( p1GoHomeLED, p1PlayLED, p1WinLED, 0 ); //p1 lights blink
delay(300); //delay
}//end while()
}//end winner()
//*********************************************
//* *
//* NAME :blinkLights() *
//* INPUT :3 bytes *
//* OUTPUT :none *
//* DESC :randomly blinks a set of leds *
//* *
//*********************************************
//inputs are a player leds and prevNum index 0 = p1, 1 = p2
void blinkLights( byte red, byte green, byte yellow, int player ) {
int newLight = 0; //var to hold to allow the loop to be closed
int randomNum = 0; //var to hold random number 1-3
//make sure that a new light is lit up every single time
while ( newLight == 0 ) {
randomNum = random(1, 4); //sets randomNum to a random number (1, 2, or 3)
if (randomNum != prevNum[player]) { //checks if randomNum[ player( 0 or 1 ) ] is not = to the previous num
newLight = 1; //sets newLight to 1 (exits loop)
}//end if()
prevNum[player] = randomNum; //sets previousNum[0 or 1] to the new random number
}//end while()
//turn all leds off
digitalWrite( red, LOW );
digitalWrite( green, LOW );
digitalWrite( yellow, LOW );
//check which led # is randomNum equal to
if (randomNum == 1) {
//if 1 turn on red led
digitalWrite( red, HIGH );
} else if (randomNum == 2) {
//if 2 turn on grn led
digitalWrite( green, HIGH );
} else {
//if 3 turn on yel led
digitalWrite( yellow, HIGH );
}//end if()
newLight = 0; //reset new light var
}//end blinkLights()
//**********************************************
//* *
//* NAME : winTheme() *
//* INPUT : none *
//* OUTPUT : none *
//* DESC : plays a winner theme *
//* *
//**********************************************
void winTheme () {
//music note (hertz values in order)
int notes[] = {
392, 523, 659, 784, 659, 208, 262, 311, 415, 622, 784, 0, 233, 294, 349, 466, 587, 698, 932, 932, 932, 932, 1047
};//end hertz of notes()
//note durations
int durations[] = {
117, 117, 117, 351, 351, 117, 117, 117, 117, 117, 351, 50, 117, 117, 117, 117, 117, 117, 351, 100, 100, 100, 1500
};//end all durations()
//plays the sfx
for (int n = 0; n < 27; n++) { //runs until all the 31 notes are played (therefor n < 32)
tone(speaker, notes[n] / 1.3, durations[n]); //play the note
delay(durations[n] * 1.1); //delay durations
}//end for()
}//end winTheme()