// **************************************
// * *
// * Name: Hamza Al-Masri *
// * Sketch name: Jaseybox.ino *
// * Date : 2023-10-18 *
// * Desc : a program with 3 buttons*
// * and three LEDs with *
// * different colors for *
// * every player.Every *
// * player has one press *
// * until mr jasey presses *
// * continue game or reset. *
// * *
// **************************************
const int button1 = 4; //Variable for button 1 (blue)
const int button2 = 5; //Variable for button 2 (green)
const int button3 = 7; //Variable for button 3 (red)
const int ledP1 = 9; //Variable for blue led
const int ledP2 = 10; //Variable for green led
const int ledP3 = 6; //Variable for red led
const int resetInt = 3; //variable for the reset button
const int continueInt = 2; //variable for the cotinue game button
const int speaker = 8; //Variable for the speaker
int continueGame = 0; //Variable to continue game
int resetGame = 0; //Variable to reset the game
int P1Stopped = 0; //Variable for stopping player 1
int P2Stopped = 0; //Variable for stopping player 2
int P3Stopped = 0; //Variable for stopping player 3
void setup() {
pinMode (button1, INPUT_PULLUP); //Sets the blue button as an input
pinMode (button2, INPUT_PULLUP); //Sets the green button as an input
pinMode (button3, INPUT_PULLUP); //Sets the red button as an input
pinMode (ledP1, OUTPUT); //Set the blue led as an output
pinMode (ledP2, OUTPUT); //Set the green led as an output
pinMode (ledP3, OUTPUT); //Set the red led as an output
pinMode (continueInt, INPUT_PULLUP); //Set continueGame button (white button) as an intput
attachInterrupt(digitalPinToInterrupt (continueInt), contInt, FALLING); //Inturrupts the loop and continues the game
pinMode (resetInt, INPUT_PULLUP); //Set resetGame button (yellow button) as an intput
attachInterrupt(digitalPinToInterrupt (resetInt), rstInt, FALLING); //Inturrupts the code and resets the game
Serial.begin(300); //serial printing speed
}//End void setup
void loop() {
Serial.println("looping"); //Prints looping at the begining of every loop
if ((digitalRead(button1) == 0) ){ //If the button is presseed and the player isn't stopped, run the code
P1Stopped = 1; //Stop player 1 from pressing again
buttonWasPressed(ledP1); //Sends a message to the blue led to start fading
}//end if()
if ((digitalRead(button2) == 0) ){ //If the button is presseed and the player isn't stopped, run the code
P2Stopped = 1; //Stop player 2 from pressing again
buttonWasPressed(ledP2); //Sends a message to the green led to start fading
}//end if()
if ((digitalRead(button3) == 0)){ //If the button is presseed and the player isn't stopped, run the code
P3Stopped = 1; //Stop player 3 from pressing again
buttonWasPressed(ledP3); //Sends a message to the red led to start fading
}//end if()
if (resetInt == 1){ //if the reset button was pressed, the code runs
resetGame = 1; //resets the game
P1Stopped = 0; //Allows player 1 to press again
P2Stopped = 0; //Allows player 2 to press again
P3Stopped = 0; //Allows player 3 to press again
Serial.println("-----------RESET ---------"); //Prints "-----------RESET ---------"
}//end if()
resetGame = 0; //Stops the reset game function
}//end void loop
//**********************FUNCTOINS BELOW****************************
//*****************************************************************
// * Functoin Header *
// * NAME : buttonWasPressed *
// * INPUTS : void *
// * RETUENS : Does not return *
// * Desc : fades up and down when a player pressed the button *
//*****************************************************************
void fadeUp (int pinNumber){ //Fade up function under this code
int brightIncr = 5; //Variable for brightness incroment
int level = 0 ; //Variable for brightness level
while (level <= 225){ //loops while the brightness level is less than 225
analogWrite (pinNumber, level); //runs the functioonn for the chosen led
level = level + brightIncr; //Adds 5 to the brightness level (0,5,10,15)
delay (10); //delay by 10 ml second
}//end while()
}//end fade up
void fadeDown (int pinNumber){ //Fade up function under this code
int brightIncr = 5; //Variable for brightness incroment
int level = 225 ; //Variable for brightness level
while (level >= 0){ //loops while the brightness level is more than 0
analogWrite (pinNumber, level); //runs the functioonn for the chosen led
level = level - brightIncr; //Subtracts 5 to the brightness level (225,220,215,210)
delay (10); //delay by 10 ml second
}//end while()
}//end fadeDown()
void buttonWasPressed (int playerLight){ //Function for pressing the button
tone (speaker, 1000, 100); //The speaker (pin 8) will make a sound of 1000 Hz for 100 ml seconds
delay (100); //Delays for 100 ml seconds
while ( continueGame == 1 && resetGame == 0) { //Keeps looping while continue game and reset game buttons are not pressed
fadeUp (playerLight); //Runs the function(fadeUp) on the chosen LED
fadeDown (playerLight); //Runs the function(fadeDown) on the chosen LED
} //end while()
analogWrite(playerLight, 255); //Makes the chosen LED light up by 10
continueGame = 1; //sets continue game to 0
}//End buttonWasPressed
//*****************************************************************
//*****************************************************************
//************* ALL INTERRUPT SERVICE ROUTINE BELOW **************
//*****************************************************************
//*****************************************************************
//*****************************************************************
// * Functoin Header *
// * NAME : Reset and continue *
// * INPUTS : void *
// * RETUENS : Does not return *
// * Desc : turns off all the lights if reset. *
//*****************************************************************
void rstInt(){ //an inturrupt to reset everything in the game
P1Stopped = 0; //makes player 1 button work if you press it
P2Stopped = 0; //makes player 2 button work if you press it
P3Stopped = 0; //makes player 3 button work if you press it
analogWrite(ledP1, 0); //turn player 1's LED off
analogWrite(ledP2, 0); //turn player 2's LED off
analogWrite(ledP3, 0); //turn player 3's LED off
resetGame = 0; //stops the game from reseting
}// end rstInt()
void contInt(){ //Inturrupt to continue the game
continueGame = 0; //makes the game continue
} //end contInt