//**********************************************************
//**********************************************************
//** **
//** NAME: Terrance Hem **
//** DATE: 03/27/24 **
//** PROG: THjaseyBox.ino **
//** DESC: creating a game show using interrupts **
//** where players will push buttons and the LEDs **
//** will react based on the timing of the game **
//** **
//**********************************************************
//**********************************************************
const int button1 = 7; // connects button 1 (blue) to pin 7
const int button2 = 5; // connects button 5 (yellow) to pin 5
const int button3 = 4; // connects button 3 (white) to pin 4
const int P1Led = 6; // connects blue led to pin 6
const int P2Led = 10; // connects yellow led to pin 10
const int P3Led = 9; // connects white led to pin 9
int whenPlayerStopped = 0; // defines whenPlayerStopped as a variable
const int speaker = 8; // connects the speaker to pin 8
const int resetInt = 3; // connects the reset button to pin 3
const int continueInt = 2; // connects the continue/start button to pin 2
int continueGame = 0; // defines continueGame as a variable
int resetGame = 0; // defines resetGame as a variable
int P1Stopped = 0; // defines P1Stopped as a variable
int P2Stopped = 0; // defines P2Stopped as a variable
int P3Stopped = 0; // defines P3Stopped as a variable
void setup() {
pinMode (P1Led, OUTPUT); // defines the blue led as an output
pinMode (P2Led, OUTPUT); // defines the yellow led as an output
pinMode (P2Led, OUTPUT); // defines the white led as an output
pinMode(button1, INPUT_PULLUP); // sets the blue button as input pullup
pinMode(button2, INPUT_PULLUP); // sets the yellow button as input pullup
pinMode(button3, INPUT_PULLUP); // sets the white button as input pullup
pinMode( continueInt, INPUT_PULLUP); // sets continueInt as an input pullup
attachInterrupt(digitalPinToInterrupt(continueInt),contInt, FALLING); // adds an interrupt to continueInt
pinMode( resetInt,INPUT_PULLUP); // sets resetInt as an input pullup
attachInterrupt(digitalPinToInterrupt(resetInt),rstInt, FALLING); // adds an interrupt to resetInt
}// end setup()
void loop() {
if (resetGame == 1){ // do the code below if resetGame equals 1
resetGame = 0; // set resetGame to 0
analogWrite(P1Led,0); // set P1Led to 0 voltage
analogWrite(P2Led,0); // set P2Led to 0 voltage
analogWrite(P3Led,0); // set P3Led to 0 voltage
}// end if()
if (digitalRead(button1) == 0 && P1Stopped == 0){ // do the code below if button1 equals 0 and P1Stopped equals 0
P1Stopped =1; // set P1Stopped to 1
buttonWasPressed (P1Led); // run a buttonWasPressed action for P1Led
}// end if()
if (digitalRead(button2) == 0 && P2Stopped == 0){ // do the code below if button2 equals 0 and P2Stopped equals 0
P2Stopped =1; // set P2Stopped to 1
buttonWasPressed (P2Led); // run a buttonWasPressed action for P2Led
}// end if()
if (digitalRead(button3) == 0 && P3Stopped == 0){ // do the code below if button3 equals 0 and P3Stopped equals 0
P3Stopped =1; // set P3Stopped to 1
buttonWasPressed (P3Led); // run a buttonWasPressed action for P3Led
}//end if()
}// end loop()
//*******************************************
//** Name: buttonWasPressed **
//** Input: ledToLightUp **
//** Output: **
//** Desc: runs a while loop for buttons **
//** that were pressed **
//*******************************************
void buttonWasPressed(int ledToLightUp){ // use buttonWasPressed action for new integer ledToLightup
while (continueGame == 0 && resetGame == 0){ // run code below while continueGame and resetGame is 0
fadeDown(ledToLightUp); // creates action fadeDown for integer
fadeUp(ledToLightUp); // creates action FadeUp for integer
}// end while()]
analogWrite(ledToLightUp ,5); // sets ledToLightUp voltage to 5
continueGame = 0; // sets continueGame to 0
}// end buttonWasPressed()
//***********************************************
//** Name: fadeUp **
//** Inputs: pinNumber **
//** Outputs: **
//** Desc: gradually increases led brightness **
//***********************************************
void fadeUp (int pinNumber){ // use fadeUp action for new integer pinNumber
int brightnessIncrement = 5; // creates integer brightnessIncrement and sets to 5
int level = 0; // creates integer level and sets to 0
while ( level <= 255 ){ // run code below while level is less than or equal to 255
analogWrite (pinNumber , level); // sets pinNumber voltage to change to what level is
level = level + brightnessIncrement; // level equals to level + brightnessIncrement
delay (100); // sets a delay of 10ms
}// end while()
}// end fadeUP()
//***********************************************
//** Name: fadeDown **
//** Inputs: pinNumber **
//** Outputs: **
//** Desc: gradually decreases led brightness **
//***********************************************
void fadeDown(int PinNumber){ // use fadeDown action for integer pinNumber
int brightnessIncrement = 5; // creates integer brightnessIncrement and sets to 5
int level = 255; // creates integer level and sets to 255
while ( level >=0 ){ // run code below while level is more than or equal to 0
analogWrite (PinNumber , level); // sets pinNumber voltage to change to what level is
level = level - brightnessIncrement; // level equals to level - brightnessIncrement
delay (100); // sets a delay of 10ms
}// end while()
}// end fadeDown()
//***********************************************
//** Name: contInt() **
//** Inputs: **
//** Outputs: **
//** Desc: sets integer continueGame to 1 **
//***********************************************
void contInt(){ // method contInt()
continueGame = 1; // sets continueGame to 1
}// end contInt()
//***********************************************
//** Name: rstInt() **
//** Inputs: **
//** Outputs: **
//** Desc: sets integer resetGame to 1 and sets**
//** P1Stopped, P2Stopped and P3Stopped **
//** to 0 **
//***********************************************
void rstInt(){ // method rstInt()
resetGame = 1; // sets resetGame to 1
P1Stopped = 0; // sets P1Stopped to 0
P2Stopped = 0; // sets P2Stopped to 0
P3Stopped = 0; // sets P3Stopped to 0
}// end rstInt()