Arduino Reaction Time Tester
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);
}
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
garbaus2000 8 years ago
That's amazing. A great work. Do you think that it could be used to screen or assess degree of substance abuse?
alexi.roy123 4 years ago
86 8 years ago
It might work, however it would require some research to be able to use the data in any meaningful way.
Luis AndrésG 8 years ago
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
86 8 years ago
elseif ((millis()- beginTime)< (randomSeconds + maxTimeAfterStart)){
serial.println("message")
}
Have you tried something like this
Nice job commenting the code, I always forget
Luis AndrésG 8 years ago
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?
86 8 years ago
if ((millis() - beginTime) > (randomSeconds + maxTimer) && gameStarted == true)
This works, I tested it out this time.
Luis AndrésG 8 years ago
Could you send your code? Doesn't seem to work in my sketch. Where did you put that "if" statement?
86 8 years ago
if ((millis() - beginTime) > (randomSeconds + maxTimer) && gameStarted == true) {
Stop();
//resets game:
gameStarted = false;
timerFlag = false;
currentButton = LOW;
lastButton = LOW;
Blink(2);
Serial.println("message");
}
86 8 years ago
full code here http://pastebin.com/f7rtnkDL
Luis AndrésG 8 years ago
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.
86 8 years ago
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
Luis AndrésG 8 years ago
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.
86 8 years ago
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
Luis AndrésG 8 years ago
No, this is not what is intended. Let me rephrase it:
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).
Luis AndrésG 8 years ago
@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
Ozmo Mac 8 years ago
nice good work
86 8 years ago
Thanks
Ozmo Mac 8 years ago
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
86 8 years ago
It is possible to do, I will upload the changes in the code soon.