Westpol
Published

Reaction Time Game

This game works with sound and is made for two people. The code can easily be expanded for more people.

IntermediateShowcase (no instructions)7,494
Reaction Time Game

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×3
Arduino Piezo buzzer
Couldn't find single ones.
×1
Andium Arduino Starter Kit
Not necessary, but all used parts are included, so you wouldn't have to buy all the other components up in the list.
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

ReactionGame.ino

C/C++
Just paste the code into your Arduino IDE, and you're ready to go.
int tonefreq = 3000;      // adjusts your frequency for the tone() command
int untilpress;
int reset;
int Start;

unsigned long now;
unsigned long soon;
unsigned long react;
unsigned long react2; 

int playerone = 3;        // adjusts your pinouts
int playertwo = 4;
//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
int resetPin = 5;
int outputPin = 10;

int upperWaitingTime = 3000;      // changes waiting time in milliseconds
int lowerWaitingTime = 7000;

void setup() {
  pinMode(outputPin,OUTPUT);
  pinMode(playerone,INPUT_PULLUP);
  pinMode(playertwo,INPUT_PULLUP);
  pinMode(resetPin,INPUT_PULLUP);
  Serial.begin(9600);
  untilpress = 0;
  
  Serial.print("Press button ");
  Serial.print(resetPin);
  Serial.println(" to start.");
  
  while(digitalRead(resetPin) == HIGH){
    
  }
  now = millis();
  soon = now + random(3000,7000);
  
}



void loop() {
  while(Start == 0){
    Serial.println("start");
    Start++;
    Serial.println("_________________________________________");
  }
  
  if(digitalRead(playerone) == LOW || digitalRead(playertwo) == LOW) {      // losing detection
  if(digitalRead(playerone) == LOW) {
  Serial.println("Left loses.");
  }

  if(digitalRead(playertwo) == LOW){
  Serial.println("Right loses.");
  
/*  if(digitalRead(playerthree) == LOW) {                             //if you want three players, then you can just delete the block comment syntax
  Serial.println("However you want to call it loses.");  
  }
*/               
  }
  
  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 
  now = millis();  
  soon = now + random(3000,7000);
  }                                             
  }


  
  if(soon == millis()){                       // tone
    tone(outputPin,tonefreq);
    react = millis();
    
  while(untilpress == 0){
    
  if(digitalRead(playerone) == LOW) {                   // loop until one player presses the button
    Serial.println("Left won.");
    untilpress++;
    Serial.print("Reaction time:");
    react2 = millis() - react;
    Serial.print(react2);
    Serial.println("ms");
  }

  
  if(digitalRead(playertwo) == LOW){
   Serial.println("Right has won.");
   untilpress++;
   Serial.print("Reaction time:");
   react2 = millis() - react;
   Serial.print(react2);
   Serial.println("ms");
  }

  
/*    if(digitalRead(playerthree) == LOW){            //if you want three players, then you can just delete the block comment syntax
   Serial.println("However you want to call it has won.");
   untilpress++;
   Serial.print("Reaction time:");
   react2 = millis() - react;
   Serial.print(react2);
   Serial.println("ms");
  }
*/
  }


                                                //reset after someone has won
  while(digitalRead(resetPin) == HIGH) {
    
  Start = 0;
  noTone(outputPin);
  now = millis();  
  soon = now + random(3000,7000);
  
  while(reset == 0){
    Serial.println("_________________________________");
  Serial.print("Press button ");
  Serial.print(resetPin);
  Serial.println(" to restart.");
    reset++;
    
  }
  }
  }

  untilpress = 0;
  reset = 0;
}

Credits

Westpol

Westpol

8 projects • 22 followers

Comments