Task-switching experiments are often a bit more complex than single task experiments, but can easily be run in PsyToolkit (especially because developer Gijsbert Stoet does research on task switching).

Background

Task switching paradigms became really popular in experimental cognitive psychology in the mid 1990s. In short, participants have to rapidly switch between two different cognitive tasks. Whenever people make a switch from doing one task to another, they get much slower.

Before you read on, read more about task-switching and try the experiment explained here yourself first. Read about and do a task-switching experiment.

Preparation

For this experiment, you need a whole bunch of little images of the letters and numbers used, the presented grid, and the instructions. You can, of course, all download these from the link above.

Idea behind the code

In most experiments, it is easiest to put each experimental condition in a table, which each row of the table representing the information needed for one condition. In this example, though, we use a very different approach. The conditions are created in the task statement itself, and we use the random function for that. Further, we also make sure that the same stimuli are not repeated between trials (because that makes things too easy for participants). For the latter, we use a while/while-end loop.

The ideas behind the code are as follows:

  • Stimuli need to be presented in the four quadrants of a 2x2 grid, and to do that, we have a global variable pos, referred to &pos, because global variables always start with the & symbol as a matter of PsyToolkit convention. The variable pos can have 4 values (1 = topleft, 2=topright, 3=bottomright, 4=bottomleft), and the variable is increased up to value 4 each trial, and when it is 4, it is set back to 1.

  • Each stimulus is the combination of a letter and a number, such as P8 or A1. The letters and numbers are chosen using a random function, choosing values between 1 and 8 (because we have 8 letters and 8 numbers available). Because bitmaps start with number 1 (e.g., bitmap "pg" is the letter "G", and corresponds to the first bitmap), so if you say "show bitmap 1", it actually is the same as "show bitmap pg".

  • A while loop is used to make sure the letter/numbers of the previous task are not used.

  • The global variable in the block and task justtask helps to do just one task, or to do both tasks.

  • In-task code keeps track of whether a trial is a task-switch or a task-repeat trial. This is then "saved" in the data, and used in the "feedback".

  • At the end of the blocks, participants get feedback about their performance. For this, we use the feedback section, which might look somewhat cryptic at first, but is highly flexible.

  • We use a pager to show all the different instruction screens. The advantage of the pager is that people can browser forward and backward using the arrow keys of the keyboard. If you use this, you need to make sure that you tell participants which keys to use. By default, the letter q gets you out of the instruction, the arrows and space gets your forward/backward.

Full code

# A slightly modified version of the alternate runs task switching
# paradigm: Rogers and Monsell, Journal of Experimental Psychology:
# General, 124, 207-231 (2003).

# grid position 1 is top left, and then clockwise
# task 1 is in top quadrants
# task 2 in bottom quadrants

#### this code does not use a table, but the conditions are calculated
#### with helper variables. The code looks a bit more complicated, but
#### the reason is that neither letters nor numbers should repeat
#### themselves in consecutive trials. This seems the most efficient
#### way of coding it.

options
  bitmapdir stimuli
  escape
  variable dummy 0

##
# G K M R  A E I U
# 3 5 7 9  2 4 6 8
# L L L L  R R R R

bitmaps
  ############### stimuli used in tasks
  pg
  pk
  pm
  pr
  pa
  pe
  pi
  pu
  p3
  p5
  p7
  p9
  p2
  p4
  p6
  p8
  ## other stimuli
  grid
  task1
  task2
  ready
  instructions1
  instructions2
  instructions3
  instructions4
  instructions5
  readyletters
  readynumbers
  readylettersnumbers
  thankyou

fonts
  courier 20

######################################################################
# TASK SWITCHING BASED ON ROGERS/MONSELL 1995

# letters 1-8 and numbers 1-8.
# G K M R  A E I U     bitmaps 1-8
# 3 5 7 9  2 4 6 8     bitmaps 9-16 (i.e. 1-8 +8)
# L L L L  R R R R     response

