//***************************************************
//* *
//*Name : Damilare Osuntola *
//*Program :Osuntola.Assignment6.ino *
//*Date :2024-04-05 *
//*Desc : A game show program with 3 buttons, 3 *
//* LED's, and 2 interrupt buttons.Through methods *
//* and interupts if a player presses their button *
//* their player LED pulses with modulation. Then *
//* if they answer correctly the red reset game but*
//* ton is pressed triggering the interupt that res*
//* ets the game. If they answer incorrectly their *
//* player Led dims and they can't answer again. *
// *
//***************************************************
// Constant interger variables for the buttons, LEDs, and buzzer
const int button1 = 7; // constant interger variable that sets pin 7 as button1
const int button2 = 5; // constant interger variable that sets pin 5 as button2
const int button3 = 4; // constant interger variable that sets pin 4 as button3
const int P1Led = 6; // constant interger variable that sets pin 6 as P1Led
const int P2Led = 10; // constant interger variable that sets pin 10 as P2Led
const int P3Led = 9; // constant interger variable that sets pin 9 as P3Led
const int buzzer =8; // constant interger variable that sets pin 8 as the buzzer
const int resetGame =3; // constant interger variable that sets pin 3 as resetGame, the button interrupt
const int continueGame =2; // constant interger variable that sets pin 2 as continueGame,the button interrupt
// Interger variables that are all set to 0
int P1Stopped = 0; // interger variable that sets P1Stopped equal to 0
int P2Stopped = 0; // interger variable that sets P2Stopped equal to 0
int P3Stopped = 0; // interger variable that sets P3Stopped equal to 0
int contInt = 0; // interger variable that sets contInt equal to 0
int rstInt = 0; // interger variable that sets contInt equal to 0
void setup() {
// put your setup code here, to run once:
// Sets all player LED's to Outputs
pinMode(P1Led, OUTPUT); // sets P1Led as an output
pinMode(P2Led, OUTPUT); // sets P2Led as an output
pinMode(P3Led, OUTPUT); // sets P3Led as an output
//Sets all player buttons to Input Pullups
pinMode(button1, INPUT_PULLUP);// sets button1 as an Input Pullup
pinMode(button2, INPUT_PULLUP);// sets button1 as an Input Pullup
pinMode(button3, INPUT_PULLUP);// sets button1 as an Input Pullup
//Sets interrupt buttons to Input pullups and as interrupt buttons
pinMode(continueGame, INPUT_PULLUP); // sets continueGame as an Input Pullup
attachInterrupt(digitalPinToInterrupt(continueGame), continueInt, FALLING); //sets pin continueGame, to trigger interrupt continueInt,whenever it's value goes from 1 to 0
pinMode(resetGame, INPUT_PULLUP); //sets resetGame as an Input Pullup
attachInterrupt(digitalPinToInterrupt(resetGame), resetInt, FALLING); //sets pin reseteGame, to trigger interrupt resetInt,whenever it's value goes from 1 to 0
}// end setup()
void loop() {
// put your main code here, to run repeatedly:
//if statement for reseting all LED's analogue value to 0
if (rstInt == 1){// if variable rstInt is equal to 1 then
rstInt = 0; // set variable rstInt equal to 0 and
analogWrite (P1Led, 0);// set P1Led analogue value equal to 0
analogWrite (P2Led, 0);// set P2Led analogue value equal to 0
analogWrite (P3Led, 0);// set P3Led analogue value equal to 0
}
//if statements for player responses
if (digitalRead(button1) == 0 && P1Stopped == 0){// if pin button1 and variable P1Stopped are both equal to 0 then
P1Stopped = 1; // set variable P1Stopped equal to 1 and
buttonWasPressed (P1Led); //use method buttonWasPressed with P1Led
}// end if()
if (digitalRead(button2) == 0 && P2Stopped == 0){// if pin button2 and variable P2Stopped are both equal to 0 then
P2Stopped = 1; // set variable P2Stopped equal to 1 and
buttonWasPressed (P2Led); // use method buttonWasPressed with P1Led
}// end if()
if (digitalRead(button3) == 0 && P3Stopped == 0){// if pin button3 and variable P3Stopped are both equal to 0 then
P3Stopped = 1; // set variable P3Stopped equal to 1 and
buttonWasPressed (P3Led); //use method buttonWasPressed with P1Led
}// end if()
}// endloop()
////////////////////////////////////////////////////
// ALL METHODS ARE BELOW //
/// ///
//// ////
////////////////////////////////////////////////////
void buttonWasPressed(int playerLed) { // defines method buttonWasPressed, aswell as defining an integer variable as player led
// while loop for Pulse With Modulation on LED
while ( contInt == 0 && rstInt== 0) { //while variables contInt and rstInt are equal to 0
fadeUp (playerLed); //use method fadeUp with playerLed
fadeDown(playerLed); //use method fadeDown with playerLed
}// end while()
analogWrite(playerLed, 5); // set playerLed to analog value 5
contInt= 0; // sets variable contInt value to 0
}// end buttonWasPressed()
void fadeUp (int pinNumber){ // method fadeUp, defines pinNumber as an interger variable
int lightIncrement = 5; // sets variable lightIncrement equal to 5
int level = 0; // sets variable lightIncrement equal to 5
//while loop for gradually increasing LED's analog value
while( level <= 255){ // while variable level is less than or equal to 255 then
analogWrite(pinNumber, level); // set variable pinNumber equal to varible level
level = level + lightIncrement; // variable level is equal to level plus lightIncrement
delay(10); // delay by 10 miliseconds
}//end while()
}// end fadeUp()
void fadeDown ( int pinNumber){ //method fadeDown, defines pinNumber as an interger variable
int lightIncrement = 5; // sets variable lightIncrement equal to 5
int level = 255; // sets variable lightIncrement equal to 255
//while loop for gradually decreasing LED's analog value
while ( level >= 0){ // while variable level is greater than or equal to 0 then
analogWrite(pinNumber, level); // set variable pinNumber equal to varible level
level = level - lightIncrement; // variable level is equal to level plus lightIncrement
delay(10); // delay by 10 miliseconds
}//end while()
}// end fadeDown()
///****************************************************\\\
//*******************************************************\\
// ALL OUR INTERRUPT HANDLING CODE BELOW \\
//*******************************************************\\
///******************************************************\\\
void continueInt(){ // interrupt continueInt
//
contInt = 1; // sets variable contInt equal to 1
}//end continueInt()
void resetInt(){// interrupt resetInt
//
rstInt = 1;// sets variable rstInt equal to 1
P1Stopped = 0; //sets P1Stopped equal to 0
P2Stopped = 0; //sets P2Stopped equal to 0
P3Stopped = 0; //sets P3Stopped equal to 0
}// end resetInt()