psychopy.org | Reference | Downloads | Github

Visual Clock hidden from the subject and presents the total time at the end of the experiment

Hi all,

I have been able to run the code component and have been able to get the total time used by the subjects in responding to my experiments, which is working perfectly. Unfortunately, the time they are displaying are not the standard clock time (example: Total time : 13.75), and it is difficult to convert it into the standard time.

The code that I have created is:

At the end of the experiment, I have opened an ‘EndScreen’ Routine, which has the Code Component named ‘Code’, and the codes are:

#At the begin experiment
expClock = core.Clock()

#At the begin routine
msg = “Total time : %.2f” %(expClock.getTime()/60.0)

And then in the Text box, I am calling

$msg

which is ‘set at every repeat.’

My problem is, is there a better, more efficient and visually feasible and appealing way of getting the total time at the end of the experiment?

Thanks in advance!

@Shardul_Shankar, you could use globalClock in the same way, which is a built-in variable used to track the time since experiment started. Also, the getTime() displays times in seconds, so you do not need to convert the time to get the format you want (that is, if you want secs.millisecs).

Thank you for your reply @dvbridges.

It works perfectly! But I think my real problem that I missed writing in the question was that I wanted to show the clock throughout my experiment to the user on the top right hand side, hidden, but visible when called; so that they can track the time of their experiment whenever they want, and then at the end present the total time.

Is this possible?

Thanks again for your time!

Yes, that can be done. You might want something like this - the names I chose for components are just for demonstration purposes. Have a keyboard component called “resp”, and add ‘c’ to the allowed keys, where ‘c’ will be for Clock. Note, I set the keys to only store first key pressed. If you want all keys stored, let me know and we will have to change the code below. Add a text component called “theTimer” - where you have your msg variable presented, and set the opacity to 0 i.e., invisible. Add a code component and add the following to the relevant tabs:

# Begin Exp
timeToShow = 2  # How long to present clock in secs
startTime = None
msg = 0

# Every Frame
if resp.keys == 'c':
    startTime = t  # Get timer presentation start time
    resp.keys = []  # reset keys so 'c' for Clock is not there anymore
    theTimer.opacity = 1  # set the clock opacity to 1 - i.e., visible
    
if startTime is not None:
    msg = "Total time : %.2f" % (globalClock.getTime())  # Update msg var
    if t > startTime + timeToShow:  # Condition to stop the clock presentation
        theTimer.opacity = 0
        startTime = None

# End Routine
 finalTime = "Total experiment time : %.2f" % (globalClock.getTime()) 

You then need to add another routine at the end of the experiment with a text component, where you present the finalTime variable in a text component.

I have tried what you said in the code @dvbridges , but for some reason, the visibility of the clock is not happening on pressing ‘c’, and also, the ‘finalTime’ is presenting a weird time of over 5 mins when the experiment is not even running for 30 secs. I am attaching the code and the excel file so that you can see what the error is, as I am not able to find it out.

Thanks for the help!

demo for 3.psyexp (17.7 KB)
image_stimuli_Test3.xlsx (9.4 KB)
Main_Stimuli_Test3.xlsx (9.8 KB)

@Shardul_Shankar, you will need to put the code component in the routines that run the trials, currently your timer code only runs for the first routine. Also, you do not need to define globalClock, it is defined at the beginning of the experiment - compile the script and take a look - search for globalClock to see where PsychoPy defines it.