task lettersnumbers
  keys b n
  ## instead of table, determine stimulus/response here #########
  set $letter random 1 8 # select a random letter
  set $number random 1 8 # select a random number
  # keep selecting until letter and number are different from previous trial
  while $letter == &previousletter || $number == &previousnumber
    set $letter random 1 8
    set $number random 1 8
  while-end
  set &previousletter $letter # keep for the next trial
  set &previousnumber $number # keep for the next trial
  set $numberbitmap expression $number + 8 # number bitmaps run from 9-16
  # associate responses for both possible tasks #################
  set $lettertaskresponse 1   # left button
  if $letter > 4
    set $lettertaskresponse 2 # right button
  fi
  set $numbertaskresponse 1   # left button
  if $number > 4
    set $numbertaskresponse 2 # right button
  fi
  ###############################################################
  show bitmap grid
  delay 150 # ITI (RS = 150)
  # which quadrant is being used? this goes clockwise
  # top left is 1, up to four
  set &pos increase 1
  if &pos > 4
    set &pos 1
  fi
  ########## you can also do just one task then you use only 2 quadrants
  if &justtask == 1
    if &pos > 2
      set &pos increase -2
    fi
  fi
  if &justtask == 2
    if &pos < 3
      set &pos increase 2
    fi
  fi
  # determine position 1 and 2 of stimuli (letter is left or number)
  if &pos == 1           # letter task
    set $xpos1 -50
    set $xpos2 -25
    set $ypos  -33
    set $key $lettertaskresponse
    set $tasktype 1
  fi
  if &pos == 2           # letter task
    set $xpos1 25
    set $xpos2 50
    set $ypos  -33
    set $key $lettertaskresponse
    set $tasktype 1
  fi
  if &pos == 3           # number task
    set $xpos1 25
    set $xpos2 50
    set $ypos  33
    set $key $numbertaskresponse
    set $tasktype 2
  fi
  if &pos == 4           # number task
    set $xpos1 -50
    set $xpos2 -25
    set $ypos  33
    set $key $numbertaskresponse
    set $tasktype 2
  fi
  # set up screen
  draw off
    show bitmap $letter $xpos1 $ypos
    show bitmap $numberbitmap $xpos2 $ypos
  draw on
  readkey $key 5000
  if STATUS != CORRECT
    if $tasktype == 1
      show bitmap task1 210 -50
    fi
    if $tasktype == 2
      show bitmap task2 210 40
    fi
    delay 3000
    clear -1
  fi
  clear 2 3
  # code if this trial was a task switch or task repeat trial
  if &previoustask == $tasktype
    set &taskswitch 0
  fi
  if &previoustask != $tasktype
    set &taskswitch 1
  fi
  set &previoustask $tasktype
  # save data
  save BLOCKNAME &pos $tasktype $letter $number &justtask &taskswitch STATUS RT TT

######################################################################
#			   BLOCKS START HERE                         #
######################################################################

block lettersnumbers
 pager instructions1 instructions2 instructions3 instructions4 instructions5
 set &previoustask -1
 set &taskswitch 0 # 1 if a trial is a task switch trial
 set &justtask 1 # this makes that only the letter task is being selected
 bitmap readyletters ## instruction
 wait_for_key
 tasklist
   lettersnumbers 40
 end

block colorsshapes
 set &justtask 2 # this makes that only the number task is being selected
 bitmap readynumbers
 wait_for_key
 tasklist
   lettersnumbers 40
 end

block mixed
 set &justtask 0 # this makes that both tasks are in alternative run sequence
 bitmap readylettersnumbers
 wait_for_key
 tasklist
   lettersnumbers 40
 end
 ## now create the feedback for participants. Note that c9 refers to
 ## the 9th column, c8 to the eight, etc.
 feedback
  label 0 0 "Response times (ms):"
  text 0 50  mean c9 ; prefix "Pure blocks                    :" ; select c8 == 1 && c6 != 0 && c7 == 0
  text 0 100 mean c9 ; prefix "Mixed block, task-repeat trials:" ; select c8 == 1 && c6 == 0 && c7 == 0
  text 0 150 mean c9 ; prefix "Mixed block, task-switch trials:" ; select c8 == 1 && c6 == 0 && c7 == 1
 end

See it in action