Reaction time test circuit.

CODE:

int pbIn =4;
int ledOut = 7;
int button = 0;
long randNumber;

void setup()
{
pinMode(pbIn, INPUT);
pinMode(ledOut, OUTPUT);

}

void loop()
{
button = digitalRead(pbIn);
digitalWrite(ledOut, LOW);
//randNumber = random(500, 1000);
if (button == LOW)
{
delay(1000);
digitalWrite(ledOut, HIGH);
// delay(1000);
}
}

I wanted a delay of (500-1000 ms) before the led lights up when I press the button, I used the random function for this, but when I tried even with a simple delay of 1000 ms, nothing happens, however the led lights up on button press (when delay is not there in the code).

smkuls:
I wanted a delay of (500-1000 ms) before the led lights up when I press the button, I used the random function for this, but when I tried even with a simple delay of 1000 ms, nothing happens, however the led lights up on button press (when delay is not there in the code).

int pbIn =4; 
int ledOut = 7;  
int button = 0;
long randNumber;

void setup()
{
  pinMode(pbIn, INPUT);
  pinMode(ledOut, OUTPUT);
  
}

void loop() 
{
  button = digitalRead(pbIn); 
  digitalWrite(ledOut, LOW);
  randNumber = random(500, 1000);
  delay(randNumber); // If you wanted a delay of (500-1000 ms) you have use delay
  if (button == LOW)  
  {
  delay(1000);
    digitalWrite(ledOut, HIGH);
   // delay(1000);
  }
  delay(randNumber); // or here
}

you have to put delay() function out side the If()

This doesn't seem to be doing anything relating to reaction times at the moment and I'm not entirely clear what behaviour you're trying to achieve, but currently you turn the LED off at the top of loop() so even if you turn it on it will only be on for a couple of microseconds.

My guess is you want something similar to this sequence:

Wait for a random interval.
Turn the LED on.
Wait for button to be pressed and released.
Turn LED off.
Repeat.

Is that the general idea?

well I figured it out, it was just a part of the code, and thanks I gave a delay after turning it on, so that it stays on for some time :smiley:

Thanks a lot.

try not to use delay could you?

PeterH:
My guess is you want something similar to this sequence:

Wait for a random interval.
Turn the LED on.
Wait for button to be pressed and released.
Turn LED off.
Repeat.

Is that the general idea?

Hi all, I would like to make a reaction test game but am having trouble getting my head round it even though I am sure it is very simple to sort out.

I'd like to do the following...

  • the game / loop starts
  • you wait for an LED to light up after a random time (say within 3 secs of startup)
  • then you have a 1 sec window (for example) within which you have to press a button
  • if you press the button in time then something indicates that you are successful
  • if you don't press it quick enough then something tells you that you failed
  • then it starts again

Now I have managed to code something (which is down below) based on the BlinkWithoutDelay example that can detect when a button is pressed and when it is released, and then do something depending on what the time difference turns out to be, but it's not quite what I am after.

I'm thinking using interrupts might be in order, to have button push detection running in the background, but I don't know. Any advice is greatly appreciated.

int buttonPin = 2;
int ledPin = 10;
int buttonState, lastButtonState = 0;
long pressTime, releaseTime, timeDiff = 0;


void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}


void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState)   {
    if (buttonState == HIGH)     {
      pressTime = millis();
    }

    else     {
      releaseTime = millis();

      timeDiff = releaseTime - pressTime;

      if (timeDiff < 2000)       {
        digitalWrite (ledPin, HIGH);
      }

      else       {
        digitalWrite (ledPin, LOW);
      }
    }
  }
  lastButtonState = buttonState;
}

Interrupts are not necessary for this problem. The approach you are taking seems sensible to me, and at first glance that code looks OK. In what way is it 'not quite what I am after'?

Thanks PeterH, I wanted the button pressing bit to be prompted by a light coming on and couldn't work out the order things had to happen in the loop.
A friend advised me though and I have the following. However, the success LED (ledPinYes) comes on regardless of whether I press the button or not, although it does work when I press the button in time. Getting there though...

int buttonPin = 2;
int ledPin = 10;
int ledPinYes = 9;
int ledPinNo = 9;

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinYes, OUTPUT);
  pinMode(ledPinNo, OUTPUT);
}

void loop() {

  // start game, reset all displays
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPinYes, LOW);
  digitalWrite(ledPinNo, LOW);

  // wait until the buttons are released
  while (digitalRead(buttonPin)) {
    // do nothing
  }

  // random delay 1-4 seconds
  int random_delay = 1000 + random(3000);
  delay(random_delay);

  // turn on led
  digitalWrite(ledPin, HIGH);

  // can adjust this reaction time, current time plus a sec
  long failtime = millis() + 1000; 
  int success = false;

  while (millis() < failtime) {
    if (digitalRead(buttonPin)) {
      success = true;
      break;
    }
  }

  if (success) {
      digitalWrite(ledPinYes, HIGH);

  } 
  else {
      digitalWrite(ledPinNo, HIGH);

  }

  // hold the success/failure display for a few seconds
  delay(3000);

}

The code looks great, and I think it would work if you just defined the yes and no LEDs to be on different pins.

int ledPinYes = 9;
int ledPinNo = 9;

Oh man, how did i miss that. THANKS!

Hello,

I am new to the world of Arduino, I work on a system that measures and displays the response time of an athlete, for that I must use 7 LED that light up in a random manner or on a selected sequence and each time an lED is on the athlete must hit the corresponding plate to turn off the lED and measured the time between switching on and off of the led, I'm between using a piezoelectric sensor as the impact sensor or a proximity sensor
thank you for supporting me

cdt