Building a "Clock" for participants to choose time like setting an alarm

Hi Everyone,

I’m trying to figure out how to build something like the following in PsychoPy so that participants would be able to easily set a time as a submitted answer (e.g., I do sleep research and something as simple as asking participants what time they went to bed can lead to 10pm, 10:00, 22h, 22:00, etc). Ideally this would be online/via Pavlovia but offline as a start would be fine.

The idea would be the image below, where participants are able to switch the selected number with up/down arrows on a keyboard, then use left/right to move onto the other number, finally pressing enter to submit the chosen time. Any thoughts on how to do this?

Thanks!

Hello,
Is it an online/offline experiment? Is this supposed to be changed with a keypress/touch?
Some details are missing that. Without them, it’s impossible to understand how easy/difficult it will be to create.

Chen

Thanks for the response, Ideally online, but I think starting it offline with a keyboard press is where I’m thinking, got no plans for touch screen as that sounds much more like a nightmare!

I’ve edited the original post as well to include these details.

Hello,
I created an offline version of the functionality you have asked for, If you want to convert it to an online experiment, it should be relatively easy; you will need to change the keyboard.
But what is important here is the functionality.

The routine:

First of all, at the start of the routine, we set the following code:

kb = keyboard.Keyboard()

toggleHours = True

hours = []
for i in range(24):
    hours.append(str(i).rjust(2, '0'))

hourCounter = 0

minutes = []
for j in range(60):
    minutes.append(str(j).rjust(2, '0'))

minuteCounter = 0

kb - Set the keyboard
toggleHours - Toggle the control between the hours and the minutes
hours and minutes - An array with all the minutes and hours, e.g. [“00”, “01”, “02”, “03”…]
hourCounter and minuteCounter - Counting the number of presses for the minutes and hours.

Then, we set the following in Every Frame:

keys_pressed = kb.getKeys(['up', 'down', 'right', 'left'], waitRelease=False, clear=True)

for key in keys_pressed:
    
    if 'right' == key or 'left' == key:
        toggleHours = not toggleHours
        
    if toggleHours:
        if 'up' == key:
            hourCounter= (hourCounter - 1) % 24
    
        elif 'down' == key:
            hourCounter= (hourCounter + 1) % 24
            
    else:
        if 'up' == key:
            minuteCounter= (minuteCounter - 1) % 60
    
        elif 'down' == key:
            minuteCounter= (minuteCounter + 1) % 60

We define the keys pressed in each frame (keys_pressed).
We then iterate through the pressed keys (because the .getKeys method returns an array).
If the right/left key were pressed, we toggle the toggleHours variable. When it’s true, we control the hours; when it’s false, we control the minutes.
When the up/down key is pressed, we set the hourCounter to be equal to itself minus/plus one. We also use the % operator to get the remainder of the hourCounter (for example, if the down key was pressed 24, we want to return to 0). The same logic goes to the minuteCounter.

So now, after we define our counters, we can return to the routine and set the components.
I will take the hours components as an example:

For the hour component, we want to take an hour, which is in the hourCounter position in the hours array.
So if our counter equals 5, we will take the sixth item from the hours array, which is “06”.

For the hour_plus and hour_minus components, we use the same logic, but we add/subtract one:


We also need to use the % operator again in case the hourCounter is out of range.

And there you go:

Hope it helps,
Chen

You can download it here:
dc.psyexp (20.1 KB)

2 Likes

You are amazing, thank you so much! Going to become a staple of my experiments :grinning:

2 Likes

That’s great to hear! Please don’t forget to acknowledge Chen Brilling in any papers you write using it.

2 Likes

Happy to help :slight_smile: