//******************************
// NAME: Rhys Miller *
// Program: Arduino Interupts *
// Date: April 9 2024 *
// Desc: Jasey box game *
//******************************
const int button1 = 8; // button pin for player one
const int button2 = 12; // button pin for player two
const int button3 = 13; // button pin for player three
const int P1Led = 11; // LED pin for player one
const int P2Led = 10; // LED pin for player two
const int P3Led = 9; // LED pin for player three
const int resetIntb = 2; // button pin for reset game
const int continueGameb = 3; // button pin for continue game
int continueGame = 0; // variable to track continue game method
int resetGame = 0; // variable to track reset game method
int P1Stopped = 0; // variable to track player one status
int P2Stopped = 0; // variable to track player two status
int P3Stopped = 0; // variable to track player three status
void setup() {
// set pin modes for LEDs
pinMode(P1Led, OUTPUT);
pinMode(P2Led, OUTPUT);
pinMode(P3Led, OUTPUT);
// set pin modes for player buttons
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
// set pin modes for game operator
pinMode(resetIntb, INPUT_PULLUP);
pinMode(continueGameb, INPUT_PULLUP);
// setup interupts for game control
attachInterrupt(digitalPinToInterrupt(continueGameb), continueInt, FALLING);
attachInterrupt(digitalPinToInterrupt(resetIntb), resetInt, FALLING);
} // end setup
// main loop responsible for most game function
void loop() {
if (resetGame == 1){ // reset game code
resetGame = 0;
analogWrite(P1Led, 0);
analogWrite(P2Led, 0);
analogWrite(P3Led, 0);
}
if (digitalRead(button1) == LOW && P1Stopped == 0) { // button for P1
P1Stopped = 1;
buttonWasPressed(P1Led);
}
if (digitalRead(button2) == LOW && P2Stopped == 0) { // button for P2
P2Stopped = 1;
buttonWasPressed(P2Led);
}
if (digitalRead(button3) == LOW && P3Stopped == 0) { // button for P3
P3Stopped = 1;
buttonWasPressed(P3Led);
}
} // end loop
// method used to light up the led according to which button was pressed. returns nothing
void buttonWasPressed(int ledToLightUp) {
while (continueGame == 0 && resetGame == 0) {
// fade curreent led up and down
fadeUp (ledToLightUp);
delay(5);
fadeDown (ledToLightUp);
delay(5);
}
analogWrite(ledToLightUp, 5); // Dims the LED after the loop
continueGame = 0; // Allows for another player to press a button
}
// method used to fade up led, returns nothing
void fadeUp( int pinNumber){
int level = 0;
int bright = 5 ;
while ( level <=255){
analogWrite (pinNumber,level);
level = level + bright;
delay (10);
}
}
// method used to fade down led, returns nothing
void fadeDown( int pinNumber){
int level = 255;
int bright = 5;
while (level >= 0){
analogWrite(pinNumber, level);
level = level - bright;
delay (10);
}
}
// INTERUPT HANDERLERS BELOW ******************
void continueInt() {
continueGame = 1;
}
void resetInt() {
resetGame = 1;
P1Stopped = 0;
P2Stopped = 0;
P3Stopped = 0;
}