Arduino Reaction Time Tester

25,789

20

22

Posted

Introduction: Arduino Reaction Time Tester

This is a simple reaction time tester. It will randomly turn on a L.E.D. and measure the time it takes you to press a button and then send the measurement to your computer.

Step 1: Materials

Step 2: Making the Circuit

Start by connecting one leg of the button to the positive rail on the bread board. Next, add the 10K resistor between the other leg and ground. Then, add a led between pin 13 on the arduino and ground.   After that, connect the negative rail on the bread board to the ground on the arduino and the positive rail to 5V.  Finally, connect the leg of the push button with the 10K resistor to pin two on the arduino.

Step 3: Upload the Sketch

Copy and paste the sketch into arduino IDE and upload to the arduino.

int switchPin = 2;
int ledPin = 13 ;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean Started = false;
boolean timer = false;
long startTime;
long endTime;
long randomTime;
float elapsedTime;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if(last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}


void loop()
{
  currentButton = debounce(lastButton);
  if(lastButton == LOW && currentButton == HIGH)
  {
    Started = !Started;
    lastButton = HIGH;
  }
  lastButton = currentButton;
  if(Started == true && timer == false)
  {
    Random();
    timer = true;
  }
  if(Started == false && timer == true)
  {
    Stop();
    timer = false;
  }
 
}
void Random()
{
  randomTime = random(4,10);
  randomTime = randomTime*1000;
 
  digitalWrite(ledPin, HIGH);
  delay(100);
  digitalWrite(ledPin, LOW);
  delay(randomTime);
  Start();
}


void Start(){
  startTime = millis();
  digitalWrite(ledPin, HIGH);
}

void Stop(){
  endTime = millis();
  elapsedTime = (endTime - startTime)+5;
  elapsedTime = elapsedTime/1000;
  Serial.print("Time Seconds: ");
  Serial.println(elapsedTime);
  digitalWrite(ledPin, LOW);
   
}

Step 4: Test

To use the reaction time tester, plug the arduino into your computer and open the serial monitor.  Next, press the button, the L.E.D. should blink once, four to ten seconds later the L.E.D. should turn on. Finally, press the button and the elapsed time from the L.E.D. being turned on to the button being pressed will be displayed on the serial monitor.

Share

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • Gluten Free Challenge

    Gluten Free Challenge
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

22 Comments

Great code!

I've adapted your code to do something more complex.

The problem I've found is that I cannot figure out how to program it to print a message if a certain amount of time has passed and the user didn't pushed the button AFTER the game has started.

Here's my adapted code:

http://pastebin.com/kMsBkVyj

user

elseif ((millis()- beginTime)< (randomSeconds + maxTimeAfterStart)){

serial.println("message")

}

Have you tried something like this

Nice job commenting the code, I always forget

That seems to work, kinda!!

http://pastebin.com/NADJMRjm

Now the problem is the following:

When the maximum time has elapsed, it doesn't return to the same point as if there was a correct or early answer, which is wait for the next time the user presses the button. It keeps looping game after game. Any ideas?

user

if ((millis() - beginTime) > (randomSeconds + maxTimer) && gameStarted == true)

This works, I tested it out this time.

Could you send your code? Doesn't seem to work in my sketch. Where did you put that "if" statement?

user

if ((millis() - beginTime) > (randomSeconds + maxTimer) && gameStarted == true) {

Stop();

//resets game:

gameStarted = false;

timerFlag = false;

currentButton = LOW;

lastButton = LOW;

Blink(2);

Serial.println("message");

}

user

full code here http://pastebin.com/f7rtnkDL

Thanks so much for uploading the code. I see now. Maybe I wasn't clear (sorry, english is not my native language) but the problem is the following.

Let's look at the serial output

9090 <- I pressed the button, gives 9090 (random timer initialized and waiting for reaction on led lightup)
1010 <- I pressed the button early, so it gives code 1010 (early response). The game PAUSES infinitely until I press the button again (which is the appropriate behavior)

9090 <-pressed the button again, but this time I will not press any button to test the timeout
Timed out <-success! it timed out and didn't output the reaction time
message <- your suggestion has been executed
9090 <- ***HERE***, the game starts, without waiting for my response! (which is inappropriate).
Timed out <- it will continue to loop these 3 messages up to infinity
message
9090

So, that was the problem I need to solve.

user

Do you still get that error with the code I uploaded ?

I tested out your code and got the error you are describing but the code that I uploaded fixed that for me

very weird. after a timeout it stays idle, and does not print "9090" at all?

in case i got something wrong by copy/pasteing it, i've downloaded it and ran it and it's the same. after a timeout returns to "9090" and keeps looping endlessly.