psychopy.org | Reference | Downloads | Github

The two participants perform an experiment at the same time

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2020.2.4, 64bit
Standard Standalone? (y/n) Y

What are you trying to achieve?:
I want two participants to do the experiment(an attention task) at the same time and start simultaneously.

What did you try to make it work?:
The task is that first a small circle appears as a fixation point in the middle of the screen and then the participant presses the Space key and after that, cue appears on one of the four sides of the screen and then four images appear on the fours sides of screen and then in one moment, the brightness of one of the images changes, and if the participant detects it, he/she presses the Right key on the keyboard (in 80% of cases, the cued image brightness changes).
Now I want the two participants to do this experiment at the same time so that in half of the cases the changes in one image happened to both (the same trial) and in half the cases the changes in different images happened.

What specifically went wrong when you tried that?:
The experiment should start when both people have pressed the Space button. Do I need to use two different keyboards and screens(window) for participants to do this experiment?
Since I am a beginner in Python, can this be done in a builder environment?

Thanks so much for any tips.
Fazel

Hi Fazel,

Before we go further here, can I check that you do not intend to run this online? PsychoJS/Pavlovia cannot currently accommodate “dual player” type set ups.

If it is the case that you wish to run this locally, you will need to monitor 2 keyboard devices for keyboard events and then move on once space has been pressed on each of them. The first step here will be to distinguish between different keyboard devices, may I recommend taking a look at the documentation here Keyboard — PsychoPy v2021.1 on getKeyboards() which might help you in the right direction.

As a further point, I notice that youa re running 2020.2.4, could I recommend an upgrade since lot’s of bugs will have been ironed out since then, and that will make task creation much smoother!

Thanks,
Becca

1 Like

Hi Becca,
thanks so much for your response.
As far as I understand unfortunately it is not possible to define two keyboards (device) in an experiment in Windows and I must use another system.
Now I have another question, I put two keyboard component by the name of a_resp and b_resp each for a subject and I used this code at the End Routine to start the task when both pressed ‘space’ but it doesn’t work.

I sought similar problems which happened for others like this but all of them where about getting two correct answer from a keyboard(response component).
Do you or anyone have an idea to solve this problem?

Thanks for any help.
Fazel

In the past I’ve used an external USB number pad which acts like an additional mini keyboard. Could one participant be using the main keyboard and the other using something like that? Alternatively, could you simply define different keys for the participants to use on two standard keyboards?

Hi Fazel,

If you open coder view and type this what happens?

from psychopy.hardware import keyboard


print('There are ', len(keyboard.getKeyboards()), 'keyboards')
print('Keyboards: ', keyboard.getKeyboards())

Thanks,
Becca

Yes, it can be done, but will we not encounter the same getKeyboards() problem in Windows?

Hi Becca,
I did what you said and got this message:
There are 1 keyboards Keyboards: [{'usagePageValue': 1.0, 'usageValue': 6.0, 'usageName': 'slave keyboard', 'index': 0.0, 'transport': 'Keyboard', 'vendorID': None, 'productID': -1.0, 'version': None, 'manufacturer': None, 'product': 'Keyboard', 'serialNumber': None, 'locationID': -1.0, 'interfaceID': -1.0, 'totalElements': 0.0, 'features': 0.0, 'inputs': 0.0, 'outputs': 0.0, 'collections': 0.0, 'axes': 0.0, 'buttons': 0.0, 'hats': 0.0, 'sliders': 0.0, 'dials': 0.0, 'wheels': 0.0, 'touchDeviceType': -1.0, 'maxTouchpoints': -1.0}]

I’m suggesting that if the participants use different keys then it won’t matter whether PsychoPy can tell which keyboard is being pressed.

Hi Fazel,

Thanks for that. To confirm - that is when both keyboards are connected via USB is that correct?

Thanks,
Becca

Hi Becca,

Yes, I connected two keyboards to the PC via USB and then checked it.

Thanks,
Fazel

OK that is interesting! Sorry I thought that would work.

In that case I would recommend moving forward with Wakefields suggestion of using 2 different keys for your two keyboards.

