Reaction time Arduino program

Dear all,
My name is CHI. We designed a DIY device to measure left and right button pressing time. For example, the time of 1st left button pressing time is 1.556s, then the 2nd left button pressing maybe 3.778, after that the 1st right button pressing time is 5.557. The preference data example and our DIY device picture have been uploaded as attachments.

More importantly, we found one of the program from internet. But this one is not real what we want. This one may be suitable for measuring reaction time, which measure time interval between light open and pressing time.But we just want the pressing time.
Hence, any one can kindly help us to edit the program? Please help us. Many thanks.

volatile int state = 0;
int ledArray = {8, 9, 10, 11, 12};
int count = 0;
int timer = 1200;
int stopLed = 6;
int goLed = 7;
int randMin = 250;
int randMax = 750;
int startDelay = 750;
volatile float time ;
float start_time;

void setup() {
for (count=0;count< 5;count **){
pinMode(ledArray[count], OUTPUT);
}
attachInterrupt (0, ledonoff, RISING)
pinMode(stopLed, OUTPUT);
randomSeed(analogRead {0});
Serial.begin(9600);
}

void loop(){
//start state
if (state == 0) {
digitalWrite(stopLed, HIGH);
digitalWrite(goLed, LOW);
for (count=0;count< 5;count **){
digitalWrite(ledArray[count], LOW);
}
}
// start button pressed
if (state ==1){
// random start
startDelay = random(randMin,randMax);
delay(startDelay);
start_time = millis();
digitalWrite(stopLed, LOW)
digitalWrite(goLed, HIGH)
for (count=0;count< 5;count **){
delay(timer);
if (state ==2) {
time = (time - start_time)/1000;
Serial.print("Reaction time: " );
Serial.print(time);
Serial.println(“seconds”);
delay(1000);
break;
}
digitalWrite{ledArray[count], HIGH};
}
}
}
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);

}void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}

What does "pressing time" mean to you? Do you mean the time between a button being pressed and then released i.e. the time the button is held down. Or something else ?

Steve

No,sir. The pressing button time for me is that when will the subjects press the button. Not the holding time of the button.
For my research , the participant will watch the TV. The TV will show some stimuli items regularly. When they saw those items, they should press the the button as soon as. I just want to measure when they press the button. For example, the 1st second they press the left button? or the 2nd second they press the left button.

O.k. but when is the time measured FROM? In the program you show the Arduino creates the "stimulus" by switching an LED on. So it can easily measure how long after that the button was pressed.

If you are measuring reaction time you need to measure from when stimulus appears. In your case how is the Arduino going to know when something appears on the TV that causes someone to press one of the buttons?

Steve

Yep. That program is easier for measuring reaction time by giving a light stimuli (switching the LED light on the device). But we didn't want to measure this kind of reaction time, sir.

We just want to measure when will the subjects press the button. I think the Arduino may not need to know when the stimuli will appear because we will set the stimuli time by ourselves. The stimuli will be set at each 2 seconds, such as the 2nd, 4th, 6th, 8th, ...................

For example, the 1st stimuli will appear at the 2nd seconds on our TV. The Arduino measured that the subjects press the button at the 2.555 seconds for the 1st time(no matter left or right side). Then, I will know the 1st reaction time for this subjects is 2.555s-2s=0.555s (or 555ms). We just want Arduino to measure that 2.555s value.

Can we make it possible by Arduino ? Many thanks
Sorry I didn't have any background about the Arduino. If I made many mistakes, please forgive me.

OK, let's cut to the chase.

Please go and read the instructions, then go back and modify your post using the "More --> Modify" option you will find to the bottom right of the post, to mark up the code (but it always needs to be the complete code) as such so we can examine it conveniently and accurately. Please do not post a ".ino" file as an attachment - that would mean that you are expecting people to actually load it to their IDE to look at it and that is extra unnecessary labour. In fact, attachments do not always show properly on different operating systems.

If you do not mark it up as code, whatever code you do post could well be garbled and is certainly anything but easy to read. Yours seems to have survived so far but I didn't look too closely as it was just too messy.

Note: Also mark up any data - as a separate section - in the same way. This includes error output that you get from the IDE.

And - before you post any code, use "Auto Format" in the Tools menu of the IDE to properly present the code. You can then use the "copy for forum" function in order to paste it into a message with the "code" tags already provided.

