How to create a countdown timer on the same screen as a stimulus?

Hi! I want to present a stimulus text e.g. “Rest” and below this text I want to have a timer counting down from 60 below this.

To try and do this, I’ve created a csv with numbers counting down from 60, is it easier to also have a csv with “Rest” repeated so I can loop through both and draw them? How would one implement this? Or is there an easier way to do this?

I would appreciate any help or direction, thank you! I’ve looked at other threads, however, these use builder exclusively. The code snippet is below :slight_smile:

for j in all_text:
  text = Text_stim(win, text = j)
  clock.reset()
       win.callOnFlip(port.write(trigger_start.encode())
    
       while clock.getTime() < 60:
          text.draw()
          win.flip()

  port.write(trigger_stop.encode())
1 Like

Hi There,

If this is a code component in builder then you could just add a text stim and use this in the text field:

$'Rest\n' + str(int(60 - t))

Thanks,
Becca

I am only using the coder where I’ve defined all the text
e.g.

rest_text = $'Rest\n' + str(int(60 - t))

So that I can draw it all later on in the expeirment. When I try and run it, psychopy just gives me “invalid syntax”…

This code snippet is designed to go directly within the text field of the textstim component - if you are specifying it in code then you can remove the dollar sign and use $rest_text in the text component

Thanks,
Becca

Ive got the following code which shows the countdown timer fine but the number stays on “9” and does not move, how do I get it to change every second? Thanks!

    j = 'Rest \n' + str(int(60-VFT_clock))
    presented_category = TextStim(win, text=j, height=0.2)
    VFT_clock.reset()  
    while VFT_clock.getTime() < 60:
        presented_category.draw()
        win.flip()

countdown.psyexp (5.9 KB)
I really recommend that you just use the code snippet inside a text component and set to update every frame - like this (it will be much easier)

For your version it would be:



    presented_category = TextStim(win, text='', height=0.2)
    VFT_clock.reset()  
    while VFT_clock.getTime() < 60:
        presented_category.text = 'Rest \n' + str(int(60-VFT_clock.getTime()))
        presented_category.draw()
        win.flip()

Hi @Becca , how can I make the countdown of the script "countdown.psyexp " goes in milliseconds instead of seconds? Thank you so much!

Hi There,

You’de just want to remove the int() argument, so the text would be something like $round(60-t, 3)

Hope that helps!
Becca

Dear @Becca
Thanks for the suggestion. It seems to work, but it shows every second (eg: 59,999). I need to countdown from 1000 milliseconds to 0. How can I do? Thank you!

Can you multiple this value by 1000?