You would then have one keyboard component, make sure “force end routine” is unchecked and in the code component in the “each frame” tab have:

if 'space' in key_resp.keys and 'return' in key_resp.keys:
    continueRoutine = False
    print('both required keys pressed, starting task')

hope this helps,
Becca

Hi Becca,
I hope you are well.
With your help and @wakecarter , my previous problem was fortunately solved and now I can use two keyboards, so that two keys are defined to start the task(‘y’ and ‘n’), one of which is pressed by the first subject(for example, ‘y’) and the other by the second subject(‘n’).
But the problem I have now is that, for example, when the first subject finishes its trial sooner and presses its own button quickly(‘y’), while the second subject has not yet answered itself and is in the previous trial, and when it responds and Enters the new trial and presses its own button(‘n’), the new trial starts for the first subject but does not start for the second subject until the start button of the first subject(‘y’) is pressed again.
Can you help me solve this problem?

Thanks so much for any tips.

Hi There,

Pleased to hear you solved your first problem! OK I think there are 2 steps here:

  1. Make sure you do not have “Force end routine” checked in either keyboard component.
  2. in a code component type in the “each frame” tab:
if key_resp.keys:
    if 'y' in key_resp.keys and 'n' in key_resp.keys:
        continueRoutine = False

This will mean that the trial only progresses when both the Y and the N keys have been pressed.

Hope this helps,
Becca

1 Like

Hi Becca,
thanks for the quick reply.
I did everything you said, but unfortunately I still have the same problem. My problem is that when the first subject finishes its trial sooner than the second subject and hits the key(‘y’). This key does not record for the second subject, which is still in the previous trial, and practically when the second subject finishes its own trial and he hits ‘n’, the next trial starts for the first subject but not for the second subject! How can I call the answer of the first subject that was given before the end of the second subject trial for the next trial of the second subject ?


This image shows my experiment design, I run two tasks(same task) at the same time and subjects do the their own task(task A, task B).
Thanks,
Fazel

Hi Fazel,

It looks like you have two separate PsychoPy tasks that are not connected here - should both tasks not be launched from the same builder (.psyexp) file?

Thanks,
Becca

Hi Becca,
In fact, the content of these two tasks is almost the same. The goal of duplicating a task is that because one of the subjects may respond sooner, his or her trial will end sooner. Our main problem is that we want the two subjects to start together each trial.
Thanks,
Fazel

In that case, I am afraid that there is currently no direct way for one experiment to wait for the other experiment - as the two are not connected. You could try using python to export a .csv file as soon as something is clicked on one computer and then continuously check how many .csv files there are in a subfolder in each script, and as soon as both detect there are 2 files then they start the next routine together.

In python you can check how many .csv files there are in a folder using:

# note this will only work locally not online (glob is a python package)
import glob
filenames = glob.glob("data/*.csv")
participantCount = len(filenames)

And to export a .csv file you can use

import csv 
# A list to write to csv - will have one col with header key pressed, and one row with value 1
myList = [{'key presses':1}]
keys = myList[0].keys()


# open a new csv file and write to it
with open('conditions.csv', 'w', newline='') as output_file:
    dict_writer = csv.DictWriter(output_file, keys)
    dict_writer.writeheader()
    dict_writer.writerows(myList)

You would need to have the second snippet of code in the end routine tab of the routine where the key is pressed, then you would need to have a routine with a code component something like:

participantCount = len(glob.glob("data/*.csv"))
while participantCount < 2:
    participantCount = len(glob.glob("data/*.csv"))

I haven’t tried this but hopefully it could be one solution.
Becca

Hi Becca,
Thank you for your help, but since I’m not good at Python, do you or anyone else have a different solution?
For example, can I get the answer of the first/second subject(‘y’/‘n’) that is hit earlier by placing the keyboard component in the task of the second/first subject and save it in a variable and move it to the next trial of the second/first subject?

Thanks,
Fazel

Sorry no, that wouldn’t be possible with two seperate experiment files that aren’t connected

Are you running two experiments simultaneously on the same computer?

If so, then it is worth checking whether a key press can be registered by both experiments.

If it can, then you should be able to wait for both key presses before moving forward.