People often learn code most easily from examples. This is a simple example, which explains some of the basics

Background

The Simon effect is a well know effect in cognitive experimental psychology. There are many papers about this effect and it can be implemented in slightly different ways.

In this example, we have two stimuli that participants have to respond to, and these two stimuli can each appear in two different positions on the screen, which leads to four different experimental conditions:

  1. A red rectangle and positioned left of fixation point (compatible)

  2. A green rectangle and positioned right of fixation point (compatible)

  3. A red rectangle and positioned right of fixation point (incompatible)

  4. A green rectangle and positioned left fixation point (incompatible)

In this task, people need to respond with a left button press (the B on regular keyboard) to a red rectangle, no matter where it appears. Green rectangles require the N key on the keyboard.

Conditions 1 and 2 are called "compatible", because there is a straightforward correspondence between the response (left or right) and the location on the screen (left or right).

We know that people respond more slowly and less accurately in incompatible conditions.

Preparation

In most experiments, you would use image files for stimuli, but in this experiment we do not. In this experiment, the stimuli are the most basic PsyToolkit stimuli, namely colored rectangles.

Colors

It is important to understand that if you want to tell the computer what color to use, you need to use the RGB coding scheme.

This might look somewhat complex, but it is actually really simple. Each color on a computer screen is made of three basic colors, namely a mix of red, green, and blue. Each color can be given a value between 0 and 255. Here are some examples:

In this experiment, there are rectangles of various different colors.

  • A pure red stimulus has RGB code 255 0 0. That is, red is full on, whereas for green and blue we have values zero, which means that there is no green and blue involved at ll.

  • A pure green stimulus has RGB code 0 255 0. Thus, only the green channel is on, whereas red and blue are on zero.

  • Yellow is a mix of red and green, but no blue, thus that is 255 255 0

Positions

Stimuli can be presented at different locations. A position is always expressed in a horizontal (X) and vertical (Y) coordinate. The center of the screen is 0 0. Thus, if you want to show a rectangle in at the center of the screen, it should be positioned at 0 0. If you want to present a stimulus left of the center, you might choose -200 0.

Code

Complete Simon task
table simontasktable # table with four rows, each trial one row is randomly chosen
#-name of condition--------------position--red?--green?--correct key--------------
# @1                             @2        @3    @4      @5
#---------------------------------------------------------------------------------
  "leftresponse  compatible"     -200      255   0       1
  "leftresponse  incompatible"    200      255   0       1
  "rightresponse compatible"      200        0   255     2
  "rightresponse incompatible"   -200        0   255     2

task simon
  table simontasktable
  keys b n
  show rectangle 0 0 10 10 255 255 255
  delay 200
  clear 1
  delay 50
  show rectangle @2 0 100 100 @3 @4 0
  readkey @5 5000
  clear 2
  if STATUS != CORRECT
    show rectangle 0 0 10 10 255 255 255
    delay 100
    show rectangle 0 0 20 20 255 255   0
    delay 100
    show rectangle 0 0 30 50 255 255 255
    delay 100
    show rectangle 0 0 50 50 255 255   0
    delay 100
    clear 3 4 5 6
  fi
  delay 100
  save BLOCKNAME @1 TABLEROW KEY STATUS RT

block test
  tasklist
    simon 40
  end

The code explained

Sections

First of all, note that there are different possible sections (not all used in this example):

  1. bitmaps

  2. fonts

  3. table

  4. task

  5. message

  6. block

Sections are separated from one another by an empty line!

The table explained

table simontasktable # table with four rows, each trial one row is randomly chosen
#-name of condition--------------position--red?--green?--correct key--------------
# @1                             @2        @3    @4      @5
#---------------------------------------------------------------------------------
  "leftresponse  compatible"     -200      255   0       1
  "leftresponse  incompatible"    200      255   0       1
  "rightresponse compatible"      200        0   255     2
  "rightresponse incompatible"   -200        0   255     2

The task explained

