/*
//************************************************
//************************************************
//* **
//* name : Liam Dutot **
//* program : 2player game with weird sstuf **
//* date : 21/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 cheater if you win lights **
//* will flash **
//* **
//************************************************
//************************************************
*/
int startGame = 0; //var to start game (0 = don't start, 1 = start)
int winner = 0; //var to hold who won
int p1Playing = 0; //var to hold if p1 touched wire (0 = touched)
int p2Playing = 0; //var to hold if p1 touched wire (0 = touched, 1 = haven't touched)
int prevNum[] = { 0 , 0 }; //var to hold previous random num (for blinkLights function) [0] = player 1, [1] = player 2
byte speaker = A0; //speaker pin
byte p1GoHomeLED = 8; //p1 red led pin
byte p1PlayLED = 9; //p1 green led pin
byte p1WinLED = 10; //p1 yellow led pin
byte p1StartInp = 2; //p1 slide switch pin (start)
byte p1WireInp = 3; //p1 gray button pin (wire)
byte p1WinInp = 4; //p1 yellow button pin (end)
byte p2GoHomeLED = 11; //p2 red led pin
byte p2PlayLED = 12; //p2 green led pin
byte p2WinLED = 13; //p2 yellow led pin
byte p2StartInp = 5; //p2 slide switch pin (start)
byte p2WireInp = 6; //p2 gray button pin (wire)
byte p2WinInp = 7; //p2 yellow button pin (end)
void setup() {
// put your setup code here, to run once:
//sets leds for p2 and p1 to output
pinMode( p1GoHomeLED, OUTPUT );
pinMode( p1PlayLED, OUTPUT );
pinMode( p1WinLED, OUTPUT );
pinMode( p2GoHomeLED, OUTPUT );
pinMode( p2PlayLED, OUTPUT );
pinMode( p2WinLED, OUTPUT );
//sets all inputs(buttons) 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 to allow the speaker to work properly
delay(1000);
//plays pacman theme
pacmanTheme();
//randomizes the random number outcomes (the seed)
randomSeed( analogRead(0) );
//runs fairstart function
fairStart();
}//end setup()
void loop() {
// put your main code here, to run repeatedly:
//checks if player 1 is at start and turns on play led (turns off other leds)
if ( digitalRead(p1StartInp) == 0 ) {
digitalWrite( p1GoHomeLED, LOW );
digitalWrite( p1PlayLED, HIGH );
digitalWrite( p1WinLED, LOW );
//sets p1playing to 1 (they can win)
p1Playing = 1;
}//end if
//checks if player 1 is touching wire
if ( digitalRead(p1WireInp) == 0 ) {
//turns on go to start led and sets p1Playing to 0 (turns off other leds)
digitalWrite( p1GoHomeLED, HIGH );
digitalWrite( p1PlayLED, LOW );
digitalWrite( p1WinLED, LOW );
p1Playing = 0;
}//end if()
//checks if p1 won
if ( digitalRead(p1WinInp) == 0 && p1Playing == 1 ) {
//turns the win led on and sets winner to p1 (turns off other leds)
digitalWrite( p1GoHomeLED, LOW );
digitalWrite( p1PlayLED, LOW );
digitalWrite( p1WinLED, HIGH );
p1Playing = 0;
winner = 1;
//jumps to won function (inputs the other player's leds)
won( p2GoHomeLED, p2PlayLED, p2WinLED );
}//end if()
//checks if player 2 is at start and turns on play led (turns off other leds)
if ( digitalRead(p2StartInp) == 0 ) {
digitalWrite( p2GoHomeLED, LOW );
digitalWrite( p2PlayLED, HIGH );
digitalWrite( p2WinLED, LOW );
//sets p2playing to 1 (they can win)
p2Playing = 1;
}//end if()
if ( digitalRead(p2WireInp) == 0 ) {
//turns on go to start led and sets p2Playing to 0 (turns off other leds)
digitalWrite( p2GoHomeLED, HIGH );
digitalWrite( p2PlayLED, LOW );
digitalWrite( p2WinLED, LOW );
p2Playing = 0;
}//end if()
//checks if p2 won
if ( (digitalRead(p2WinInp) == 0) && (p2Playing == 1 )) {
//turns the win led on and sets winner to p2 (turns off other leds)
digitalWrite( p2GoHomeLED, LOW );
digitalWrite( p2PlayLED, LOW );
digitalWrite( p2WinLED, HIGH );
p2Playing = 0;
winner = 2;
//jumps to won function (inputs the other player's leds)
won( p1GoHomeLED, p1PlayLED, p1WinLED );
}//end if()
}//end loop()
//************************************* FUNCTIONS BELOW *************************************
//**********************************************
//* *
//* NAME : pacmanTheme() *
//* INPUT : none *
//* OUTPUT : none *
//* DESC : plays the intro theme *
//* *
//**********************************************
void pacmanTheme() {
// array to hold the hertz values of the notes on the treble clef (in order)
int notesTreble[] = {
493, 988, 740, 622, 988, 698, 659, 262, 1047, 784, 659, 1047, 784, 659,
493, 988, 740, 622, 988, 698, 659, 650, 659, 690, 698, 740, 780, 783, 830, 880, 988
};//end notesTreble
// 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
};//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 pacmanTheme()
//**********************************************
//* *
//* NAME : fairStart() *
//* INPUT : none *
//* OUTPUT : none *
//* DESC : makes both players start at *
//* the starting position, then *
//* waits between 1-5 seconds *
//* to start game (to prevent *
//* cheating). *
//* *
//**********************************************
void fairStart() {
//runs while startGame is 0
while ( startGame == 0 ) {
//runs while both or 1 player isn't at start
while ( (digitalRead ( p1StartInp ) != 0) || (digitalRead ( p2StartInp ) != 0) ) {
//checks if player 1 is at start, if not, will turn on go home led
if ( digitalRead(p1StartInp) != 0 ) {
digitalWrite( p1GoHomeLED, HIGH );
} else {
digitalWrite( p1GoHomeLED, LOW );
}// end if()
//checks if player 2 is at start, if not, will turn on go home led
if ( digitalRead(p2StartInp) != 0 ) {
digitalWrite( p2GoHomeLED, HIGH );
} else {
digitalWrite( p2GoHomeLED, LOW );
}// end if()
}//end while ( digitalRead ( p1StartInp ) != 0 || digitalRead ( p2StartInp ) != 0 )
//turns both lights off so there's no weird go to start light on when they're at start
digitalWrite( p1GoHomeLED, LOW );
digitalWrite( p2GoHomeLED, LOW );
//3 beeps
tone ( speaker, 500, 100 );
delay(1000);
tone ( speaker, 500, 100 );
delay(1000);
tone ( speaker, 500, 100 );
//delay for a random amt of time (1-5 seconds)
delay( random( 1, 5 ) * 1000 );
//checks if both players are still at start
if ( (digitalRead(p1StartInp) == 0) && (digitalRead(p2StartInp) == 0) ) {
//plays high pitched beep and exits loop
startGame = 1;
tone ( speaker, 1000, 200 );
} else {
//plays low pitched boop and repeats loop
startGame = 0;
tone ( speaker, 100, 100 );
}//end if()
}//end while ( startGame == 0 )
}//end fairStart()
//**********************************************
//* *
//* NAME : won() *
//* INPUT : 3 bytes *
//* OUTPUT : none *
//* DESC : Blinks opponent's LEDs and *
//* plays music. *
//* *
//**********************************************
//input is the opponents 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's leds and prevNum index ([0] = p1 , [1] = p2)
void blinkLights( byte red, byte green, byte yellow, int player ) {
int newLight = 0; //var to hold when to allow loop to be closed
int randomNum = 0; //var to hold random num (1-3)
//makes sure that a new light is lit up every 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()
//turns all leds off
digitalWrite( red, LOW );
digitalWrite( green, LOW );
digitalWrite( yellow, LOW );
//checks 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 green led
digitalWrite( green, HIGH );
} else {
//if 3, turn on yellow led
digitalWrite( yellow, HIGH );
}//end if()
newLight = 0; //resets new light var
}//end blinkLights()
//**********************************************
//* *
//* NAME : winTheme() *
//* INPUT : none *
//* OUTPUT : none *
//* DESC : plays a winner theme *
//* *
//**********************************************
void winTheme () {
//music notes (hz vals, in order)
int notes[] = {
196, 262, 330, 392, 523, 659, 784, 659, 208, 262, 311, 415, 523, 622, 784, 0, 233, 294, 349, 466, 587, 698, 932, 932, 932, 932, 1047
};//end notes[]
//note durations (in order)
int durations[] = {
117, 117, 117, 117, 117, 117, 351, 351, 117, 117, 117, 117, 117, 117, 351, 50, 117, 117, 117, 117, 117, 117, 351, 100, 100, 100, 1500
};//end durations[]
//plays the sfx
for (int n = 0; n < 27; n++) { //runs until the 31 notes are played (thus n < 32)
tone(speaker, notes[n] / 1.3, durations[n]); //plays the note (divided because I was too lazy to lower all the hz vals in the array)
delay(durations[n] * 1.1); //delay x 1.1 so it's slightly longer
}//end for()
}//end winTheme()