loading
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.
<p>Great code!</p><p>I've adapted your code to do something more complex. </p><p>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. </p><p>Here's my adapted code: </p><p>http://pastebin.com/kMsBkVyj</p>
<p>elseif ((millis()- beginTime)&lt; (randomSeconds + maxTimeAfterStart)){</p><p style="margin-left: 20.0px;">serial.println(&quot;message&quot;)</p><p>}</p><p>Have you tried something like this</p><p>Nice job commenting the code, I always forget </p>
<p>That seems to work, kinda!!</p><p><a href="http://pastebin.com/NADJMRjm" rel="nofollow">http://pastebin.com/NADJMRjm</a></p><p>Now the problem is the following:</p><p>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?</p>
<p>if ((millis() - beginTime) &gt; (randomSeconds + maxTimer) &amp;&amp; gameStarted == true)</p><p>This works, I tested it out this time.</p>
<p>Could you send your code? Doesn't seem to work in my sketch. Where did you put that &quot;if&quot; statement?</p>
<p>if ((millis() - beginTime) &gt; (randomSeconds + maxTimer) &amp;&amp; gameStarted == true) {</p><p style="margin-left: 20.0px;"> Stop();</p><p style="margin-left: 20.0px;"> //resets game:</p><p style="margin-left: 20.0px;"> gameStarted = false;</p><p style="margin-left: 20.0px;"> timerFlag = false;</p><p style="margin-left: 20.0px;"> currentButton = LOW;</p><p style="margin-left: 20.0px;"> lastButton = LOW;</p><p style="margin-left: 20.0px;"> Blink(2);</p><p style="margin-left: 20.0px;"> Serial.println(&quot;message&quot;);</p><p> }</p>
<p>full code here http://pastebin.com/f7rtnkDL</p>
<p>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. </p><p>Let's look at the serial output</p><p>9090 &lt;- I pressed the button, gives 9090 (random timer initialized and waiting for reaction on led lightup)<br>1010 &lt;- 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)</p><p>9090 &lt;-pressed the button again, but this time I will not press any button to test the timeout<br>Timed out &lt;-success! it timed out and didn't output the reaction time<br>message &lt;- your suggestion has been executed<br>9090 &lt;- ***HERE***, the game starts, without waiting for my response! (which is inappropriate). <br>Timed out &lt;- it will continue to loop these 3 messages up to infinity<br>message<br>9090</p><p>So, that was the problem I need to solve. </p>
<p>Do you still get that error with the code I uploaded ? </p><p>I tested out your code and got the error you are describing but the code that I uploaded fixed that for me</p>
<p>very weird. after a timeout it stays idle, and does not print &quot;9090&quot; at all?</p><p>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 &quot;9090&quot; and keeps looping endlessly. </p>
<p>I made a few changes, I missed a few things in the last one</p><p>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. </p><p></p><p><a href="http://pastebin.com/BpbB4g8d">http://pastebin.com/BpbB4g8d</a></p><p>Let me know if this is what you want to happen</p>
<p>No, this is not what is intended. Let me rephrase it:</p><ol><li>wait for a button press. once this happens, a led blinks once indicating the game begins. &quot;9090&quot; is printed in the serial (is a code that sends a signal to the PC to play a sound).<li>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. <li>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). <li>if you press before the led lights up, it blinks three times and the game ends. (&quot;1010&quot; 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. <li>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). <li>once the game ends, it only begins again if you press the button again. </ol><p>What must not happen, and is happening even with your last code, is that after a &quot;Timed out&quot;, 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. </p><p>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). </p>
<p>@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. </p><p>The only problem is to get the correct reaction time. </p><p>Check it out. </p><p>http://pastebin.com/4prEbN0W</p>
<p>That's amazing. A great work. Do you think that it could be used to screen or assess degree of substance abuse?</p>
<p>It might work, however it would require some research to be able to use the data in any meaningful way.</p>
<p>nice good work</p>
<p>Thanks</p>
<p>good beginners projet </p><p>is it possible to make it so it stop and led flickers if u press it too early</p><p>and could u make it that</p><p>i made it too but i dont have camera so i cant show it but its good </p>
<p>It is possible to do, I will upload the changes in the code soon. </p>
Cool
<p>I have attached the new code to this comment, if you press the button too soon the LED will blink, and give you a message in the serial terminal.</p><p>Sorry it took so long, I have been bust and this silpped my mind.</p>
thx good work

About This Instructable

23,692views

19favorites

License:

More by 86:Twitter Controled Dalek Arduino and Plotly Based Seismograph Robopod the Arduino Powered Robot 
Add instructable to: