Arduino Reaction Time Tester

36K2224

Intro: 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.

23 Comments

That's amazing. A great work. Do you think that it could be used to screen or assess degree of substance abuse?

too many things affect reaction time, chances are that it wouldn't be that great of an analysis tool for that

It might work, however it would require some research to be able to use the data in any meaningful way.

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

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?

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?

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

Stop();

//resets game:

gameStarted = false;

timerFlag = false;

currentButton = LOW;

lastButton = LOW;

Blink(2);

Serial.println("message");

}

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.

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.

I made a few changes, I missed a few things in the last one

When you first press the button nothing should happen, then after between 2 and 5 seconds it will print out 9090 and start the timer. If the button is not pressed within 5 seconds of this it will reset the game and print Timed Out. If the button is pressed before this happens it will print out your reaction time in seconds.

http://pastebin.com/BpbB4g8d

Let me know if this is what you want to happen

No, this is not what is intended. Let me rephrase it:

  1. wait for a button press. once this happens, a led blinks once indicating the game begins. "9090" is printed in the serial (is a code that sends a signal to the PC to play a sound).
  2. after a random lapse, the led lights up, and a clock starts to measure the time until you press the button again. In this lapse of time, nothing is printed.
  3. if you press the button after the light is turned on, it shows the reaction time in seconds (which the computer stores in a CSV file).
  4. if you press before the led lights up, it blinks three times and the game ends. ("1010" is a code that signals the PC to play an error sound). it then should return to an idle state and return to step 1.
  5. if you don't press the button after a time, the led blinks twice and the game ends. returns to step 1. (another code goes here, i haven't thought of it yet).
  6. once the game ends, it only begins again if you press the button again.

What must not happen, and is happening even with your last code, is that after a "Timed out", it shouldn't return to a code 9090. It should stay idle until a button is pressed. In this last coude you moved 9090 to a place where it's not meant to be.

Thank you for your patience! This seems arbitrary but it must follow this specification because it is intended to work on animals (which is my field of research).

@86! While I was trying to solve this method, I got suggested to try machine state approach. I revamped completely the code and for now it works, somehow.

The only problem is to get the correct reaction time.

Check it out.

http://pastebin.com/4prEbN0W

Thanks

good beginners projet

is it possible to make it so it stop and led flickers if u press it too early

and could u make it that

i made it too but i dont have camera so i cant show it but its good

It is possible to do, I will upload the changes in the code soon.

More Comments