/*
*****************************************************************************************************
*****************************************************************************************************
** Name: Liam dutot **
** Project Name: Jasey game **
** Date:2024-10-07 **
** Description: A game with three players, each player has an assigned LED. When a player **
** presses their color coded button, their LED lights up, and the other players can't press thei **
** buttons until the "continue" button is pressed. the "reset" button restarts the game **
** turning off all LEDs. **
*****************************************************************************************************
*****************************************************************************************************
*/
const int button1 = 4; // button 1 connected to pin 4
const int button2 = 5; // button 2 connected to pin 5
const int button3 = 7; // button 3 connected to pin 7
const int P1Led = 9; // LED for P1
const int P2Led = 10; // LED for P2
const int P3Led = 6; // LED for P3
const int speaker = 8; // Speaker connected to pin 8
const int contInt = 2; // Cont button interrupt on pin 2
const int resetInt = 3; // Rst button interrupt on pin 3
int contGame = 0; // 0 = not pressed, 1 = pressed
int resetGame = 0; // 0 = not pressed, 1 = pressed
int P1Stopped = 0; // P1 status: 0 = not stopped, 1 = stopped
int P2Stopped = 0; // P2 status: 0 = not stopped, 1 = stopped
int P3Stopped = 0; // P3 status: 0 = not stopped, 1 = stopped
// Interrupt handler for continue button
void contInterrupt (){
tone(speaker, 1000, 500); //plays sound for 500ms
contGame = 1; //cont button pressed
}
void resetInterrupt() {
tone(speaker, 1000, 500); //plays sound for 500ms
resetGame = 1;
P1Stopped = 0; //p1 status stopped
P2Stopped = 0; //p2 status stopped
P3Stopped = 0; //p3 status stopped
}
void setup() {
// Initialize buttons
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
// Initialize LEDs
pinMode(P1Led, OUTPUT);
pinMode(P2Led, OUTPUT);
pinMode(P3Led, OUTPUT);
// Initialize speaker
pinMode(speaker, OUTPUT);
// Initialize interrupt buttons
pinMode(contInt, INPUT_PULLUP);
pinMode(resetInt, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(contInt), contInterrupt, FALLING); // Attach cont interrupt
attachInterrupt(digitalPinToInterrupt(resetInt), resetInterrupt, FALLING); // Attach rst interrupt
}
void loop() {
// Check if P1 pressed the button and is not stopped
if (digitalRead(button1) == 0 && P1Stopped == 0) {
P1Stopped = 1; //block p1 from doing anything
buttPress(P1Led);
}//end if()
// Check if P2 pressed the button and is not stopped
if (digitalRead(button2) == 0 && P2Stopped == 0) {
P2Stopped = 1; // Stop all players
buttPress(P2Led);
}//end if()
// Check if P3 pressed the button and is not stopped
if (digitalRead(button3) == 0 && P3Stopped == 0) {
P3Stopped = 1; // Stop all players
buttPress(P3Led);
}//end if()
// Handle cont button press
if (contGame == 1) {
contGame = 0; // Rst continue flag
}//end if()
if(resetGame == 1) {
resetGame = 0;
analogWrite(P1Led, LOW);
analogWrite(P2Led, LOW);
analogWrite(P3Led, LOW);
}//end if()
}
void buttPress(int ledNumber) {
while( contGame == 0 && resetGame == 0 ) {
fadeUp (ledNumber);
fadeDown(ledNumber);
}
analogWrite(ledNumber ,20);
contGame = 0;
}
void fadeUp(int LedPin) {
int increment = 5; // Brightness increment
int level = 0; // Initial brightness level
// Fade up loop
while (level <= 255) {
analogWrite(LedPin, level);
level = level + increment;
delay(10);
}
}
void fadeDown(int LedPin) {
int increment = 5; // Brightness decrement
int level = 255; // Start at maximum brightness
// Fade down loop
while (level >= 0) {
analogWrite(LedPin, level); // Write the current brightness level to the pin
level = level - increment; // Decrease the brightness by the increment
delay(10); // Add a small delay to slow down the fade effect
}
}