int startGame = 0; //variable to start game (0 = don't start, 1 = start)
int winner = 0; //variable to hold who won
int player1Playing = 0; //variable to hold if player1 touched wire (0 = touched)
int player2Playing = 0; //variable to hold if player1 touched wire (0 = touched, 1 = haven't touched)
int prevNumber[] = { 0 , 0 }; //variable to hold previous random num (for blinkLights function) [0] = player 1, [1] = player 2
byte speaker = A0; //speaker
byte player1GoHomeLed = 8; //player1 red led
byte player1PlayLed = 9; //player1 green led pin
byte player1WinLed = 10; //player1 yellow led pin
byte player1StartInput = 2; //player1 slide switch
byte player1WireInput = 3; //player1 gray button
byte player1WinInput = 4; //player1 yellow button
byte player2GoHomeLed = 11; //player2 red led
byte player2PlayLed = 12; //player2 green led
byte player2WinLed = 13; //player2 yellow led
byte player2StartInput = 5; //player2 slide switch
byte player2WireInput = 6; //player2 gray button
byte player2WinInput = 7; //player2 yellow button
void setup() {
//sets leds for player2 and player1 to output
pinMode( player1GoHomeLed, OUTPUT );
pinMode( player1PlayLed, OUTPUT );
pinMode( player1WinLed, OUTPUT );
pinMode( player2GoHomeLed, OUTPUT );
pinMode( player2PlayLed, OUTPUT );
pinMode( player2WinLed, OUTPUT );
//sets all inputs(buttons) to input_pullup
pinMode( player1StartInput, INPUT_PULLUP);
pinMode( player1WireInput, INPUT_PULLUP);
pinMode( player1WinInput, INPUT_PULLUP);
pinMode( player2StartInput, INPUT_PULLUP);
pinMode( player2WireInput, INPUT_PULLUP);
pinMode( player2WinInput, INPUT_PULLUP);
//delay to allow the speaker to work
delay(1000);
//plays win theme song
winthemesong();
//randomizes the outcomes
randomSeed( analogRead(0) );
//runs fair start function
fairStart();
}//end setup()
void loop() {
//checks if player1 is at start and turns on play led
if ( digitalRead(player1StartInput) == 0 ) {
digitalWrite( player1GoHomeLed, LOW );
digitalWrite( player1PlayLed, HIGH );
digitalWrite( player1WinLed, LOW );
//sets p1playing to 1 (they can win)
player1Playing = 1;
}//end if
//checks if player1 is touching wire
if ( digitalRead(player1WireInput) == 0 ) {
//turns on go to start led and sets player1Playing to 0
digitalWrite( player1GoHomeLed, HIGH );
digitalWrite( player1PlayLed, LOW );
digitalWrite( player1WinLed, LOW );
player1Playing = 0;
}//end if()
//checks if player1 won
if ( digitalRead(player1WinInput) == 0 && player1Playing == 1 ) {
//turns the win led on and sets winner to player1
digitalWrite( player1GoHomeLed, LOW );
digitalWrite( player1PlayLed, LOW );
digitalWrite( player1WinLed, HIGH );
player1Playing = 0;
winner = 1;
//jumps to won function
won( player2GoHomeLed, player2PlayLed, player2WinLed );
}//end if()
//checks if player2 is at start and turns on play led
if ( digitalRead(player2StartInput) == 0 ) {
digitalWrite( player2GoHomeLed, LOW );
digitalWrite( player2PlayLed, HIGH );
digitalWrite( player2WinLed, LOW );
//sets p2playing to 1 (they can win)
player2Playing = 1;
}//end if()
if ( digitalRead(player2WireInput) == 0 ) {
//turns on go to start led and sets player2Playing to 0
digitalWrite( player2GoHomeLed, HIGH );
digitalWrite( player2PlayLed, LOW );
digitalWrite( player2WinLed, LOW );
player2Playing = 0;
}//end if()
//checks if player2 won the game
if ( (digitalRead(player2WinInput) == 0) && (player2Playing == 1 )) {
//turns the win led on and sets winner to player2
digitalWrite( player2GoHomeLed, LOW );
digitalWrite( player2PlayLed, LOW );
digitalWrite( player2WinLed, HIGH );
player2Playing = 0;
winner = 2;
//jumps to won function
won( player1GoHomeLed, player1PlayLed, player1WinLed);
}//end if()
}//end loop()
//*******************************************************************************
//************************************* FUNCTIONS BELOW *************************
//************************************************
//** **
//** NAME : winThemesong() **
//** INPUT : none **
//** OUTPUT : none **
//** DESC : plays the intro song **
//** **
//************************************************
//*******************************************************************************
//**************************************FUNCTIONS ABOVE**************************
void winthemesong() {
//Array to hold the hertz values of the notes
int notesTreble[] = {
262, 999, 740, 523, 400, 200, 1000, 1500, 1000, 723, 500, 1000, 702, 662,
452, 918, 724, 624, 924, 600, 659, 650, 659, 690, 698, 740, 900, 1000, 1300, 1600, 1988
};//end notesTreble
//Array to hold the note durations
int noteDurations[] = {
500, 300, 225, 225, 223, 227, 250, 225, 125, 555, 175, 623, 147, 250, 180,
525, 325, 535, 163, 387, 250, 263, 462, 125, 263, 462, 225, 523, 112, 125, 1050
};//end noteDuration[]
// Plays the winthemesong
for (int n = 0; n < 31; n++) { //runs until the all notes are done playing
tone(speaker, notesTreble[n] / 1.3, noteDurations[n]); //plays the note
delay(noteDurations[n] * 1.1); //delay x 1.1 makes it longer
}//end for()
}//end winThemesong()
//***********************************************
//** **
//** NAME : fairStart() **
//** INPUT : none **
//** OUTPUT : none **
//** DESC : makes both players start at **
//** the starting position, then **
//** waits between 1-5 sec **
//** **
//** **
//** **
//***********************************************
void fairStart() {
//runs while start Game = 0
while ( startGame == 0 ) {
//runs either with 1 or 2 players isn't at start
while ( (digitalRead ( player1StartInput ) != 0) || (digitalRead ( player2StartInput ) != 0) ) {
//checks if player1 is at start, if not will turn on go home led
if ( digitalRead(player1StartInput) != 0 ) {
digitalWrite( player1GoHomeLed, HIGH );
} else {
digitalWrite( player1GoHomeLed, LOW );
}// end if()
//checks if player2 is at start, if not will turn on go home led
if ( digitalRead(player2StartInput) != 0 ) {
digitalWrite( player2GoHomeLed, HIGH );
} else {
digitalWrite( player2GoHomeLed, LOW );
}// end if()
}//end while ( digitalRead player1StartInput != 0| digitalRead player2StartInput != 0 )
//turns both lights off
digitalWrite( player1GoHomeLed, LOW );
digitalWrite( player2GoHomeLed, LOW );
//3 beeps before the game starts
tone ( speaker, 500, 100 );
delay(1000);
tone ( speaker, 500, 100 );
delay(1000);
tone ( speaker, 500, 100 );
//delay for a random time
delay( random( 1, 5 ) * 1000 );
//checks if both players are still at start
if ( (digitalRead(player1StartInput) == 0) && (digitalRead(player2StartInput) == 0) ) {
//plays high pitched sound and exits loop
startGame = 1;
tone ( speaker, 1000, 200 );
} else {
//plays low pitched sound and repeats loop
startGame = 0;
tone ( speaker, 100, 100 );
}//end if()
}//end while ( startGame == 0 )
}//end fair Start ()
//***********************************************
//** **
//** NAME : won() **
//** INPUT : 3 bytes **
//** OUTPUT : none **
//** DESC : Blinks other player LED **
//** **
//** **
//***********************************************
//inputs are the other player`s Led
void won( byte red, byte green, byte yellow ) {
//plays win theme song
winTheme();
//delay
delay(100);
//blinks the enemy's 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);
//turns the leds off
digitalWrite( red, LOW );
digitalWrite( green, LOW );
digitalWrite( yellow, LOW );
//delay
delay(300);
}//end for()
//loop that runs forever
while ( 1 == 1 ) {
//blinks everyone's lights (simple light show)
blinkLights( player2GoHomeLed, player2PlayLed, player2WinLed, 1 ); //player2 lights blink
blinkLights( player1GoHomeLed, player1PlayLed, player1WinLed, 0 ); //player1 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 previousNumber ([0] = p1 , [1] = p2)
void blinkLights( byte red, byte green, byte yellow, int player ) {
int newLight = 0; //variable to hold when to allow loop to be closed
int randomNumber = 0; //variable to hold random num (1-3)
//makes sure that a new light is on every time
while ( newLight == 0 ) {
randomNumber = random(1, 4); //sets randomNumber to a random number (1, 2, or 3)
if (randomNumber != prevNumber[player]) { //checks if randomNumber[ player( 0 or 1 ) ]
newLight = 1; //sets newLight to 1 (exits loop)
}//end if()
prevNumber[player] = randomNumber; //sets previousNumber[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 randomNumber equal to
if (randomNumber == 1) {
//if 1, turn on red led
digitalWrite( red, HIGH );
} else if (randomNumber == 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 variable
}//end blinkLights()
//**********************************************
//* *
//* NAME : winTheme() *
//* INPUT : none *
//* OUTPUT : none *
//* DESC : plays a winner theme *
//* *
//**********************************************
void winTheme () {
//music notes
int notes[] = {
995, 254, 323, 253, 822, 674, 488, 653, 282, 269, 355, 822, 234, 622, 784, 110, 263, 794, 949, 296, 327, 598, 242, 142, 142, 1000, 1500
};//end notes[]
//note duration
int durations[] = {
117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 1500
};//end duration[]
//plays sound
for (int n = 0; n < 27; n++) { //runs until the 31 notes are played
tone(speaker, notes[n] / 1.3, durations[n]); //plays the note
delay(durations[n] * 1.1); //delay x 1.1 makes it longer
}//end for()
}//end winThemesong()