I am trying to create an experiment that presents a black screen until a switch connected to a Raspberry Pi is pressed. The goal is for this switch to be pressed by a bird to trigger video of a tutor bird singing, but I can’t find any information on using Psychopy in conjunction with Raspberry Pi IO pins. Is there some way I can use the “condition” option to make this work, or do I have to provide custom code for this? What would the code look like if I have to do that?
Are you using PsychoPy on the Raspberry Pi itself, or wanting the Pi to communicate to PsychoPy running on another computer?
If the former, there are Python packages to read from the pins, which are likely already installed on your Pi. Yes, custom code will be needed to import one of those packages at the beginning of the experiment and then read from the pins on every screen refresh.
There are many guides out there on using Python to do this - just adapt those and incorporate into a Builder code component. “Start” fields can contain only a single code expression, so you will probably just want to insert some variable name there, like start_video
, say, and define that to be False
at the start of the routine, and update it on every screen refresh by checking the relevant pin.
Yes I will be using PsychoPy on the Raspberry Pi – I had already put together some code to read from the pin and open video in Python before being told by a labmate it might be better to run the experiment in PsychoPy, so I have some idea of how to do it – still very new to coding in general though so many obvious things aren’t yet obvious to me – thanks for your help – I may be back with other questions
OK, you’ve probably done the hard work already then. Would suggest you use the Builder graphical interface, which should allow you to avoid most of the coding. Then just insert a “Code component” and insert the pin reading code you have there. The graphical code component helps put the code in the right place. e.g. in the “begin experiment” tab, insert the code to import the relevant GPIO library and do any necessary set up. Then in the “Each frame” tab, put the code to actually read the pin - it will then run automatically on every screen refresh.