psychopy.org | Reference | Downloads | Github

Minimum value of the maxWait in event.waitKeys ()

In my application, I am trying to toggle the LED lights for a predefined time like 30 seconds. For that, I am using a while loop and it must break once the time.time() function reaches predefined values irrespective of any key press, but my while loop stays on even though time.time() reaches a predefined time. The reason is code stays at first event.waitKeys() for an infinite amount of time. I got to know it is possible to define the waiting time. so,
I am curious to know the minimum value I can give for maxWait in event.waitKeys() function?

while ((time.time() < Mytimer)):     
    
        if  myChannel == '1': 
            if u'i' in event.waitKeys(maxWait = 1,):
                command = arduino.write(struct.pack(u'>H',513)) 
                myTime = myTime - 1
                instruction1.draw() 
                win.flip()
            if u'e' in event.waitKeys(maxWait = 1,):
                command = arduino.write(struct.pack(u'>H',512))
                myTime = myTime - 1
                instruction2.draw()
                win.flip()

waitKeys() isn’t really compatible with embedding within a loop that is based on time, as waitKeys() itself will wait for an indeterminate period: even though you specify a maximum period, this may still lead to the value in your while condition being exceeded. In this case, you should really be using getKeys(), to just instantaneously check the keyboard for a response: https://www.psychopy.org/api/event.html

e.g. in your code, you subtract 1 from the time value, but you don’t know that wait keys() actually took 1 second: it would have returned earlier if a key was pressed. It would be better if you left the value of the criterion in the while check unaltered.

Also note that your while condition refers to Mytimer, but the rest of the code refers to myTime.

Lastly, you should probably create a dedicated PsychoPy Clock object for tasks like this, rather than rely on time.time(), as clocks can be reset and can even countdown for you: https://www.psychopy.org/api/core.html

e.g.

from psychopy import core

timer = core.CountdownTimer(30)
while timer.getTime() > 0:  # after 30 s will become negative
    # do stuff

Your suggestions worked perfectly. Thank you for the help.