Try and avoid unnecessary white space (blank lines). You should only use these to separate functional blocks of code.

SHUXUNCHI:
For example, the 1st stimuli will appear at the 2nd seconds on our TV. The Arduino measured that the subjects press the button at the 2.555 seconds for the 1st time(no matter left or right side). Then, I will know the 1st reaction time for this subjects is 2.555s-2s=0.555s (or 555ms). We just want Arduino to measure that 2.555s value.

I'll try again...when is the 2.555 seconds measured FROM! The Arduino can easily measure times like you need but it's times will either start counting from when it is first switched on OR when some event happens to trigger the timing starting. Your stimuli may appear every 2 seconds but it will be every 2 seconds from some zero time and the Arduino has to know about it.

Steve

How accurate do you want these times to be? Which Arduino board are you using - not all have accurate crystal oscillators...

slipstick:
I'll try again...when is the 2.555 seconds measured FROM! The Arduino can easily measure times like you need but it's times will either start counting from when it is first switched on OR when some event happens to trigger the timing starting. Your stimuli may appear every 2 seconds but it will be every 2 seconds from some zero time and the Arduino has to know about it.

Steve

Thank you so much,sir. I think I just want to measure the time that will start counting from when it is first switched on. I mean when the Arduino was switched to start, the time on Arduino will continue. When my participant press the button, the Arduino will help us to measure the exactly time. The time when participant press the button, such as 2.555;4.555;6.555. We do not need the time when the event happens to trigger the timing starting because I think it may be impossible for us. We will use the TV to show the event, we may hardly make a synchronization between TV and Arduino.
Is this way will be easier? Thank you so much. Please kindly help us.

Please kindly do what is advised in #5.

MarkT:
How accurate do you want these times to be? Which Arduino board are you using - not all have accurate crystal oscillators...

We hope we can measure to be millisecond. Then the data may be like 1.555s. I have no idea about the Arduino board. I found an engineer to design the device. He said we can measure millisecond. But he can't make an Arduino program as our wish. Please help us.

The Arduino can easily measure in milliseconds, but you need to somehow store and synchronize the result with your video.

You could maybe setup a realtime clock with milliseconds in the arduino, then timestamp the button press and send it serially to a PC using UART.

Then you only need to know at what time you started the video (at millisecond exactly), next you need to figure out at what time offset your stimuli appears in the video and you can hold that data against the timestamped button presses.

The Arduino normally uses a crystal as a clock source AFAIK, so the time deviation should be fairly exact; in the ppm range.

EDIT: milliseconds, not microseconds!

FYI, micros() times in 4 microsecond increments.

As MarkT says, not all Arduinos and their variants use crystal oscillators.

I did mean to say "The Arduino can easily measure in MILLIseconds".

µS would be a stretch, even with bare-metal coding! :slight_smile:

Still, it can measure in microseconds, just in 4us chunks. Much higher time res than milliseconds.

GeronimoDK:
The Arduino can easily measure in milliseconds, but you need to somehow store and synchronize the result with your video.

You could maybe setup a realtime clock with milliseconds in the arduino, then timestamp the button press and send it serially to a PC using UART.

Then you only need to know at what time you started the video (at millisecond exactly), next you need to figure out at what time offset your stimuli appears in the video and you can hold that data against the timestamped button presses.

The Arduino normally uses a crystal as a clock source AFAIK, so the time deviation should be fairly exact; in the ppm range.

EDIT: milliseconds, not microseconds!

Yep, we want to measure at millisecond. Thank you so much for your kind help.

Hello,

As already mentioned, an entry is required to perform the counting down, from which the time record can be recorded until the button is pressed.

So, I think it should have 3 buttons.

1 for TV presenter, who will shoot the time count and

2 for participants 1 and 2.

Consider reading the buttons (for participants) through interrupt so it will be more accurate.

I think the reset button (for TV presenter) does not need to be monitored by interruption, so it will be possible to use an Arduino with Atmega328.

There is not much secret in the count, I think the biggest challenge is the way this count will be reported.

rtek1000:
Consider reading the buttons (for participants) through interrupt so it will be more accurate.

Rubbish! :roll_eyes:

A flowchart can help in the elaboration of the algorithm

buttons timer.jpg

yEd:
https://www.yworks.com/products/yed/download