Howdy, Stranger!

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

Supported by

[solved] Breaking a loop by key press? How do I do that?

edited December 2014 in OpenSesame

Hello everyone,
I'm doing a project for a class and I'm a strait up newbie at this program or Python coding (no idea how it works).
I created an experiment that is based on time response but I can't seem to figure out how to make a loop stop by a key press (example: spacebar) which is crucial for the experiment and for the data. I tried reading the discussions about similar problems but I have really no idea how Python works so I just got lost by trying different codes which didn't work. Can someone please help this newbie? Thank you in advance :)

Comments

  • edited 4:07PM

    Hi,

    First of all, welcome to the Forum! The easiest way to learn about Opensesame (and some python), if you do one of the tutorials (e.g. this one).

    What you can do is defining a variable that is True if you want to run a loop and False if not. Then you only have to monitor your keypresses and set the variable to Falseas soon as space is pressed.

    As for the code you'll need an inline_script before the loop you're talking about, in which you can initialize your breaking variable:
    breaking_variable = True

    and another inline_script in the end of your loop sequence (preferably in its run_phase) in which you can set the variable to False, if the response is 'space'.

    if exp.response == 'space':
        breaking_variable = False
    

    Finally, you have to set the run if fields to [breaking_variable] = True, for every element in the loop.

    If you get stuck, ask again with your specific problem or have a look on the documentation of Opensesame. Most of the information you need, you can find somewhere there.

    Good luck,

    Eduard

    Buy Me A Coffee

  • edited December 2014

    Hello Eduard, Thank you so much for you fast response. I did the tutorials before and that's the way I figured out how to use the program it's just I got stuck at this point because I have to use Python coding which just confused me haha.

    I understand defining the variable to change stimuli or get the right or wrong response but I don't know how I can define a variable the way you said: so "it is True if I want to run a loop and False if not." Everything else I did as you said. Can you help me with that?

    Basically I'm recreating a visual experiment where a participant needs to see differences between two stimuli where one have been altered and press space bar when they see the change and to report verbally where the change was. I created a loop with 12 variables that has another loop altering one variable with the original image and the modified version and basically I want the variables changing when they recognized the change (pressed space bar). I made the stimuli to alter but I cant make it stop by pressing a key.

    Here's basically what I did.
    image

    Thanks again for your help,
    Kat

  • edited 4:07PM

    Hi Kat,

    So you want this loop, in which you present modified and unmodified images alternatively to break, as soon as participants indicated per keypress that they saw the change?

    If so, I recommend you simplify your Imageloop_sequence a bit. Instead of showing every image in a sketchpad you can move stimulus presentation to an inline_script, because otherwise you would have to include as many inline_scripts as you have sketchpads. The code below, put in an inline_scriptshould be enough to replace everything inside your sequence. I didn't test it very thoroughly though. Probably you have to adjust some things. Also, please make sure that everything what you need is logged!


    from openexp.canvas import canvas from openexp.keyboard import keyboard # open canvas for every different screen you want to present image1_canvas = canvas(exp) image2_canvas = canvas(exp) blank_canvas = canvas(exp) # Determine the absolute path for your files: path1 = exp.get_file(u'get-started.png') path2 = exp.get_file(u'step1.png') # draw each of the canvases image1_canvas.image(path1) image2_canvas.image(path2) blank_canvas.fixdot(500,500) # or whatever your blank looks like # add a keyboard object to be able to log keypresses my_keyboard = keyboard(exp, keylist=['space']) while True: t1 = ti.time() image1_canvas.show() # "time" defines how long you'll see the image key, end_time = my_keyboard.get_key(timeout= 500) if key == 'space' or t1-t0 > 5: exp.set('response', key) #set response in logger break image2_canvas.show() key, end_time = my_keyboard.get_key(timeout= 500) if key == 'space' or t1-t0 > 5: exp.set('response', key) break blank_canvas.show() key, end_time = my_keyboard.get_key(timeout= 500) if key == 'space' or t1-t0 > 5: exp.set('response', key) break

    Good luck,

    Eduard

    Buy Me A Coffee

  • edited 4:07PM

    thank you a bunch for your help!!

  • Hi eduard,
    I tried what you described here. So, in order to break a loop, I put breaking_variable = True in an inline-scripte before the loop, and an inline-script containing
    if exp.response == 'space': breaking_variable = False
    in the last position of the loop (in the loop is just one sketchpad). Then I put [breaking_variable] = True in the run-if block of the sequence.
    However, my experiment does not run at all, the error message says "The variable 'breaking_variable' does not exist."
    What did I do wrong?
    Thank you!

  • Hi Pia,

    Your experiment does not run because [breaking_variable] refers to var.breaking_variable, not breaking_variable. See what I mean?

    Btw. Not that it matters, but it is a little unintuitive that your loop runs if breaking variable is True and breaks when it it False.

    Eduard

    Buy Me A Coffee

  • Hi eduard,
    thank you. I think I understand, but when I change braking_variable (in the inline-scripts only) to var.breaking_variable I get the following error:
    AttributeError: response not found
    So I changed the duration of the sketchpad to 0 (instead of keypress) and included a keyboard_response-item.
    But I still get this error-message!

  • Hi Pia,

    The script I wrote 2014 didn't really work.

    This makes more sense:

    from openexp.canvas import canvas
    from openexp.keyboard import keyboard
    
    # open canvas for every different screen you want to present
    image1_canvas = canvas(exp)
    image2_canvas = canvas(exp)
    blank_canvas = canvas(exp)
    
    # draw each of the canvases
    image1_canvas.text('path1')
    image2_canvas.text('path2')
    blank_canvas.fixdot(500,500) # or whatever your blank looks like
    
    # add a keyboard object to be able to log keypresses
    my_keyboard = keyboard(exp, keylist=['space'])
    t0 = clock.time()
    while True:
        t1 = clock.time()
        image1_canvas.show()
            # "time" defines how long you'll see the image
        key, end_time = my_keyboard.get_key(timeout= 500) 
        if key == 'space' or t1-t0 > 5000:
            exp.set('response', key) #set response in logger
            break
        image2_canvas.show()
        key, end_time = my_keyboard.get_key(timeout= 500)
        if key == 'space' or t1-t0 > 5000:
            exp.set('response', key)
            break
        blank_canvas.show()
        key, end_time = my_keyboard.get_key(timeout= 500)
        if key == 'space' or t1-t0 > 5000:
            exp.set('response', key)
            break
    

    Btw. Are you aware that the script takes care of both stimulus presentation as well as response collection? So if you want to combine it with a sketchpad you have to be careful to not mix things up and get into trouble.

    Does that make sense? Feel free to upload your experiment, if you can't get any further.

    Eduard

    Buy Me A Coffee

  • Hi eduard,
    thank you! However, I have only one sketchpad therefore I didn't use that script in the first place. I only included the inline-scripts to define the variable and to break the loop.
    So, I have a sketchpad, which is in a loop and the position of a rectangle of the sketchpad changes with each iteration of the loop. Participants should be able to break the loop if they feel it is at the right position.
    I'll upload my experiment. It is the first loop, I'm working on.

  • Hi Pia,

    I felt free to implement your design in an inline_script. I tried first to make it work with sketchpads and loops but it wasn't really working. Sorry for that. The good thing though, is that this implementation is really easy and rather intuitive.

    Let me know if anything is unclear.

    Eduard

    Buy Me A Coffee

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games