Components and supplies
Male/Male Jumper Wires
Pushbutton switch 12mm
LilyPad LED Blue (5pcs)
blue led
Arduino UNO
5 mm LED: Green
Resistor 220 ohm
Big Red Dome Button
Apps and platforms
Arduino IDE
Project description
Code
the code
arduino
just open it.
1/****************************************************************** 2 * GAME 3 * 2 players 4 * reaction time 5 * Version 1.0 11/11/2018 By Lotte and Arjan 6/*****************************************************************/ 7 8const int switchPin1 = 10; //Player 1 button 9const int switchPin2 = 8; //Player 2 button 10 11int switchStart = 12; //Start button 12int ledPin = 11 ; //Game start LED 13int ledPinP1 = 9 ; //Player 1 win indicator 14int ledPinP2 = 7 ; //Player 2 win indicator 15int buttonStateP1 = 0; //Player 1 button tracking 16int buttonStateP2 = 0; //Player 2 button tracking 17int lastButtonStateP1 = 0; // Player 1 last button state (used to detect a button press) 18int lastButtonStateP2 = 0; // Player 2 last button state (used to detect a button press) 19int scoreP1 = 0; // Player 1 score tracking 20int scoreP2 = 0; // Player 2 score tracking 21 22boolean bothDone = false; // Used to see if both players have pressed their buttons 23boolean gameOn = false; // Keep track if the game is going on or not 24boolean startButtonState = LOW; // Start button initalization 25boolean p1Done = false; // Keep track of Player 1's button press 26boolean p2Done = false; // Keep track of Player 1's button press 27boolean welcomeMsg = false; // Keep track if the welcome message has already been displayed 28 29long randomTime; // Hold the random time between the start of the game and the indicator light coming back on 30long startTime; // When did the game start 31long endTimeP1; // When did Player 1 press their button 32long endTimeP2; // When did Player 2 press their button 33 34float finalTimeP1; // Time elapsed between start of the game and Player 1 pressing their button 35float finalTimeP2; // Time elapsed between start of the game and Player 2 pressing their button 36float winningTime; // Time between the winning and losing player's time 37 38void setup() 39{ 40 pinMode(switchPin1, INPUT); 41 pinMode(switchPin2, INPUT); 42 pinMode(ledPin, OUTPUT); // Game start LED 43 pinMode(ledPinP1, OUTPUT); // Player 1 win LED 44 pinMode(ledPinP2, OUTPUT); // Player 1 win LED 45 Serial.begin(9600); 46} 47 48void loop(){ 49 // Print a welcome message, the current player's score, and set that the welcome message has been displayed 50 if (welcomeMsg == false){ 51 Serial.println(" "); 52 Serial.println("Press the start button to begin"); 53 Serial.println("Scoreboard:"); 54 Serial.print("Player 1 - "); 55 Serial.println(scoreP1); 56 Serial.print("Player 2 - "); 57 Serial.println(scoreP2); 58 welcomeMsg = true; 59 } 60 startButtonState = digitalRead(switchStart); // Listen for the start button to be pressed 61 62 // if the start button has been pressed and there is no game already running, begin the game 63 if (startButtonState == HIGH && gameOn == false){ 64 Random(); 65 } 66} 67 68// Generate a random ammount of time to delay between the begining of the game intil the LED comes back on 69void Random(){ 70 Serial.println(" "); 71 Serial.println("Get ready!"); 72 randomTime = random(4,10); 73 randomTime = randomTime*1000; 74 digitalWrite(ledPin, HIGH); // Turn the game LED on and off to indicate a game is about to start 75 delay(1000); 76 digitalWrite(ledPin, LOW); 77 delay(randomTime); 78 startGame(); 79} 80 81// Listen for the Player 1 and Player 2 buttons to be pressed 82void startGame(){ 83 gameOn = true; // Declare a game currently running 84 startTime = millis(); 85 digitalWrite(ledPin, HIGH); // Turn on game LED indicating players should press their buttons as quickly as possible 86 while(p1Done == false || p2Done == false){ 87 buttonStateP1 = digitalRead(switchPin1); 88 buttonStateP2 = digitalRead(switchPin2); 89 // Listen for Player 1 button to be pressed and set Player 1 as done. 90 if (buttonStateP1 == HIGH && p1Done == false) { 91 endTimeP1 = millis(); 92 p1Done = true; 93 } 94 // Listen for Player 2 button to be pressed and set Player 2 as done. 95 if (buttonStateP2 == HIGH && p2Done == false) { 96 endTimeP2 = millis(); 97 p2Done = true; 98 } 99 } 100 endGame(); 101} 102 103void endGame(){ 104 digitalWrite(ledPin, LOW); // Turn off the game LED 105 finalTimeP1 = (endTimeP1 - startTime); //Calculate how long it took Player to push their button 106 finalTimeP2 = (endTimeP2 - startTime); //Calculate how long it took Player to push their button 107 Serial.print("P1 time:"); 108 Serial.println(finalTimeP1/1000); // Display Player 1's final time in seconds 109 Serial.print("P2 time:"); 110 Serial.println(finalTimeP2/1000); // Display Player 2's final time in seconds 111 112 if (endTimeP1 < endTimeP2){ // Run if Player 1 won the round 113 digitalWrite(ledPinP1, HIGH); 114 winningTime = (endTimeP2 - startTime) - (endTimeP1 - startTime); 115 scoreP1 = scoreP1 + 1; 116 Serial.print("P1 won by:"); 117 Serial.println(winningTime/1000); 118 digitalWrite(ledPinP1, HIGH); 119 digitalWrite(ledPinP2, LOW); 120 } 121 else{ 122 Serial.print("P2 won by:"); // Run if Player 2 won te round 123 winningTime = (endTimeP1 - startTime) - (endTimeP2 - startTime); 124 scoreP2 = scoreP2 + 1; 125 Serial.println(winningTime/1000); 126 digitalWrite(ledPinP2, HIGH); 127 digitalWrite(ledPinP1, LOW); 128 } 129 delay(5000); 130 digitalWrite(ledPinP1, LOW); // Turn of Player 1's LED 131 digitalWrite(ledPinP2, LOW); // Turn of Player 2's LED 132 133 // Reset all variables to restart the game 134 buttonStateP1 = 0; 135 buttonStateP2 = 0; 136 lastButtonStateP1 = 0; 137 lastButtonStateP2 = 0; 138 bothDone = false; 139 gameOn = false; 140 startButtonState = LOW; 141 p1Done = false; 142 p2Done = false; 143 randomTime = 0; 144 startTime = 0; 145 endTimeP1 = 0; 146 endTimeP2 = 0; 147 finalTimeP1 = 0; 148 finalTimeP2 = 0; 149 winningTime = 0; 150 welcomeMsg = false; 151 152} 153
the code
arduino
just open it.
1/****************************************************************** 2 * GAME 3 * 2 players 4 * reaction time 5 * Version 1.0 11/11/2018 By Lotte and Arjan 6/*****************************************************************/ 7 8const int switchPin1 = 10; //Player 1 button 9const int switchPin2 = 8; //Player 2 button 10 11int switchStart = 12; //Start button 12int ledPin = 11 ; //Game start LED 13int ledPinP1 = 9 ; //Player 1 win indicator 14int ledPinP2 = 7 ; //Player 2 win indicator 15int buttonStateP1 = 0; //Player 1 button tracking 16int buttonStateP2 = 0; //Player 2 button tracking 17int lastButtonStateP1 = 0; // Player 1 last button state (used to detect a button press) 18int lastButtonStateP2 = 0; // Player 2 last button state (used to detect a button press) 19int scoreP1 = 0; // Player 1 score tracking 20int scoreP2 = 0; // Player 2 score tracking 21 22boolean bothDone = false; // Used to see if both players have pressed their buttons 23boolean gameOn = false; // Keep track if the game is going on or not 24boolean startButtonState = LOW; // Start button initalization 25boolean p1Done = false; // Keep track of Player 1's button press 26boolean p2Done = false; // Keep track of Player 1's button press 27boolean welcomeMsg = false; // Keep track if the welcome message has already been displayed 28 29long randomTime; // Hold the random time between the start of the game and the indicator light coming back on 30long startTime; // When did the game start 31long endTimeP1; // When did Player 1 press their button 32long endTimeP2; // When did Player 2 press their button 33 34float finalTimeP1; // Time elapsed between start of the game and Player 1 pressing their button 35float finalTimeP2; // Time elapsed between start of the game and Player 2 pressing their button 36float winningTime; // Time between the winning and losing player's time 37 38void setup() 39{ 40 pinMode(switchPin1, INPUT); 41 pinMode(switchPin2, INPUT); 42 pinMode(ledPin, OUTPUT); // Game start LED 43 pinMode(ledPinP1, OUTPUT); // Player 1 win LED 44 pinMode(ledPinP2, OUTPUT); // Player 1 win LED 45 Serial.begin(9600); 46} 47 48void loop(){ 49 // Print a welcome message, the current player's score, and set that the welcome message has been displayed 50 if (welcomeMsg == false){ 51 Serial.println(" "); 52 Serial.println("Press the start button to begin"); 53 Serial.println("Scoreboard:"); 54 Serial.print("Player 1 - "); 55 Serial.println(scoreP1); 56 Serial.print("Player 2 - "); 57 Serial.println(scoreP2); 58 welcomeMsg = true; 59 } 60 startButtonState = digitalRead(switchStart); // Listen for the start button to be pressed 61 62 // if the start button has been pressed and there is no game already running, begin the game 63 if (startButtonState == HIGH && gameOn == false){ 64 Random(); 65 } 66} 67 68// Generate a random ammount of time to delay between the begining of the game intil the LED comes back on 69void Random(){ 70 Serial.println(" "); 71 Serial.println("Get ready!"); 72 randomTime = random(4,10); 73 randomTime = randomTime*1000; 74 digitalWrite(ledPin, HIGH); // Turn the game LED on and off to indicate a game is about to start 75 delay(1000); 76 digitalWrite(ledPin, LOW); 77 delay(randomTime); 78 startGame(); 79} 80 81// Listen for the Player 1 and Player 2 buttons to be pressed 82void startGame(){ 83 gameOn = true; // Declare a game currently running 84 startTime = millis(); 85 digitalWrite(ledPin, HIGH); // Turn on game LED indicating players should press their buttons as quickly as possible 86 while(p1Done == false || p2Done == false){ 87 buttonStateP1 = digitalRead(switchPin1); 88 buttonStateP2 = digitalRead(switchPin2); 89 // Listen for Player 1 button to be pressed and set Player 1 as done. 90 if (buttonStateP1 == HIGH && p1Done == false) { 91 endTimeP1 = millis(); 92 p1Done = true; 93 } 94 // Listen for Player 2 button to be pressed and set Player 2 as done. 95 if (buttonStateP2 == HIGH && p2Done == false) { 96 endTimeP2 = millis(); 97 p2Done = true; 98 } 99 } 100 endGame(); 101} 102 103void endGame(){ 104 digitalWrite(ledPin, LOW); // Turn off the game LED 105 finalTimeP1 = (endTimeP1 - startTime); //Calculate how long it took Player to push their button 106 finalTimeP2 = (endTimeP2 - startTime); //Calculate how long it took Player to push their button 107 Serial.print("P1 time:"); 108 Serial.println(finalTimeP1/1000); // Display Player 1's final time in seconds 109 Serial.print("P2 time:"); 110 Serial.println(finalTimeP2/1000); // Display Player 2's final time in seconds 111 112 if (endTimeP1 < endTimeP2){ // Run if Player 1 won the round 113 digitalWrite(ledPinP1, HIGH); 114 winningTime = (endTimeP2 - startTime) - (endTimeP1 - startTime); 115 scoreP1 = scoreP1 + 1; 116 Serial.print("P1 won by:"); 117 Serial.println(winningTime/1000); 118 digitalWrite(ledPinP1, HIGH); 119 digitalWrite(ledPinP2, LOW); 120 } 121 else{ 122 Serial.print("P2 won by:"); // Run if Player 2 won te round 123 winningTime = (endTimeP1 - startTime) - (endTimeP2 - startTime); 124 scoreP2 = scoreP2 + 1; 125 Serial.println(winningTime/1000); 126 digitalWrite(ledPinP2, HIGH); 127 digitalWrite(ledPinP1, LOW); 128 } 129 delay(5000); 130 digitalWrite(ledPinP1, LOW); // Turn of Player 1's LED 131 digitalWrite(ledPinP2, LOW); // Turn of Player 2's LED 132 133 // Reset all variables to restart the game 134 buttonStateP1 = 0; 135 buttonStateP2 = 0; 136 lastButtonStateP1 = 0; 137 lastButtonStateP2 = 0; 138 bothDone = false; 139 gameOn = false; 140 startButtonState = LOW; 141 p1Done = false; 142 p2Done = false; 143 randomTime = 0; 144 startTime = 0; 145 endTimeP1 = 0; 146 endTimeP2 = 0; 147 finalTimeP1 = 0; 148 finalTimeP2 = 0; 149 winningTime = 0; 150 welcomeMsg = false; 151 152} 153
Downloadable files
the scematics
the scematics
Comments
Only logged in users can leave comments
icecreambear
0 Followers
•0 Projects
Table of contents
Intro
3
0