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. | 7 Comments |





7 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



Leave a Reply

*