How to add vibrations to an experiment

OS: Windows 10 Pro
PsychoPy version : (2021.2.3)
What are you trying to achieve?: Trying to create an experiment with vibrations

I am trying to create an experiment using vibrations on a tablet. The participants are to complete the SART in both fixed and random conditions, and a high and low notification condition (so four trials). I have an audio version and a visual version in which I have a loop inside a loop, with spreadsheets randomized in one routine, which works well. However, I’m not sure how to create the vibration without writing a code component, which I am less familiar with. I’m also not sure how to control the code for each trial, because in two trials I want the tablet to vibrate four times, and in two trials I want it to vibrate 16 times.

What did you try to make it work?:

I have tried to create a code component using the startHaptics function. It returns an error message. I am pretty new to using the software so I’m not sure where to go from there, or if it’s even possible.

What specifically went wrong when you tried that?:

The coder in builder is not recognizing it as written code. I’m not sure what I need to add to get it to work

Apologies for the lack of detail but I’m not even sure where to start as I am unfamiliar with PsychoPy and even more so with writing code, so I appreciate any insights :slight_smile:

Hi There,

You can present vibrating in your task by adding a code component, change the “code Type” to JS and then adding this in the Begin Routine tab (if you do indeed want the vibration to happen at the start of the routine):

navigator.vibrate(1000);

where “1000” is the duration of the fibration in ms - for more details on what you can add into the navigator vibrate see here.

Note that this will not work on devices that do not have a vibrate feature available. So, to avoid an error, I suggest that you add the following to the Begin Experiment tab of your code component:

if ('vibrate' in navigator) {
    console.log('You browser supports vibrations');
} else {
    console.log('You browser does not supports');
    alert("Your browser does not support vibrations, which are important to this study. Please use an Anderoid device with a chrome or firefox browser");
}

Hope this helps!
Becca