task simon
  table simontasktable
  keys b n
  show rectangle 0 0 10 10 255 255 255
  delay 200
  clear 1
  delay 50
  show rectangle @2 0 100 100 @3 @4 0
  readkey @5 5000
  clear 2
  if STATUS != CORRECT
    show rectangle 0 0 10 10 255 255 255
    delay 100
    show rectangle 0 0 20 20 255 255   0
    delay 100
    show rectangle 0 0 30 50 255 255 255
    delay 100
    show rectangle 0 0 50 50 255 255   0
    delay 100
    clear 3 4 5 6
  fi
  delay 100
  save BLOCKNAME @1 TABLEROW KEY STATUS RT

Each line does something, and this will now be explained line for line.

task simon

Each task must have a name. You can choose anything, but it must not have any spaces or special characters such as & or !.

Good examples:

  • simon

  • simon_task

  • simon1

Bad examples, which will lead to an error:

  • simon task

  • simon!

  • *simon*

  table simontasktable
  keys b n
  show rectangle 0 0 10 10 255 255 255
  delay 200
  clear 1
  delay 50
  show rectangle @2 0 100 100 @3 @4 0
  readkey @5 5000

This shows the rectangle. It chooses the X position from the row chosen in this trial. Imagine that the computer had randomly chosen the second row, that the value would be "200". In this case, a rectangle is then shown at position 200 0, which is right of the center. The height and width of the rectangle are 100 pixels. The color code would be 255 0 0, which is a pure red rectangle.

The readkey line tells us that a keypress must be given, that the computer will wait maximally 5000 milliseconds (that is 5 seconds), and that the expected key is defined in the 5th column of the currently chosen row. For row 2 that is key 1. That is the "n" key of the keyboard.

  clear 2

This wipes the second stimulus from the screen.

  if STATUS != CORRECT
    show rectangle 0 0 10 10 255 255 255
    delay 100
    show rectangle 0 0 20 20 255 255   0
    delay 100
    show rectangle 0 0 30 50 255 255 255
    delay 100
    show rectangle 0 0 50 50 255 255   0
    delay 100
    clear 3 4 5 6
  fi

The lines between if and fi are only done if the participant made no error. The != means "not correct". Thus the 9 lines are only carried out if the status of the preceding readkey statement were wrong. For example, because the participant pressed the wrong key or because the participant was too slow.

  delay 100

The line delay just always waits for a certain number of milliseconds, in this case 100 milliseconds.

  save BLOCKNAME @1 TABLEROW KEY STATUS RT

The save line is the last line of this task. In this case, it saves a number of variables to the datafile. Variables in capitals are built-in variable names. The @1 is the first element of the table, which in this case actually consists of two words (because the first column is a string containing two words).

  • BLOCKNAME The name of the block. In this experiment, there is only one block, so it will be the word "test".

  • TABLEROW Which row of the table was randomly selected in this specific trial? Because there are 4 rows, it is a number between 1 and 4.

  • KEY Which key was being pressed? This is a number.

  • STATUS Whether or not the keypress in the last readkey command was correct (1), wrong (2), or a timeout (3).

  • RT The reaction time in milliseconds of the last readkey command.

After the last line of your task, you need to enter an empty line. Empty lines are critically important to tell the computer which lines belong together, such as the lines of the task, and which sections are apart.

The block sections explained

block test
  tasklist
    simon 40
  end
  • In this part of the experiment, you describe groups of trials. The line starting with block has a blockname that you can choose

  • Between the lines tasklist and end, you can list the tasks that you want to be done. In this case, the task simon will be carried out 40 times. Thus, a participant will do 40 trials.

This example is very simple, the block syntax has all kinds of options, for example to carry out trials in a fixed list, to repeat conditions on error, to keep running until a minimum of trials has been carried out without making a mistake.

Data analysis

The produced data files can be imported into R using the read.table command. Alternatively, you can analyse it in SPSS, LibreOffice Calc, or Excel.

See it in action

This experiment can be viewed in action here: