// built with processing 3.0a1 // Experimental settings boolean isDebug; int numTrial; int fixationDuration; int judgementDuration; color backgroundColor; color textColor; // Variables int currentState; // variable for controling state int currentTrial; // current trial id int baseTime; // variable for controling state duration int[] response; // variable for storing participant's response int[] RT; // variable for storing RT (reaction time) PFont font; color diskColorLeft; color diskColorRight; void setup(){ size( 800, 600 ); frameRate( 60 ); // Define experimental settings isDebug = false; numTrial = 3; fixationDuration = 1000; // [milliseconds] judgementDuration = 4000; // [milliseconds] backgroundColor = 0; // gray-scale value from 0 (black) to 255 (white) textColor = 255; // gray-scale value from 0 (black) to 255 (white) // initialize valuables currentState = 0; currentTrial = 0; baseTime = 0; response = new int[ numTrial ]; RT = new int[ numTrial ]; font = createFont( "Georgia", 24 ); textFont( font ); } void draw() { background( backgroundColor ); if( currentState == 0 ){ titlePhase(); } else if( currentState == 1 ){ fixationPhase(); } else if( currentState == 2 ){ responsePhase(); } else if( currentState == 3 ){ endPhase(); } if( isDebug ){ drawDebugInfo(); } } void titlePhase(){ // draw message fill( textColor ); text( "Press Enter button to start experiment.", 100, height * 0.8 ); } void fixationPhase(){ // draw fixation cross stroke( 200 ); // define gray scale color (0 to 255) of lines strokeWeight( 3 ); line( width/2 - 10, height/2, width/2 + 10, height/2 ); // orizontal line line( width/2, height/2 - 10, width/2, height/2 + 10 ); // vertical line // check elapsed time to transit state int elapsedTime = millis() - baseTime; if( elapsedTime > fixationDuration ){ transitState(); } } void responsePhase(){ // draw message fill( textColor ); text( "Which circle is brighter?", 250, 100 ); // draw stimuli noStroke(); fill( diskColorLeft ); ellipse( width/2 - 200, height/2, 100, 100 ); fill( diskColorRight ); ellipse( width/2 + 200, height/2, 100, 100 ); // check elapsed time to transit state int elapsedTime = millis() - baseTime; if( elapsedTime > judgementDuration ){ transitState(); } } void endPhase(){ // draw message fill( textColor ); text( "Thank you for your time!", 200, height * 0.8 ); } void transitState(){ if( currentState == 1 ){ currentState = 2; baseTime = millis(); diskColorLeft = (int)random( 255 ); diskColorRight = (int)random( 255 ); } else { if( currentTrial == numTrial - 1 ){ // if all the trials have done, save data and transit to state 3. saveData(); currentState = 3; } else { // move on to next trial currentTrial++; currentState = 1; baseTime = millis(); } } } void keyPressed() { if ( key == ENTER || key == RETURN ){ if( currentState == 0 ){ currentState = 1; baseTime = millis(); } } else if ( keyCode == LEFT ) { if( currentState == 2 ){ // record performance response[ currentTrial ] = 1; RT[ currentTrial ] = millis() - baseTime; // transit state transitState(); } } else if ( keyCode == RIGHT ) { if( currentState == 2 ){ // record performance response[ currentTrial ] = 2; RT[ currentTrial ] = millis() - baseTime; // transit state transitState(); } } } void saveData(){ String fileName = "data/result.txt"; String[] dataStrings = new String[ numTrial ]; for( int i=0; i < numTrial; i++ ){ dataStrings[ i ] = nf( response[i], 1 ) + "\t" + nf( RT[i], 4 ); } saveStrings( fileName, dataStrings ); } void drawDebugInfo(){ float elapsedTime = (millis() - baseTime) / 1000.0; fill( textColor ); // set font color text("currentState: " + currentState, 20, 25 ); text("currentTrial: " + currentTrial, 20, 50 ); text("elapsedTime: " + nf( elapsedTime, 1, 2 ), 20, 75 ); }