Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

OpenSesame: VAS scale slider bar using keypress

Hi there,

I've come across a few solutions for slider bars using mouse click but I'm trying to set up one using an fMRI button box (keypress) whereby multiple keypresses can move the slider during a set duration (12 sec) and at the end record the final slider position. However, I'm stuck and would appreciate some help.

This is what my canvas looks like:


And this is my code below. It displays the scales but the slider doesn't move (I have gotten it to move by one increment if there's no while loop). Also, I'm using 'm' and 'n' but ideally I'll set these to the keys triggered by our button box.

from openexp.canvas import canvas

from openexp.keyboard import keyboard

my_canvas = canvas(self.experiment)

my_keyboard = keyboard(self.experiment)

resp = "" # Here we store the response

timeout = 5000

score = 0

step_size = .2

start_time = clock.time()

while True:

if clock.time() - start_time >= timeout:

response_time = timeout

response = u'timeout'

key, time = my_keyboard.get_key()

if my_keyboard.to_chr(key) == "m":

score +=step_size

increment = score*200

my_canvas['slider'].x += increment

my_canvas['slider'].show()

elif my_keyboard.to_chr(key) == 'n':

my_canvas['slider'].x -= increment

my_canvas['slider'].show()

score = max(0, min(1, score))

exp.set('score', score)

set_response(response=response, response_time=response_time)


The osexp file is also attached.

Thanks,

Melanie


Comments

  • Hi Melanie,

    I took the freedom to adapt your experiment somewhat. I am not entirely sure whether I get all the details of what you need, but I think the attached experiment will bring you a nice step forward in achieving it.

    Good luck,

    Eduard


    Buy Me A Coffee

  • Hi Eduard - that worked, thanks so much! I have two more question about timing:

    How can I ensure that after the initial keypress to start the experiment, that the timings are absolute?

    For example, in my practice run I present 5 scales each with a timeout = 4950ms (to approximate 5s), but when I use a stop watch the whole trial is 50s, more than double what I was expecting it to be. Timing is very critical for my fMRI study because patients will also have deep brain stimulation delivering electrical impulses during certain blocks.

    Do I need another inline script after my "Get Ready" sketchpad message, to tell the program to wait for a USB trigger from the scanner (e.g. '5' or '=') ?

    I can't seem to find any solution for this in the forum.

    Thanks,

    Melanie

  • Hi Melanie,

    I am not sure I fully get your goal here.

    How can I ensure that after the initial keypress to start the experiment, that the timings are absolute?

    Absolute relative to what?

    but when I use a stop watch the whole trial is 50s, more than double what I was expecting it to be

    The reason for that is that you poll the response twice. Once in the inline_script itself (kb.get_key()) and once with the keyboard_response item. You don't need the second if you have it in the inline_script. For this reason you currently should have to press twice to continue each scale I think.


    Do I need another inline script after my "Get Ready" sketchpad message, to tell the program to wait for a USB trigger from the scanner (e.g. '5' or '=')

    If you know that the trigger that the scanner sends is either a 5 or a =, you can simply change the duration of your sketchpad to 0 and add a keyboard response after it that has as allowed keys only 5 or = (separated by a semicolon). Does this make sense?

    . Timing is very critical for my fMRI study

    If timing is important you have to be really careful that you won't make Opensesame unresponsive for a while, for example, by having clock.sleep commands, or similar. The approach with the while loop that is used in define should be the way to go. But irrespective of all that, there is only one way to know for sure. Testing, testing and testing :) Without running pilots to make sure your procedure works as expected, you almost certainly will find something later that could have been done somewhat better.


    Good luck,

    Eduard

    Buy Me A Coffee

  • That all makes sense. Thanks very much Eduard!!


    Melanie

Sign In or Register to comment.