# Reaction time in builder with code component

**URL:** https://discourse.psychopy.org/t/reaction-time-in-builder-with-code-component/4011
**Category:** Builder
**Created:** [February 22, 2018, 9:04am UTC](https://discourse.psychopy.org/t/reaction-time-in-builder-with-code-component/4011 "2018-02-22T09:04:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![siri](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@siri](https://discourse.psychopy.org/u/siri)
#### Post date: [February 22, 2018, 9:04am UTC](https://discourse.psychopy.org/t/reaction-time-in-builder-with-code-component/4011/1 "2018-02-22T09:04:06Z")

</div>

Hi, I have created an experiment in the builder with a code component. In short, a sound is played, and participants then need to select an image and click on it with the mouse. To get RT, Mouse.time does not work because it registers many clicks, and if I select “End routine on Press”, it registers any click, not restricting it to the image. I think my best option here is to add a code component to get reaction time. I don’t actually know how to do this. The current code I am trying gets me odd numbers in RT (always 0.000… something).

Below is my code component for registering which image has been clicked on + my failed attempt for getting RT.

```auto
win.setColor('white')
clock = core.Clock()
for stimulus1 in [uh, oh, ah, au, u, o, a]:
    if mouse.isPressedIn(stimulus1):
        timeUsed=clock.getTime() #get the the time used by the subject to respond since a stimulus is displayed.
        continueRoutine = False
        thisExp.addData('clicked', stimulus1.name)
        thisExp.addData('RT', timeUsed)
        stimulus1.opacity = 0.2
        win.flip()
        stimulus1.opacity = 1 
    if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
        event.clearEvents()
        clock.reset()
```

---

<div class="post-metadata">

### Author: ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)
#### Post date: [February 22, 2018, 9:23am UTC](https://discourse.psychopy.org/t/reaction-time-in-builder-with-code-component/4011/2 "2018-02-22T09:23:07Z")

</div>

The code needs to be revised in line with this post re the visual stimuli:

> [@Signaling when an image is clicked](https://discourse.psychopy.org/t/signaling-when-an-image-is-clicked/3988/4):
>
> You can still change the opacity, just don’t call win.flip(). Code in this tab of the code component runs once per win.flip() that Builder calls. If you insert another one here, you will interrupt the timing of every other aspect of Builder: it’s expecting the code in this tab to be complete within one screen refresh, but you’ve just inserted a whole new pause for another refresh. So on a 60 Hz screen, this code will take 33.4 ms to execute, rather than 16.7 ms. So what you need to do is add s…

But re the timing, it is much simpler than you are trying here. Builder maintains a time variable called `t` which starts at `0` at the beginning of each routine. So just do this:

```
thisExp.addData('RT', t)

```

The reason your timing can’t work is you are creating a clock object almost immediately before you ask it for its time, so it will always give you an answer very close to zero. You need to create a clock just once, in advance, and then reset it at the moment you want to start timing from (again, just the once). But Builder is already doing this for you, so just use that.

---

<div class="post-metadata">

### Author: ![siri](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@siri](https://discourse.psychopy.org/u/siri)
#### Post date: [February 22, 2018, 11:16am UTC](https://discourse.psychopy.org/t/reaction-time-in-builder-with-code-component/4011/3 "2018-02-22T11:16:39Z")

</div>

Thank you for this Michael! It gives me the correct RT values. I have integrated it in the updated code you suggested in the post on ‘Signaling when an image is clicked’.

```auto
# check if the stimulus opacity was changed on the last refresh. If so, set it 
# back to 1.0 and then end the routine after it is shown again:
if opacity_low:
    stimulus1.opacity = 1 # make it fully visible again
    opacity_low = False # reset for next trial
    continueRoutine = False # end trial after showing stimulus on next frame.
else: # check for mouse clicks:
    for stimulus1 in [uh, oh, ah, au, u, o, a]:
        stimulus1.opacity = 1
        if mouse.isPressedIn(stimulus1):
            thisExp.addData('RT', t)
            thisExp.addData('clicked', stimulus1.name)
            stimulus1.opacity = 0.2
            opacity_low = True
```

---

<div class="post-metadata">

### Author: ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)
#### Post date: [February 22, 2018, 9:06pm UTC](https://discourse.psychopy.org/t/reaction-time-in-builder-with-code-component/4011/4 "2018-02-22T21:06:33Z")

</div>

> [@siri](#):
>
> It gives me the correct RT values.

Just note that Builder allows for checking for responses only once per screen refresh. So your timing resolution is limited by the refresh rate of your screen. e.g. you will get more precise (but not necessarily more accurate) RT measurements by upgrading from a standard 60 Hz LCD screen to the increasingly-common 144 Hz gaming-oriented displays.
