Hi I have a n-back task which detects spacebar keypresses using the following code.
# Setting up trial components
nback_clock = core.Clock()
nback_clock2 = core.Clock()
nback_kb1 = keyboard.Keyboard()
nback_kb2 = keyboard.Keyboard()
fix_cros = TextStim(win, text='+', height=0.15)
# Loading stimuli
nback_letters1 = pd.read_csv('nback_letters1.csv')['letters'].tolist()
nback_corrAns1 = pd.read_csv('nback_letters1.csv')['corrAns'].tolist()
nback_list1 = list(chunks(nback_letters1, 48))
nback_corrAns = list(chunks(nback_corrAns1, 48))
# Data saving
nback_results = pd.DataFrame(columns = ['Trial number',
'Stimulus presented',
'Key pressed',
'Correct answer',
'Response',
'Reaction time'])
# Begin trials for n-back 1
for i in nback_list1:
for j in range(0, len(i)):
nback1_start = datetime.now()
if kb.getKeys(keyList=['escape']):
shutdown()
corrAns = nback_corrAns1[j]
thisLetter = nback_letters1[j]
letter_present = TextStim(win, text=nback_letters1[j], height=0.5)
fixation_cross(0.5)
if j != 0:
# Use clock-based timing
nback_clock.reset()
start_marker_sent = False
tStart =nback_clock.getTime()
nback_kb1.clock.reset()
while nback_clock.getTime() < 2.5:
if nback_clock.getTime() < 0.5:
letter_present.draw()
if start_marker_sent == False:
win.callOnFlip(port.write, 'F'.encode())
start_marker_sent = True
else:
blank_.draw()
win.flip()
keys_n = nback_kb1.getKeys(keyList = ['space'])
tResp = nback_clock.getTime()
response = 'None' if not keys_n else keys_n[-1]
port.write('f'.encode())
# Get responses
if keys_n:
RT = (tResp - tStart)-0.5
if corrAns == response or str(corrAns) == response:
answer = 1
elif corrAns != response:
answer = 0
elif response == 'None':
RT = 0
if corrAns == '' or corrAns == [] or corrAns == None or corrAns == 'None':
answer = 1
else:
answer = 0
else:
answer = 'error'
# Saving trial data
thisExp.addData('N back RT',RT)
thisExp.addData('Stimulus presented',thisLetter)
thisExp.addData('N back response',answer)
thisExp.addData('N back start',nback1_start)
thisExp.addData('Key pressed', response)
thisExp.addData('Trial number', j)
thisExp.nextEntry()
# Saving response data
nback_results = nback_results.append({'Trial number': j,
'Stimulus presented': thisLetter,
'Key pressed': response,
'Response': answer,
'Reaction time': RT}, ignore_index=True)
else:
nback_clock.reset()
while nback_clock.getTime() < 2.5:
if nback_clock.getTime() < 0.5:
letter_present.draw()
else:
blank_.draw()
win.flip()
When I then look at the csvs it says response = None and that a key wasn’t pressed. Why is this happening? I would appreciate any help Thank you!