PsychoPy version : 2021.2.0
What are you trying to achieve?:
Hello, everyone. I’m trying to build an experiment in which participants will see a picture and three buttons in one page. When they click on each button, they can hear the responding sounds.
Main problem:
I don’t know much about coding so I’m not sure what to put in the call back function. I want to write something like “if I clicked the button, then play the sound”, but I didn’t know the specific words for these functions…I’ve checked the psychopy.event and several posts but I cannot find a solution.
Could anyone give some suggestions on the codes to put in the call back function?Any help will be greatly appreciated. Thank you very much!!
This is a simpler version of my proposed button setting. Just for answer A.
What I’ve tried on the button coding according to what I’ve searched…But the syntax is wrong.
My sound component. Actually I also doubt what to put in the start time.
Instead of playSound(sound1) you could put playSound1 = True
Then put sound_1 with a start condition of playSound1
You might need to put playSound1 = False in a Begin Experiment code component tab
Hi @wakecarter, many thanks for your response!!! I just tried to make some changes to my answer_a button, sound_1 sound and code components as you suggested, but when I clicked on the button, no sound was played
if (answer_a.status === PsychoJS.Status.STARTED) {
// check whether answer_a has been pressed
if (answer_a.isClicked) {
if (!answer_a.wasClicked) {
// store time of first click
answer_a.timesOn.push(answer_a.clock.getTime());
// store time clicked until
answer_a.timesOff.push(answer_a.clock.getTime());
} else {
// update time clicked until;
answer_a.timesOff[answer_a.timesOff.length - 1] = answer_a.clock.getTime();
}
if (!answer_a.wasClicked) {
if (answer_a.isClicked) {
playSound1 = true;
}
}
// if answer_a is still clicked next frame, it is not a new click
answer_a.wasClicked = true;
} else {
// if answer_a is clicked next frame, it is a new click
answer_a.wasClicked = false
}
} else {
// keep clock at 0 if answer_a hasn't started / has finished
answer_a.clock.reset();
// if answer_a is clicked next frame, it is a new click
answer_a.wasClicked = false;
}
// start/stop sound_1
if ((playSound1) && sound_1.status === PsychoJS.Status.NOT_STARTED) {
// keep track of start time/frame for later
sound_1.tStart = t; // (not accounting for frame time here)
sound_1.frameNStart = frameN; // exact frame index
sound_1.play(); // start the sound (it finishes automatically)
sound_1.status = PsychoJS.Status.STARTED;
}
I’m not experienced with Buttons, but shouldn’t the callback function just be playSound1 = True since it only runs when the button is clicked?
Yeah I think you are right. There is no need for the line “if answer_a.isClicked:” in the callback function, but the sound cannot be played yet. Anyway, thanks so much for your kind help!! I’ll try to search more on the button function.
To check whether the issue is with the sound or the button, I would recommend creating a text element with the same conditional start.
I’m assuming you are running this locally. I also note that you’re PsychoPy version ends in 0. It might be worth upgrading to 2021.2.3, or waiting for 2022.1.2 which should be out very soon.
yeah the experiment is running locally, and thanks for your suggestion because now I can make sure that the problem is with the button and not with the sound. One step closer to the solution.
I’ve updated PsychoPy to 2022.1.1 but things still go wrong. The psychopy file is attached here and again thanks a lot for your follow-ups.
111_lastrun.py (10.4 KB)
I’ve put sound_1 with a start condition of $playSound1=True and this time the experiment works well in the Windows system but not on my Mac. Now the problem is that I cannot click in PsychoPy. I wrote a line “print(clicked)” in the callback function in the button component but nothing happens after running the experiment.