/*
******************************************
******************************************
** Name: Liam dutot **
** Prog Name: Jasey Game **
** Date: 10/03/2024 **
** Desc: Trivia game **
** **
******************************************
******************************************
*/
// DEFINE ALL NEEDED CONSTANTS FOR CONFIGURATION
// the number of the push button
// Button pins
const int button1 = 4; // Button 1 connected to pin 4 (use INPUT_PULLUP)
const int button2 = 5; // Button 2 connected to pin 5 (use INPUT_PULLUP)
const int button3 = 7; // Button 3 connected to pin 7 (use INPUT_PULLUP)
// LED pins
const int P1Led = 9; // LED for Player 1 (use OUTPUT)
const int P2Led = 10; // LED for Player 2 (use OUTPUT)
const int P3Led = 6; // LED for Player 3 (use OUTPUT)
// Speaker pin
const int speaker = 8; // Speaker connected to pin 8 (use OUTPUT)
// Interrupt pins
const int contInt = 2; // Continue button interrupt on pin 2 (use INPUT_PULLUP)
const int resetInt = 3; // Reset button interrupt on pin 3 (use INPUT_PULLUP)
// Flags for game control
int whatPlayerPressed = 0; // Keeps track of which player pressed their button
int contGame = 0; // 0 = not pressed, 1 = pressed (for continue button)
int resetGame = 0; // 0 = not pressed, 1 = pressed (for reset button)
int P1Stopped = 0; // Player 1 status: 0 = not stopped, 1 = stopped
int P2Stopped = 0; // Player 2 status: 0 = not stopped, 1 = stopped
int P3Stopped = 0; // Player 3 status: 0 = not stopped, 1 = stopped
// Interrupt handler for continue button
void contIntHandler() {
contGame = 1; // Set the continue flag when the button is pressed
}
// Interrupt handler for reset button
void resetIntHandler() {
resetGame = 1; // Set the reset flag when the button is pressed
}
void setup() {
// Initialize buttons (with internal pull-up resistors)
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
// Initialize LEDs
pinMode(P1Led, OUTPUT);
pinMode(P2Led, OUTPUT);
pinMode(P3Led, OUTPUT);
// Initialize the speaker
pinMode(speaker, OUTPUT);
// Initialize interrupt buttons (continue and reset)
pinMode(contInt, INPUT_PULLUP);
pinMode(resetInt, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(contInt), contIntHandler, FALLING); // Attach continue interrupt
attachInterrupt(digitalPinToInterrupt(resetInt), resetIntHandler, FALLING); // Attach reset interrupt
}
void loop() {
// Check if Player 1 pressed the button and is not stopped
if (digitalRead(button1) == LOW && P1Stopped == 0) {
whatPlayerPressed = 1; // Set Player 1 as the current player
P1Stopped = P2Stopped = P3Stopped = 1; // Stop all players
analogWrite(P1Led, 25); // Light up Player 1 LED at 25% brightness
tone(speaker, 1000, 500); // Play sound for 500ms
}
// Check if Player 2 pressed the button and is not stopped
if (digitalRead(button2) == LOW && P2Stopped == 0) {
whatPlayerPressed = 2; // Set Player 2 as the current player
P1Stopped = P2Stopped = P3Stopped = 1; // Stop all players
analogWrite(P2Led, 25); // Light up Player 2 LED at 25% brightness
tone(speaker, 1000, 500); // Play sound for 500ms
}
// Check if Player 3 pressed the button and is not stopped
if (digitalRead(button3) == LOW && P3Stopped == 0) {
whatPlayerPressed = 3; // Set Player 3 as the current player
P1Stopped = P2Stopped = P3Stopped = 1; // Stop all players
analogWrite(P3Led, 25); // Light up Player 3 LED at 25% brightness
tone(speaker, 1000, 500); // Play sound for 500ms
}
// Handle reset button press
if (digitalRead(resetInt) == LOW && resetGame == 1) {
resetGame = 0; // Reset the reset flag
// Turn off all LEDs
digitalWrite(P1Led, LOW);
digitalWrite(P2Led, LOW);
digitalWrite(P3Led, LOW);
// Reset player status flags
P1Stopped = P2Stopped = P3Stopped = 0;
whatPlayerPressed = 0;
}
// Handle continue button press
if (contGame == 1) {
contGame = 0; // Reset continue flag
// Allow other players to press buttons based on who pressed first
if (whatPlayerPressed == 1) {
P2Stopped = P3Stopped = 0; // Only Player 1 is stopped
} else if (whatPlayerPressed == 2) {
P1Stopped = P3Stopped = 0; // Only Player 2 is stopped
} else if (whatPlayerPressed == 3) {
P1Stopped = P2Stopped = 0; // Only Player 3 is stopped
}
}
}