Reaction Timer


Difficulty Level = 1 [What’s this?]

How fast can you press a button once a timer starts counting? Find out by attaching a button to your Arduino and the Digit Shield. A simple tactile button switch is connected to digital pin 8 and ground.




Press the button once to start the sequence. First the Arduino will wait a random amount of time between 2 and 5 seconds, then it will start counting milliseconds on the display. As soon as you see the numbers start, press the button again to stop the count. Then repeat: press the button again to clear the display and start the random wait before the numbers start counting again.

Here is the complete code:

#include <DigitShield.h>
#define BUTTON 8

unsigned long start, stop;

void setup() {
  randomSeed(analogRead(0));

  pinMode(8, INPUT);
  digitalWrite(8, HIGH);

  DigitShield.begin();
  DigitShield.setBlank(true);
}

void loop() {
  // wait until button press
  while (digitalRead(BUTTON) == HIGH);

  // turn off the display
  DigitShield.setBlank(true);

  // delay from 2 to 5 seconds
  delay(random(2000, 5000));
  start = millis();
  DigitShield.setBlank(false);

  while (true) {
    DigitShield.setValue((int)(millis() - start));
    if (digitalRead(BUTTON) == LOW) {
      stop = millis();
      break;
    }
  }

  DigitShield.setValue((int)(stop-start));
  delay(1000);
}



Published by Michael, on March 18th, 2011 at 4:40 pm. Filed under: Arduino,Level 1. | 22 Comments |





22 Responses to “Reaction Timer”

  1. Michael–fantastic instruction to build shield, worked right away, ran through all programs, great for a relative newcomer like me but I did pick up on your test of not including at the beginning of this reaction timer. After that it works fine. Look forward to more great products from you, keep me on your newsletter if you start one! JD

    Comment by John Dale on April 16, 2012 at 1:54 PM



  2. Thx for the feedback — the #include was not a test, it was my blog software messing up the code. Fixed.

    Comment by Michael on April 16, 2012 at 2:26 PM



  3. I want to modify this project a little. What do I need to do to get an LED to light up at same time clock starts counting down?
    If you answer this post please keep in mind I know very little about arduino and programing.
    Thanks.

    Comment by John on September 2, 2012 at 5:44 PM



  4. First you’d connect an LED to one of the Arduino output pins with an appropriately sized resistor. Let’s say it’s pin 13.

    In setup(), include the line:
    pinMode(13, OUTPUT);
    digitalWrite(13, LOW);

    Just before the while (true) loop, turn on the LED just before starting the countdown:
    digitalWrite(13, HIGH);

    And just after the while loop, turn the LED off:
    digitalWrite(13, LOW);

    Comment by Michael on September 3, 2012 at 7:07 AM



  5. Thanks Michael! Circuit and code works great.

    Comment by John on September 4, 2012 at 9:29 PM



  6. Hi I built a digit shield for a reaction timer I cut and pasted the code but during the check stage it came back as DigitShield was not declared in this scope.
    Could you tell me how to fix?
    Thanks
    Al Buck

    Comment by Alan on November 13, 2012 at 10:56 AM



  7. You need to install the Digit Shield library. See the product page for information about the library and how to use it.
    http://www.nootropicdesign.com/digitshield/

    Comment by Michael on November 13, 2012 at 11:02 AM



  8. Is there a way to make it start counting on someones command? Like a 12v input for a trigger?

    Comment by Autohabit on October 31, 2013 at 5:08 PM



  9. Autohabit, yes you could trigger an input pin on a voltage HIGH condition (5V) instead of a switch that connects the pin to ground. Simple code change. 5V is max because that’s the operating voltage.

    Comment by Michael on October 31, 2013 at 5:37 PM



  10. Awesome! Thank you so much!

    Comment by Autohabit on October 31, 2013 at 5:54 PM



  11. So exactly what would i have to purchase to complete this project?

    Comment by Autohabit on November 1, 2013 at 9:50 AM



  12. This project uses our Digit Shield that we sell in our store. It requires an Arduino board of course.

    Comment by Michael on November 1, 2013 at 10:01 AM



  13. Does it matter what board i use?

    Comment by Autohabit on November 1, 2013 at 2:50 PM



  14. Autohabit: Uno or Duemilanove or compatible.

    Comment by Michael on November 1, 2013 at 2:56 PM



  15. Do you have any good suggestions on a case that fits both boards?

    Comment by Autohabit on November 6, 2013 at 4:44 PM



  16. Here’s one: https://www.sparkfun.com/products/10088
    You’d have to cut out some of the face so you can see the display.

    If you google for “Arduino enclosure” you’ll find other options.

    Comment by Michael on November 8, 2013 at 9:02 AM



  17. I need help with writing the code to eliminate the random start function. Instead initiating it with button 9 and still using button 8 to stop it. Here is the catch i want it to display a negative time if the stop button is pressed before the start button.

    Comment by Autohabit on November 12, 2013 at 12:55 PM



  18. I think you will get more feedback if you post to the forum instead of the blog. http://nootropicdesign.com/forum/viewforum.php?f=42

    Also, you can’t display a negative with this display.

    Comment by Michael on November 12, 2013 at 7:13 PM



  19. I’m trying to display the time in seconds rather than milliseconds. This is what I have so far but it’s not displaying correctly.

    DigitShield.setPrecision(2);
    DigitShield.setValue((int)(stop-start)/1000);

    If I have a reaction time of 1624 milliseconds, I was hoping to get 1.62 to display however it only displays 1.0. If the the reaction time is 2292 milliseconds the display is 2.0. I don’t know why it’s rounding down.

    Any ideas on what I’m doing wrong?
    Thanks.

    Comment by Rich on February 25, 2014 at 10:35 PM



  20. Rich, it is displaying an integer because you are casting the value to an int. That truncates 1.62 to 1. Use
    DigitShield.setValue((float)(stop-start)/1000.0);

    Comment by Michael on February 26, 2014 at 7:49 AM



  21. Does exist a port of DigitShield for ChipKit Max32?

    Comment by Renato on July 18, 2014 at 7:23 AM



  22. no, not at this time.

    Comment by Michael on July 18, 2014 at 8:59 AM



Leave a Reply

*