Components and supplies
Arduino Starter Kit
Arduino UNO
Pushbutton switch 12mm
Piezo buzzer
Apps and platforms
Arduino IDE
Project description
Code
ReactionGame.ino
c_cpp
Just paste the code into your Arduino IDE, and you're ready to go.
1int tonefreq = 3000; // adjusts your frequency for the tone() command 2int untilpress; 3int reset; 4int Start; 5 6unsigned long now; 7unsigned long soon; 8unsigned long react; 9unsigned long react2; 10 11int playerone = 3; // adjusts your pinouts 12int playertwo = 4; 13//int playerthree = 6; // if you want to play with more players, then you have to uncomment the to slashes before playerthree, but then you have to correct more lines in the code 14int resetPin = 5; 15int outputPin = 10; 16 17int upperWaitingTime = 3000; // changes waiting time in milliseconds 18int lowerWaitingTime = 7000; 19 20void setup() { 21 pinMode(outputPin,OUTPUT); 22 pinMode(playerone,INPUT_PULLUP); 23 pinMode(playertwo,INPUT_PULLUP); 24 pinMode(resetPin,INPUT_PULLUP); 25 Serial.begin(9600); 26 untilpress = 0; 27 28 Serial.print("Press button "); 29 Serial.print(resetPin); 30 Serial.println(" to start."); 31 32 while(digitalRead(resetPin) == HIGH){ 33 34 } 35 now = millis(); 36 soon = now + random(3000,7000); 37 38} 39 40 41 42void loop() { 43 while(Start == 0){ 44 Serial.println("start"); 45 Start++; 46 Serial.println("_________________________________________"); 47 } 48 49 if(digitalRead(playerone) == LOW || digitalRead(playertwo) == LOW) { // losing detection 50 if(digitalRead(playerone) == LOW) { 51 Serial.println("Left loses."); 52 } 53 54 if(digitalRead(playertwo) == LOW){ 55 Serial.println("Right loses."); 56 57/* if(digitalRead(playerthree) == LOW) { //if you want three players, then you can just delete the block comment syntax 58 Serial.println("However you want to call it loses."); 59 } 60*/ 61 } 62 63 while(digitalRead(playerone) == LOW || digitalRead(playertwo) == LOW/* || digitalRead(playerthree) == LOW*/) { // if lost, then reset the timer, and here, too. Just delete the block comment syntax 64 now = millis(); 65 soon = now + random(3000,7000); 66 } 67 } 68 69 70 71 if(soon == millis()){ // tone 72 tone(outputPin,tonefreq); 73 react = millis(); 74 75 while(untilpress == 0){ 76 77 if(digitalRead(playerone) == LOW) { // loop until one player presses the button 78 Serial.println("Left won."); 79 untilpress++; 80 Serial.print("Reaction time:"); 81 react2 = millis() - react; 82 Serial.print(react2); 83 Serial.println("ms"); 84 } 85 86 87 if(digitalRead(playertwo) == LOW){ 88 Serial.println("Right has won."); 89 untilpress++; 90 Serial.print("Reaction time:"); 91 react2 = millis() - react; 92 Serial.print(react2); 93 Serial.println("ms"); 94 } 95 96 97/* if(digitalRead(playerthree) == LOW){ //if you want three players, then you can just delete the block comment syntax 98 Serial.println("However you want to call it has won."); 99 untilpress++; 100 Serial.print("Reaction time:"); 101 react2 = millis() - react; 102 Serial.print(react2); 103 Serial.println("ms"); 104 } 105*/ 106 } 107 108 109 //reset after someone has won 110 while(digitalRead(resetPin) == HIGH) { 111 112 Start = 0; 113 noTone(outputPin); 114 now = millis(); 115 soon = now + random(3000,7000); 116 117 while(reset == 0){ 118 Serial.println("_________________________________"); 119 Serial.print("Press button "); 120 Serial.print(resetPin); 121 Serial.println(" to restart."); 122 reset++; 123 124 } 125 } 126 } 127 128 untilpress = 0; 129 reset = 0; 130} 131
Comments
Only logged in users can leave comments