How to count number of clicks per trial?

In my experiment, participants are supposed to click the left mouse button only once per trial. I want to use a code component to count the number of clicks.

I put the following code in the ‘each frame’ tab:

if checkResp == 1:
     if timer.getTime() < trialEnd:
           if mouse.getPressed()[0]==1:
                clickCount = 1
                actionOnset = timer.getTime()
                tone.play()
                toneOnset = actionOnset + 0.25
                tonePlayed=t
                checkResp=0


if checkResp==0:
    if mouse.getPressed()[0]==1:
        clickCount +=1
        checkResp=2

Note: checkResp starts at 1, the participants makes a click, and checkResp is then set to 0. Once checkResp is 0, the code starts checking if any more clicks are made and adds these to clickCount. The next routine then gives an onscreen reminder message only if clickCount is greater than 1.

However, even after the trial is completely properly and only 1 click is ever made on the trial, the clickCount ends up being 2 as if two clicks were made.

How do I solve this?

Thank you!

checkResp is set to 1 in Begin Routine I assume.

Don’t you need some code to set checkResp back to 0 when no mouse button is down?

Why are you using both t and timer.getTime()?

Hi! Thank you for your reply!

Yes, checkResp is 1 in Begin Routine.

This is my ‘each frame’ tab:

if cond == 1 or cond == 2:
    if checkResp == 1:
        if timer.getTime() < trialEnd:
            if mouse.getPressed()[0]==1:
                clickCount = 1
                actionOnset = timer.getTime()
                tone.play()
                toneOnset = actionOnset + 0.25
                tonePlayed=t
                checkResp=0
                
        elif timer.getTime() > trialEnd:
            noResp=1
            continueRoutine = False   
            
            
    elif tonePlayed>0 and (t>(tonePlayed+trialEndGap)):
        noResp=0
        continueRoutine = False



elif cond == 3:
    if checkResp == 1:
        tonePlayed2 = t
        checkResp=2
        if mouse.getPressed()[0]==1:
            clickedOn3 = 1
        
    elif checkResp==2:
        if tonePlayed2>0 and (t>(tonePlayed2+2.5)):
            tone.play()
            toneOnset = timer.getTime()
            noResp=0
            tonePlayed=t
            checkResp=0
            
    elif tonePlayed>0 and (t>(tonePlayed+trialEndGap)):
        continueRoutine = False
            
    
elif cond == 4:
    if checkResp == 1:
        if timer.getTime() < trialEnd:
            if mouse.getPressed()[0]==1:
                clickCount = 1
                actionOnset = timer.getTime()
                noResp=0
                tonePlayed=t
                checkResp=0
                
        elif timer.getTime() > trialEnd:
            noResp=1
            continueRoutine = False   
            
            
    elif tonePlayed>0 and (t>(tonePlayed+trialEndGap)):
        noResp=0
        continueRoutine = False
                   
if checkResp==0:
    if mouse.getPressed()[0]==1:
        clickCount +=1
        checkResp=3

The code looks for an initial click, then sets checkResp to 0. This happens, no matter the conditions (cond). I then tried to make it so that, once the initial click had been made and checkResp was now 0, the code started looking for extra clicks.

Previously, I didn’t include checkResp =3 (or =2 in my above post). This resulted in clickCount increasing by 1 each frame. I then added checkResp = 3, so that the code only looked for one extra click. I did this because it doesn’t really matter how many extra clicks occur after the initial click. I was just trying out different things to see if it would help the situation, but it did not. countClick finished on 2, even if only the initial click is made with no additional clicks on the trial.

What do I need to do here?
Am I right in thinking that mouse.getPressed()[0] will only temporarily ==1 whilst the left mouse button is down?

Also, there’s no reason for me using both t and timer.getTime() other than I wrote those two parts of the code on different days, looking at two different forum posts for help whilst I wrote the code and hadn’t changed either one to match the other, haha.

That is my understanding.

Thank you for your reply!

I’m wondering why my code isn’t working then. It is almost behaving as if the mouse is being constantly held down, even though it’s not. I’m unsure what could be is causing this. Perhaps a bug?

I seem to have solved the issue…sort of!

if cond == 1 or cond == 2:
    if checkResp == 1:
        if timer.getTime() < trialEnd:
            if mouse.getPressed()[0]==1:
                actionOnset = timer.getTime()
                tone.play()
                toneOnset = actionOnset + 0.25
                tonePlayed=t
                checkResp=0
                
        elif timer.getTime() > trialEnd:
            noResp=1
            continueRoutine = False   
            
            
    elif tonePlayed>0 and (t>(tonePlayed+trialEndGap)):
        noResp=0
        continueRoutine = False



elif cond == 3:
    if checkResp == 1:
        tonePlayed2 = t
        checkResp=2
        if mouse.getPressed()[0]==1:
            clickedOn3 = 1
        
    elif checkResp==2:
        if tonePlayed2>0 and (t>(tonePlayed2+2.5)):
            tone.play()
            toneOnset = timer.getTime()
            noResp=0
            tonePlayed=t
            checkResp=0
            
    elif tonePlayed>0 and (t>(tonePlayed+trialEndGap)):
        continueRoutine = False
            
    
elif cond == 4:
    if checkResp == 1:
        if timer.getTime() < trialEnd:
            if mouse.getPressed()[0]==1:
                clickCount = 1
                actionOnset = timer.getTime()
                noResp=0
                tonePlayed=t
                checkResp=0
                
        elif timer.getTime() > trialEnd:
            noResp=1
            continueRoutine = False   
            
            
    elif tonePlayed>0 and (t>(tonePlayed+trialEndGap)):
        noResp=0
        continueRoutine = False
        
if checkResp==0:
    if mouse.getPressed()[0]==1:
        clickCount +=1
        print(clickCount)
        checkResp=3

So, before, after the initial click, clickCount would be set to 1. I removed clickCount=1 from this section of the code. clickCount then stays as 0 (it’s set to 0 at the beginning of the routine).

It now works as intended. clickCount == 1 if only the initial click was made, and ==2 if any additional clicks were made on the trial.

I feel like this isn’t the ‘proper’ solution…but it seems to work at least :slight_smile:

Sadly, this solution randomly stopped working.