# Using an E-Prime Button Box

**URL:** <https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483>\
**Category:** Coding\
**Created:** [November 16, 2016, 9:28pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483 "2016-11-16T21:28:23Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/richard/32/6389_2.png) [@richard](https://discourse.psychopy.org/u/richard)\
**Post date:** [November 27, 2016, 7:12pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/21 "2016-11-27T19:12:50Z")

</div>

> [@KyleMorrissey](#):
>
> Once that last bug is fixed and I add some instructions into the code component, I’ll pass everything along.

Excellent, looking forward!

---

<div class="post-metadata">

**Author:** ![KyleMorrissey](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KyleMorrissey](https://discourse.psychopy.org/u/KyleMorrissey)\
**Post date:** [December 5, 2016, 6:08pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/22 "2016-12-05T18:08:22Z")

</div>

Hi Richard,  
So here we are!

The code that runs the box itself and replaces IOhub is attached. It goes into the same folder as the experiment.

[pstButtonBox.py](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/1X/82cfab77a1bc8e8fc9d204006c6e80632602660a.py) (4.3 KB)

As is the code component/instructions (pasted at the end). Though technically for this to work it needs to be split into three code components. The “begin experiment” part cannot be in a routine that is inside of a loop, or else it gives you an error message saying that you don’t have permission to use the port.

The experiment file itself is at our lab, and I’ll send that next time I am in. We figured out the other day that I cannot attach the image files here, and so I could email that to someone.

# These are the necessary pieces to the code components for using the button box and providing error feedback

#Comments are provided for how to use this

#IMPORTANT: if you are using a windows machine, place the “begin experiment” section in its own code component at the begining of builder outside of any loops  
#… otherwise you will get an error

# Begin experiment

from pstButtonBox import pstButtonBox

#If windows machine, repalce ‘/dev/cu.usbserial’ with ‘COM1’ or ‘COM2’, etc.  
#Also select if you want any lamps on below

pstbox = pstButtonBox(‘/dev/cu.usbserial’)  
pstbox.flush()  
pstbox.lamps([1,0,0,0,1])

def get\_stimulation\_onset\_time():  
# We can use this to keep track of stimulation onset.  
global stimulation\_onset\_time  
stimulation\_onset\_time = core.getTime()

# Begin Routine

pstbox.flush()  
rt\_measurement\_started = False

# Each Frame

#Here “stim”, from stim.status is the name of the text, image, or whatever, component that participants respond to. Change name as needed.  
if not rt\_measurement\_started and Stim.status == STARTED:  
# Will set the global variable `stimulation_onset_time` to the exact time  
# the visual stimulus `stimulus` is being presented.  
win.callOnFlip(get\_stimulation\_onset\_time)  
rt\_measurement\_started = True  
pstbox.flush()

#if pstevents in (set available buttons from 1, 2, 4, 8, or 16)  
pstevents = pstbox.mostRecent()  
if pstevents in (1,16):  
responseTime = core.getTime()  
rt = responseTime - stimulation\_onset\_time  
button = pstevents  
continueRoutine = False # Stop if we have a response.  
break #Stop the for loop now that we have an acceptable key press

# End Routine

# 

if not pstevents:

```
rt = None
button = None
isCorrect = False

```

else:  
isCorrect = button == (2 \*\* (Correct - 1))  
#“Correct” above is whatever variable you used to name your list of correct answers in the excel file

Trial.addData(‘TrialResponse\_Button’, button)  
Trial.addData(‘TrialCorrect’, isCorrect)  
Trial.addData(‘TrialRT’, rt)

# End Experiement

pstbox.lamps([0,0,0,0,0])

#if you want to use error feedback.

# In the feedback routines, change the Begin Routine to this

#Make sure you set the text component displaying msg to “set every repeat”  
if button == None:  
msg=“Oops! You didn’t answer in time”  
elif isCorrect:#stored on last run routine  
msg=“Correct!”  
else:  
msg=“Oops! That was wrong”

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 16, 2017, 5:02pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/23 "2017-02-16T17:02:34Z")

</div>

Hello Richard,

I was wondering if I can ask a question regarding this post. I’ve adapted the code you provided so that we can use our Eprime response box to advance through an experiment. It works, but every button press makes the experiment advance extremely fast (across two routines). I’ve used the “clearEvents” command to empty the buffer, but it makes no difference. If you have any suggestions, I’d be very grateful.

best wishes  
josé

---

<div class="post-metadata">

**Author:** ![KyleMorrissey](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KyleMorrissey](https://discourse.psychopy.org/u/KyleMorrissey)\
**Post date:** [February 16, 2017, 5:31pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/24 "2017-02-16T17:31:52Z")

</div>

Hi josé  
Did it do this with a keyboard as well? It sounds like you have no delay between trials, and so the experiment advances to the next trial instantaneously before the participant may be ready.

Cheers,  
Kyle

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 16, 2017, 9:46pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/25 "2017-02-16T21:46:35Z")

</div>

Hi Kyle!

With the keyboard this didn’t happen. Each button press only advanced to the next screen. With the e prime response box, I guess I could add a pause between routines so that each button press only advances to the next screen, but there must be a way to make it work like the keyboard component?Thanks!

---

<div class="post-metadata">

**Author:** ![KyleMorrissey](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KyleMorrissey](https://discourse.psychopy.org/u/KyleMorrissey)\
**Post date:** [February 16, 2017, 11:58pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/26 "2017-02-16T23:58:12Z")

</div>

Hi josé,  
There isn’t really anything in the code component that should interact with your experiment in such a way as to cause it to be skipping trials. It just doesn’t interact with the loop at all. It just records what happens in the trial. Would you be able to post the experiment? You might have changed something by accident when putting in the code component.

Cheers,  
Kyle

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 17, 2017, 11:13am UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/27 "2017-02-17T11:13:12Z")

</div>

Hi Kyle,

Sorry, I think I didn’t explain this properly. The code component is not part of a loop. We want to use it to move from one routine to the next. So, I did add an “continueRoutine = False” line of code, but the problem is that if the finger is still on the button, it flashes through the experiment. I thought that the clearEvents command would take care of that, but it doesn’t.

Thanks for your time, by the way.  
Best,  
José

---

<div class="post-metadata">

**Author:** ![KyleMorrissey](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KyleMorrissey](https://discourse.psychopy.org/u/KyleMorrissey)\
**Post date:** [February 17, 2017, 1:17pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/28 "2017-02-17T13:17:57Z")

</div>

Hi José,  
I am not sure that I understand what you mean. How can you have more than one trial if there is not a loop? Did you code each routine separately? That sounds really really time consuming.

Ok well if that is the case, then I can see why adding a buffer routine would be more work than if this was embedded in a loop!

I’ll link one of the people who helped make this. He thinks that he has an idea.

Kyle

---

<div class="post-metadata">

**Author:** ![KyleMorrissey](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KyleMorrissey](https://discourse.psychopy.org/u/KyleMorrissey)\
**Post date:** [February 17, 2017, 1:36pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/29 "2017-02-17T13:36:45Z")

</div>

Ok he couldn’t log in at this moment but he says that he needs to see the code and/or builder file to understand what it is that you are trying to do.

and I’ll second the request as well

Cheers,  
Kyle

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 17, 2017, 1:49pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/30 "2017-02-17T13:49:48Z")

</div>

Hi,  
I don’t have more than one trial. I have 5 successive routines (these are to give instructions to the participants), and the idea is to have participants advance through them via button presses. Those routines are not timed. The participant gets as much time as they need to read the instructions. The button press from the response box is what ends each routine. The problem is that it also ends the next routine…

Thanks for the help!  
Best,  
José

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 17, 2017, 1:51pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/31 "2017-02-17T13:51:09Z")

</div>

Ok, I’ll share this in a tic!  
Best,  
José

---

<div class="post-metadata">

**Author:** ![KyleMorrissey](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KyleMorrissey](https://discourse.psychopy.org/u/KyleMorrissey)\
**Post date:** [February 17, 2017, 2:07pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/32 "2017-02-17T14:07:15Z")

</div>

Wait, if this is not timed, then why are you using a button box to begin with? That is the only advantage of this hardware over a keyboard.

Kyle

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 17, 2017, 2:22pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/33 "2017-02-17T14:22:33Z")

</div>

Great question!

Because the study involves over 10 tasks, and some of them are timed. So, to prevent participants from getting confused about what keys they need to use for which task, everything has been set up so that they can use two keys from a response box for all tasks. At my previous lab, where data from one experimental group are being collected, our response box functioned as a keyboard. However, at my current lab, we only have an eprime response box, which doesn’t function like that…

I hope this makes sense!  
best  
José (massively appreciative of the time you’re taking to respond to my queries)

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 17, 2017, 2:35pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/34 "2017-02-17T14:35:56Z")

</div>

Hello,

Since the real thing is huge, I created a mini experiment with the code I’m using. You’ll see that each button press makes it advance through 2 routines.

Thanks,  
josé[test.psyexp](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/1X/66853d6f5e930b3348fab6b989742b7206eb59e4.psyexp) (17.0 KB)

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 17, 2017, 3:18pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/35 "2017-02-17T15:18:10Z")

</div>

Hi,

After more testing, we think the problem is that a routine is skipped because psychopy seems to detect that the button is still pressed when the next routine begins.

I sort of solved the problem by adding a core.wait(.5) command, but I wonder if there is a way to set it up such that the “effects of the button press” end with the routine.

josé

---

<div class="post-metadata">

**Author:** ![KyleMorrissey](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@KyleMorrissey](https://discourse.psychopy.org/u/KyleMorrissey)\
**Post date:** [February 17, 2017, 3:42pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/36 "2017-02-17T15:42:55Z")

</div>

Oh that is definitely the problem. The boxes transmit a continuous stream of characters while being pressed.

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 17, 2017, 3:45pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/37 "2017-02-17T15:45:02Z")

</div>

I see… Is there a way to stop that? Is there a way to ask psychopy to only capture the first character?

Thanks, Kyle.  
best  
josé

---

<div class="post-metadata">

**Author:** ![Darcy\_Hallett](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@Darcy\_Hallett](https://discourse.psychopy.org/u/Darcy_Hallett)\
**Post date:** [February 18, 2017, 5:09pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/38 "2017-02-18T17:09:54Z")

</div>

HI José,

I helped develop the code implementation of our pstButtonBox class, so maybe I can help (see Kyle’s post from Dec. 5th). Before I get to the problem at hand, I wanted to make sure that we are talking about the same code. From the test.psyexp file you posted, it seems like you are using the serial.Pstbox from the ioHub (from Richard’s post on Nov. 22nd). What Kyle has been talking about is our own class to replace the ioHub that we developed that was specifically for the ePrime pst box. It is called pstButtonBox (see Kyle’s post from Dec. 5th). If you are using the ioHub, I don’t know that code well enough to properly advise you. I will proceed instead on the assumption that you are using our pstButtonBox code and you can adapt it accordingly.

No matter which code you are using, I think you have properly diagnosed the problem, in that the program still sees the button as being pressed when it gets to the next screen. This is not a fault of the function itself, per se, just the fact that the computer is so fast and gets to the next instruction window and waits for a button press before a normal human can lift their finger up. Adding the half-second pause, as you did, is one way to solve the problem. Another way is to have the program wait for the button to be lifted before proceeding. With our class, you would do the following:

![](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/1X/2c043f078953db2b40380f05e03eba6cc46c97aa.png)

You can insert this code in the End Routine Section, or in the Begin Routine for the next instruction screen.

I do not have a button box at home, so I am not absolutely sure if it is a None value that gets returned when a button is not being pressed. It’s possible that the number 0 is returned if no buttons are being pressed. If that doesn’t work, you can try other things (like not equal to the numbers 1 to 31). If neither of these things work for you, I will check it out for you when I go back into the office on Monday. If you are using the ioHub code, there is probably something equivalent that you can do.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 19, 2017, 6:28pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/39 "2017-02-19T18:28:41Z")

</div>

Dear Darcy,

I’ll try this first thing in the morning tomorrow! Thanks a lot for your kindness!  
Best,  
José

---

<div class="post-metadata">

**Author:** ![alemanbanon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/alemanbanon/32/31262_2.png) [@alemanbanon](https://discourse.psychopy.org/u/alemanbanon)\
**Post date:** [February 22, 2017, 3:53pm UTC](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483/40 "2017-02-22T15:53:48Z")

</div>

Hello,

Okay, so I tried using the code that you and Kyle developed, but it didn’t work. So we went back to using ioHub and we adapted the “while true: …” line of code that you suggested and now it works (but we’re not certain why…)

What I did was put the code below in the End Routine box of the code component:

while true:  
if pstbox.getEvents():  
break

I think the code tells psychopy that if a character is being sent by the response box at the end of the routine (due to the fact that the button is still pressed), then it should break the loop. Not sure it makes sense…

Thanks!

[Previous page](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483.md?page=1)

[Next page](https://discourse.psychopy.org/t/using-an-e-prime-button-box/1483.md?page=3)
