text: ﻿/******************** 
   * Gonogotest2 Test *
   ********************/
  
  // init psychoJS:
  const psychoJS = new PsychoJS({
    debug: true
  });
  
  // open window:
  psychoJS.openWindow({
    fullscr: true,
    color: new util.Color([0, 0, 0]),
    units: 'height',
    waitBlanking: true
  });
  
  // store info about the experiment session:
  let expName = 'goNogoTest2';  // from the Builder filename that created this script
  let expInfo = {'participant': '', 'session': '001'};
  
  // schedule the experiment:
  psychoJS.schedule(psychoJS.gui.DlgFromDict({
    dictionary: expInfo,
    title: expName
  }));
  
  const flowScheduler = new Scheduler(psychoJS);
  const dialogCancelScheduler = new Scheduler(psychoJS);
  psychoJS.scheduleCondition(function() { return (psychoJS.gui.dialogComponent.button === 'OK'); }, flowScheduler, dialogCancelScheduler);
  
  // flowScheduler gets run if the participants presses OK
  flowScheduler.add(updateInfo); // add timeStamp
  flowScheduler.add(experimentInit);
  flowScheduler.add(instructionsRoutineBegin());
  flowScheduler.add(instructionsRoutineEachFrame());
  flowScheduler.add(instructionsRoutineEnd());
  const trials1LoopScheduler = new Scheduler(psychoJS);
  flowScheduler.add(trials1LoopBegin, trials1LoopScheduler);
  flowScheduler.add(trials1LoopScheduler);
  flowScheduler.add(trials1LoopEnd);
  flowScheduler.add(instructions2RoutineBegin());
  flowScheduler.add(instructions2RoutineEachFrame());
  flowScheduler.add(instructions2RoutineEnd());
  const trials2LoopScheduler = new Scheduler(psychoJS);
  flowScheduler.add(trials2LoopBegin, trials2LoopScheduler);
  flowScheduler.add(trials2LoopScheduler);
  flowScheduler.add(trials2LoopEnd);
  flowScheduler.add(instructions2RoutineBegin());
  flowScheduler.add(instructions2RoutineEachFrame());
  flowScheduler.add(instructions2RoutineEnd());
  const trials3LoopScheduler = new Scheduler(psychoJS);
  flowScheduler.add(trials3LoopBegin, trials3LoopScheduler);
  flowScheduler.add(trials3LoopScheduler);
  flowScheduler.add(trials3LoopEnd);
  flowScheduler.add(endRoutineBegin());
  flowScheduler.add(endRoutineEachFrame());
  flowScheduler.add(endRoutineEnd());
  flowScheduler.add(quitPsychoJS, '', true);
  
  // quit if user presses Cancel in dialog box:
  dialogCancelScheduler.add(quitPsychoJS, '', false);
  
  psychoJS.start({
    expName: expName,
    expInfo: expInfo,
    });
  
  
  var frameDur;
  function updateInfo() {
    expInfo['date'] = util.MonotonicClock.getDateStr();  // add a simple timestamp
    expInfo['expName'] = expName;
    expInfo['psychopyVersion'] = '2020.1.3';
    expInfo['OS'] = window.navigator.platform;
  
    // store frame rate of monitor if we can measure it successfully
    expInfo['frameRate'] = psychoJS.window.getActualFrameRate();
    if (typeof expInfo['frameRate'] !== 'undefined')
      frameDur = 1.0 / Math.round(expInfo['frameRate']);
    else
      frameDur = 1.0 / 60.0; // couldn't get a reliable measure so guess
  
    // add info from the URL:
    util.addInfoFromUrl(expInfo);
    
    return Scheduler.Event.NEXT;
  }
  
  
  var instructionsClock;
  var instr1;
  var instr1Resp;
  var trialClock;
  var trialBlue;
  var trialOrange;
  var trialFix;
  var trialResp;
  var instructions2Clock;
  var instr2;
  var instr2Resp;
  var endClock;
  var thanks;
  var endResp;
  var globalClock;
  var routineTimer;
  function experimentInit() {
    // Initialize components for Routine "instructions"
    instructionsClock = new util.Clock();
    instr1 = new visual.TextStim({
      win: psychoJS.window,
      name: 'instr1',
      text: "'Space' for blue, nothing for orange. 'S' to start.",
      font: 'Arial',
      units: undefined, 
      pos: [0, 0], height: 0.05,  wrapWidth: undefined, ori: 0,
      color: new util.Color('white'),  opacity: 1,
      depth: 0.0 
    });
    
    instr1Resp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
    
    // Initialize components for Routine "trial"
    trialClock = new util.Clock();
    trialBlue = new visual.Polygon ({
      win: psychoJS.window, name: 'trialBlue', 
      edges: 180, size:[0.5, 0.5],
      ori: 0, pos: [0, 0],
      lineWidth: 1, lineColor: new util.Color([(- 1.0), (- 1.0), 1.0]),
      fillColor: new util.Color([(- 1.0), (- 1.0), 1.0]),
      opacity: 1.0, depth: 0, interpolate: true,
    });
    
    trialOrange = new visual.Polygon ({
      win: psychoJS.window, name: 'trialOrange', 
      edges: 180, size:[0.5, 0.5],
      ori: 0, pos: [0, 0],
      lineWidth: 1, lineColor: new util.Color([1.0, 0.004, (- 1.0)]),
      fillColor: new util.Color([1.0, 0.004, (- 1.0)]),
      opacity: 1.0, depth: -1, interpolate: true,
    });
    
    trialFix = new visual.TextStim({
      win: psychoJS.window,
      name: 'trialFix',
      text: '+',
      font: 'Arial',
      units: undefined, 
      pos: [0, 0], height: 0.05,  wrapWidth: undefined, ori: 0,
      color: new util.Color('white'),  opacity: 1,
      depth: -2.0 
    });
    
    trialResp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
    
    // Initialize components for Routine "instructions2"
    instructions2Clock = new util.Clock();
    instr2 = new visual.TextStim({
      win: psychoJS.window,
      name: 'instr2',
      text: "Again, 'space' for blue, nothing for orange. 'S' to start.",
      font: 'Arial',
      units: undefined, 
      pos: [0, 0], height: 0.05,  wrapWidth: undefined, ori: 0,
      color: new util.Color('white'),  opacity: 1,
      depth: 0.0 
    });
    
    instr2Resp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
    
    // Initialize components for Routine "trial"
    trialClock = new util.Clock();
    trialBlue = new visual.Polygon ({
      win: psychoJS.window, name: 'trialBlue', 
      edges: 180, size:[0.5, 0.5],
      ori: 0, pos: [0, 0],
      lineWidth: 1, lineColor: new util.Color([(- 1.0), (- 1.0), 1.0]),
      fillColor: new util.Color([(- 1.0), (- 1.0), 1.0]),
      opacity: 1.0, depth: 0, interpolate: true,
    });
    
    trialOrange = new visual.Polygon ({
      win: psychoJS.window, name: 'trialOrange', 
      edges: 180, size:[0.5, 0.5],
      ori: 0, pos: [0, 0],
      lineWidth: 1, lineColor: new util.Color([1.0, 0.004, (- 1.0)]),
      fillColor: new util.Color([1.0, 0.004, (- 1.0)]),
      opacity: 1.0, depth: -1, interpolate: true,
    });
    
    trialFix = new visual.TextStim({
      win: psychoJS.window,
      name: 'trialFix',
      text: '+',
      font: 'Arial',
      units: undefined, 
      pos: [0, 0], height: 0.05,  wrapWidth: undefined, ori: 0,
      color: new util.Color('white'),  opacity: 1,
      depth: -2.0 
    });
    
    trialResp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
    
    // Initialize components for Routine "instructions2"
    instructions2Clock = new util.Clock();
    instr2 = new visual.TextStim({
      win: psychoJS.window,
      name: 'instr2',
      text: "Again, 'space' for blue, nothing for orange. 'S' to start.",
      font: 'Arial',
      units: undefined, 
      pos: [0, 0], height: 0.05,  wrapWidth: undefined, ori: 0,
      color: new util.Color('white'),  opacity: 1,
      depth: 0.0 
    });
    
    instr2Resp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
    
    // Initialize components for Routine "trial"
    trialClock = new util.Clock();
    trialBlue = new visual.Polygon ({
      win: psychoJS.window, name: 'trialBlue', 
      edges: 180, size:[0.5, 0.5],
      ori: 0, pos: [0, 0],
      lineWidth: 1, lineColor: new util.Color([(- 1.0), (- 1.0), 1.0]),
      fillColor: new util.Color([(- 1.0), (- 1.0), 1.0]),
      opacity: 1.0, depth: 0, interpolate: true,
    });
    
    trialOrange = new visual.Polygon ({
      win: psychoJS.window, name: 'trialOrange', 
      edges: 180, size:[0.5, 0.5],
      ori: 0, pos: [0, 0],
      lineWidth: 1, lineColor: new util.Color([1.0, 0.004, (- 1.0)]),
      fillColor: new util.Color([1.0, 0.004, (- 1.0)]),
      opacity: 1.0, depth: -1, interpolate: true,
    });
    
    trialFix = new visual.TextStim({
      win: psychoJS.window,
      name: 'trialFix',
      text: '+',
      font: 'Arial',
      units: undefined, 
      pos: [0, 0], height: 0.05,  wrapWidth: undefined, ori: 0,
      color: new util.Color('white'),  opacity: 1,
      depth: -2.0 
    });
    
    trialResp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
    
    // Initialize components for Routine "end"
    endClock = new util.Clock();
    thanks = new visual.TextStim({
      win: psychoJS.window,
      name: 'thanks',
      text: 'This is the end of the experiment. Thank you for your time. Please remember to return to the questionnaire to carry on with the study. Press ‘space’ to exit.',
      font: 'Arial',
      units: undefined, 
      pos: [0, 0], height: 0.05,  wrapWidth: undefined, ori: 0,
      color: new util.Color('white'),  opacity: 1,
      depth: 0.0 
    });
    
    endResp = new core.Keyboard({psychoJS: psychoJS, clock: new util.Clock(), waitForStart: true});
    
    // Create some handy timers
    globalClock = new util.Clock();  // to track the time since experiment started
    routineTimer = new util.CountdownTimer();  // to track time remaining of each (non-slip) routine
    
    return Scheduler.Event.NEXT;
  }
  
  
  var t;
  var frameN;
  var _instr1Resp_allKeys;
  var instructionsComponents;
  function instructionsRoutineBegin(trials) {
    return function () {
      //------Prepare to start Routine 'instructions'-------
      t = 0;
      instructionsClock.reset(); // clock
      frameN = -1;
      // update component parameters for each repeat
      instr1Resp.keys = undefined;
      instr1Resp.rt = undefined;
      _instr1Resp_allKeys = [];
      // keep track of which components have finished
      instructionsComponents = [];
      instructionsComponents.push(instr1);
      instructionsComponents.push(instr1Resp);
      
      instructionsComponents.forEach( function(thisComponent) {
        if ('status' in thisComponent)
          thisComponent.status = PsychoJS.Status.NOT_STARTED;
         });
      
      return Scheduler.Event.NEXT;
    };
  }
  
  
  var continueRoutine;
  function instructionsRoutineEachFrame(trials) {
    return function () {
      //------Loop for each frame of Routine 'instructions'-------
      let continueRoutine = true; // until we're told otherwise
      // get current time
      t = instructionsClock.getTime();
      frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
      // update/draw components on each frame
      
      // *instr1* updates
      if (t >= 0.0 && instr1.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        instr1.tStart = t;  // (not accounting for frame time here)
        instr1.frameNStart = frameN;  // exact frame index
        
        instr1.setAutoDraw(true);
      }
  
      
      // *instr1Resp* updates
      if (t >= 0.0 && instr1Resp.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        instr1Resp.tStart = t;  // (not accounting for frame time here)
        instr1Resp.frameNStart = frameN;  // exact frame index
        
        // keyboard checking is just starting
        psychoJS.window.callOnFlip(function() { instr1Resp.clock.reset(); });  // t=0 on next screen flip
        psychoJS.window.callOnFlip(function() { instr1Resp.start(); }); // start on screen flip
        psychoJS.window.callOnFlip(function() { instr1Resp.clearEvents(); });
      }
  
      if (instr1Resp.status === PsychoJS.Status.STARTED) {
        let theseKeys = instr1Resp.getKeys({keyList: ['s'], waitRelease: false});
        _instr1Resp_allKeys = _instr1Resp_allKeys.concat(theseKeys);
        if (_instr1Resp_allKeys.length > 0) {
          instr1Resp.keys = _instr1Resp_allKeys[_instr1Resp_allKeys.length - 1].name;  // just the last key pressed
          instr1Resp.rt = _instr1Resp_allKeys[_instr1Resp_allKeys.length - 1].rt;
          // a response ends the routine
          continueRoutine = false;
        }
      }
      
      // check for quit (typically the Esc key)
      if (psychoJS.experiment.experimentEnded || psychoJS.eventManager.getKeys({keyList:['escape']}).length > 0) {
        return quitPsychoJS('The [Escape] key was pressed. Goodbye!', false);
      }
      
      // check if the Routine should terminate
      if (!continueRoutine) {  // a component has requested a forced-end of Routine
        return Scheduler.Event.NEXT;
      }
      
      continueRoutine = false;  // reverts to True if at least one component still running
      instructionsComponents.forEach( function(thisComponent) {
        if ('status' in thisComponent && thisComponent.status !== PsychoJS.Status.FINISHED) {
          continueRoutine = true;
        }
      });
      
      // refresh the screen if continuing
      if (continueRoutine) {
        return Scheduler.Event.FLIP_REPEAT;
      } else {
        return Scheduler.Event.NEXT;
      }
    };
  }
  
  
  function instructionsRoutineEnd(trials) {
    return function () {
      //------Ending Routine 'instructions'-------
      instructionsComponents.forEach( function(thisComponent) {
        if (typeof thisComponent.setAutoDraw === 'function') {
          thisComponent.setAutoDraw(false);
        }
      });
      // the Routine "instructions" was not non-slip safe, so reset the non-slip timer
      routineTimer.reset();
      
      return Scheduler.Event.NEXT;
    };
  }
  
  
  var trials1;
  var currentLoop;
  function trials1LoopBegin(thisScheduler) {
    // set up handler to look after randomisation of conditions etc
    trials1 = new TrialHandler({
      psychoJS: psychoJS,
      nReps: 1, method: TrialHandler.Method.SEQUENTIAL,
      extraInfo: expInfo, originPath: undefined,
      trialList: 'goNogoCond1.xlsx',
      seed: undefined, name: 'trials1'
    });
    psychoJS.experiment.addLoop(trials1); // add the loop to the experiment
    currentLoop = trials1;  // we're now the current loop
  
    // Schedule all the trials in the trialList:
    trials1.forEach(function() {
      const snapshot = trials1.getSnapshot();
  
      thisScheduler.add(importConditions(snapshot));
      thisScheduler.add(trialRoutineBegin(snapshot));
      thisScheduler.add(trialRoutineEachFrame(snapshot));
      thisScheduler.add(trialRoutineEnd(snapshot));
      thisScheduler.add(endLoopIteration(thisScheduler, snapshot));
    });
  
    return Scheduler.Event.NEXT;
  }
  
  
  function trials1LoopEnd() {
    psychoJS.experiment.removeLoop(trials1);
  
    return Scheduler.Event.NEXT;
  }
  
  
  var trials2;
  function trials2LoopBegin(thisScheduler) {
    // set up handler to look after randomisation of conditions etc
    trials2 = new TrialHandler({
      psychoJS: psychoJS,
      nReps: 1, method: TrialHandler.Method.SEQUENTIAL,
      extraInfo: expInfo, originPath: undefined,
      trialList: 'goNogoCond2.xlsx',
      seed: undefined, name: 'trials2'
    });
    psychoJS.experiment.addLoop(trials2); // add the loop to the experiment
    currentLoop = trials2;  // we're now the current loop
  
    // Schedule all the trials in the trialList:
    trials2.forEach(function() {
      const snapshot = trials2.getSnapshot();
  
      thisScheduler.add(importConditions(snapshot));
      thisScheduler.add(trialRoutineBegin(snapshot));
      thisScheduler.add(trialRoutineEachFrame(snapshot));
      thisScheduler.add(trialRoutineEnd(snapshot));
      thisScheduler.add(endLoopIteration(thisScheduler, snapshot));
    });
  
    return Scheduler.Event.NEXT;
  }
  
  
  function trials2LoopEnd() {
    psychoJS.experiment.removeLoop(trials2);
  
    return Scheduler.Event.NEXT;
  }
  
  
  var trials3;
  function trials3LoopBegin(thisScheduler) {
    // set up handler to look after randomisation of conditions etc
    trials3 = new TrialHandler({
      psychoJS: psychoJS,
      nReps: 1, method: TrialHandler.Method.SEQUENTIAL,
      extraInfo: expInfo, originPath: undefined,
      trialList: 'goNogoCond3.xlsx',
      seed: undefined, name: 'trials3'
    });
    psychoJS.experiment.addLoop(trials3); // add the loop to the experiment
    currentLoop = trials3;  // we're now the current loop
  
    // Schedule all the trials in the trialList:
    trials3.forEach(function() {
      const snapshot = trials3.getSnapshot();
  
      thisScheduler.add(importConditions(snapshot));
      thisScheduler.add(trialRoutineBegin(snapshot));
      thisScheduler.add(trialRoutineEachFrame(snapshot));
      thisScheduler.add(trialRoutineEnd(snapshot));
      thisScheduler.add(endLoopIteration(thisScheduler, snapshot));
    });
  
    return Scheduler.Event.NEXT;
  }
  
  
  function trials3LoopEnd() {
    psychoJS.experiment.removeLoop(trials3);
  
    return Scheduler.Event.NEXT;
  }
  
  
  var _trialResp_allKeys;
  var trialComponents;
  function trialRoutineBegin(trials) {
    return function () {
      //------Prepare to start Routine 'trial'-------
      t = 0;
      trialClock.reset(); // clock
      frameN = -1;
      routineTimer.add(2.000000);
      // update component parameters for each repeat
      trialBlue.setOpacity(blueOpacity);
      trialOrange.setOpacity(orangeOpacity);
      trialResp.keys = undefined;
      trialResp.rt = undefined;
      _trialResp_allKeys = [];
      // keep track of which components have finished
      trialComponents = [];
      trialComponents.push(trialBlue);
      trialComponents.push(trialOrange);
      trialComponents.push(trialFix);
      trialComponents.push(trialResp);
      
      trialComponents.forEach( function(thisComponent) {
        if ('status' in thisComponent)
          thisComponent.status = PsychoJS.Status.NOT_STARTED;
         });
      
      return Scheduler.Event.NEXT;
    };
  }
  
  
  var frameRemains;
  function trialRoutineEachFrame(trials) {
    return function () {
      //------Loop for each frame of Routine 'trial'-------
      let continueRoutine = true; // until we're told otherwise
      // get current time
      t = trialClock.getTime();
      frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
      // update/draw components on each frame
      
      // *trialBlue* updates
      if (t >= 1.0 && trialBlue.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        trialBlue.tStart = t;  // (not accounting for frame time here)
        trialBlue.frameNStart = frameN;  // exact frame index
        
        trialBlue.setAutoDraw(true);
      }
  
      frameRemains = 1.0 + 1.0 - psychoJS.window.monitorFramePeriod * 0.75;  // most of one frame period left
      if (trialBlue.status === PsychoJS.Status.STARTED && t >= frameRemains) {
        trialBlue.setAutoDraw(false);
      }
      
      // *trialOrange* updates
      if (t >= 1.0 && trialOrange.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        trialOrange.tStart = t;  // (not accounting for frame time here)
        trialOrange.frameNStart = frameN;  // exact frame index
        
        trialOrange.setAutoDraw(true);
      }
  
      frameRemains = 1.0 + 1.0 - psychoJS.window.monitorFramePeriod * 0.75;  // most of one frame period left
      if (trialOrange.status === PsychoJS.Status.STARTED && t >= frameRemains) {
        trialOrange.setAutoDraw(false);
      }
      
      // *trialFix* updates
      if (t >= 0.0 && trialFix.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        trialFix.tStart = t;  // (not accounting for frame time here)
        trialFix.frameNStart = frameN;  // exact frame index
        
        trialFix.setAutoDraw(true);
      }
  
      frameRemains = 0.0 + 1.0 - psychoJS.window.monitorFramePeriod * 0.75;  // most of one frame period left
      if (trialFix.status === PsychoJS.Status.STARTED && t >= frameRemains) {
        trialFix.setAutoDraw(false);
      }
      
      // *trialResp* updates
      if (t >= 1.0 && trialResp.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        trialResp.tStart = t;  // (not accounting for frame time here)
        trialResp.frameNStart = frameN;  // exact frame index
        
        // keyboard checking is just starting
        psychoJS.window.callOnFlip(function() { trialResp.clock.reset(); });  // t=0 on next screen flip
        psychoJS.window.callOnFlip(function() { trialResp.start(); }); // start on screen flip
        psychoJS.window.callOnFlip(function() { trialResp.clearEvents(); });
      }
  
      frameRemains = 1.0 + 1.0 - psychoJS.window.monitorFramePeriod * 0.75;  // most of one frame period left
      if (trialResp.status === PsychoJS.Status.STARTED && t >= frameRemains) {
        trialResp.status = PsychoJS.Status.FINISHED;
    }
  
      if (trialResp.status === PsychoJS.Status.STARTED) {
        let theseKeys = trialResp.getKeys({keyList: ['space'], waitRelease: false});
        _trialResp_allKeys = _trialResp_allKeys.concat(theseKeys);
        if (_trialResp_allKeys.length > 0) {
          trialResp.keys = _trialResp_allKeys[_trialResp_allKeys.length - 1].name;  // just the last key pressed
          trialResp.rt = _trialResp_allKeys[_trialResp_allKeys.length - 1].rt;
          // was this correct?
          if (trialResp.keys == corrAns) {
              trialResp.corr = 1;
          } else {
              trialResp.corr = 0;
          }
          // a response ends the routine
          continueRoutine = false;
        }
      }
      
      // check for quit (typically the Esc key)
      if (psychoJS.experiment.experimentEnded || psychoJS.eventManager.getKeys({keyList:['escape']}).length > 0) {
        return quitPsychoJS('The [Escape] key was pressed. Goodbye!', false);
      }
      
      // check if the Routine should terminate
      if (!continueRoutine) {  // a component has requested a forced-end of Routine
        return Scheduler.Event.NEXT;
      }
      
      continueRoutine = false;  // reverts to True if at least one component still running
      trialComponents.forEach( function(thisComponent) {
        if ('status' in thisComponent && thisComponent.status !== PsychoJS.Status.FINISHED) {
          continueRoutine = true;
        }
      });
      
      // refresh the screen if continuing
      if (continueRoutine && routineTimer.getTime() > 0) {
        return Scheduler.Event.FLIP_REPEAT;
      } else {
        return Scheduler.Event.NEXT;
      }
    };
  }
  
  
  function trialRoutineEnd(trials) {
    return function () {
      //------Ending Routine 'trial'-------
      trialComponents.forEach( function(thisComponent) {
        if (typeof thisComponent.setAutoDraw === 'function') {
          thisComponent.setAutoDraw(false);
        }
      });
      // was no response the correct answer?!
      if (trialResp.keys === undefined) {
        if (['None','none',undefined].includes(corrAns)) {
           trialResp.corr = 1;  // correct non-response
        } else {
           trialResp.corr = 0;  // failed to respond (incorrectly)
        }
      }
      // store data for thisExp (ExperimentHandler)
      psychoJS.experiment.addData('trialResp.keys', trialResp.keys);
      psychoJS.experiment.addData('trialResp.corr', trialResp.corr);
      if (typeof trialResp.keys !== 'undefined') {  // we had a response
          psychoJS.experiment.addData('trialResp.rt', trialResp.rt);
          routineTimer.reset();
          }
      
      trialResp.stop();
      return Scheduler.Event.NEXT;
    };
  }
  
  
  var _instr2Resp_allKeys;
  var instructions2Components;
  function instructions2RoutineBegin(trials) {
    return function () {
      //------Prepare to start Routine 'instructions2'-------
      t = 0;
      instructions2Clock.reset(); // clock
      frameN = -1;
      // update component parameters for each repeat
      instr2Resp.keys = undefined;
      instr2Resp.rt = undefined;
      _instr2Resp_allKeys = [];
      // keep track of which components have finished
      instructions2Components = [];
      instructions2Components.push(instr2);
      instructions2Components.push(instr2Resp);
      
      instructions2Components.forEach( function(thisComponent) {
        if ('status' in thisComponent)
          thisComponent.status = PsychoJS.Status.NOT_STARTED;
         });
      
      return Scheduler.Event.NEXT;
    };
  }
  
  
  function instructions2RoutineEachFrame(trials) {
    return function () {
      //------Loop for each frame of Routine 'instructions2'-------
      let continueRoutine = true; // until we're told otherwise
      // get current time
      t = instructions2Clock.getTime();
      frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
      // update/draw components on each frame
      
      // *instr2* updates
      if (t >= 0.0 && instr2.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        instr2.tStart = t;  // (not accounting for frame time here)
        instr2.frameNStart = frameN;  // exact frame index
        
        instr2.setAutoDraw(true);
      }
  
      
      // *instr2Resp* updates
      if (t >= 0.0 && instr2Resp.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        instr2Resp.tStart = t;  // (not accounting for frame time here)
        instr2Resp.frameNStart = frameN;  // exact frame index
        
        // keyboard checking is just starting
        psychoJS.window.callOnFlip(function() { instr2Resp.clock.reset(); });  // t=0 on next screen flip
        psychoJS.window.callOnFlip(function() { instr2Resp.start(); }); // start on screen flip
        psychoJS.window.callOnFlip(function() { instr2Resp.clearEvents(); });
      }
  
      if (instr2Resp.status === PsychoJS.Status.STARTED) {
        let theseKeys = instr2Resp.getKeys({keyList: ['s'], waitRelease: false});
        _instr2Resp_allKeys = _instr2Resp_allKeys.concat(theseKeys);
        if (_instr2Resp_allKeys.length > 0) {
          instr2Resp.keys = _instr2Resp_allKeys[_instr2Resp_allKeys.length - 1].name;  // just the last key pressed
          instr2Resp.rt = _instr2Resp_allKeys[_instr2Resp_allKeys.length - 1].rt;
          // a response ends the routine
          continueRoutine = false;
        }
      }
      
      // check for quit (typically the Esc key)
      if (psychoJS.experiment.experimentEnded || psychoJS.eventManager.getKeys({keyList:['escape']}).length > 0) {
        return quitPsychoJS('The [Escape] key was pressed. Goodbye!', false);
      }
      
      // check if the Routine should terminate
      if (!continueRoutine) {  // a component has requested a forced-end of Routine
        return Scheduler.Event.NEXT;
      }
      
      continueRoutine = false;  // reverts to True if at least one component still running
      instructions2Components.forEach( function(thisComponent) {
        if ('status' in thisComponent && thisComponent.status !== PsychoJS.Status.FINISHED) {
          continueRoutine = true;
        }
      });
      
      // refresh the screen if continuing
      if (continueRoutine) {
        return Scheduler.Event.FLIP_REPEAT;
      } else {
        return Scheduler.Event.NEXT;
      }
    };
  }
  
  
  function instructions2RoutineEnd(trials) {
    return function () {
      //------Ending Routine 'instructions2'-------
      instructions2Components.forEach( function(thisComponent) {
        if (typeof thisComponent.setAutoDraw === 'function') {
          thisComponent.setAutoDraw(false);
        }
      });
      // the Routine "instructions2" was not non-slip safe, so reset the non-slip timer
      routineTimer.reset();
      
      return Scheduler.Event.NEXT;
    };
  }
  
  
  var _endResp_allKeys;
  var endComponents;
  function endRoutineBegin(trials) {
    return function () {
      //------Prepare to start Routine 'end'-------
      t = 0;
      endClock.reset(); // clock
      frameN = -1;
      // update component parameters for each repeat
      endResp.keys = undefined;
      endResp.rt = undefined;
      _endResp_allKeys = [];
      // keep track of which components have finished
      endComponents = [];
      endComponents.push(thanks);
      endComponents.push(endResp);
      
      endComponents.forEach( function(thisComponent) {
        if ('status' in thisComponent)
          thisComponent.status = PsychoJS.Status.NOT_STARTED;
         });
      
      return Scheduler.Event.NEXT;
    };
  }
  
  
  function endRoutineEachFrame(trials) {
    return function () {
      //------Loop for each frame of Routine 'end'-------
      let continueRoutine = true; // until we're told otherwise
      // get current time
      t = endClock.getTime();
      frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
      // update/draw components on each frame
      
      // *thanks* updates
      if (t >= 0.0 && thanks.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        thanks.tStart = t;  // (not accounting for frame time here)
        thanks.frameNStart = frameN;  // exact frame index
        
        thanks.setAutoDraw(true);
      }
  
      
      // *endResp* updates
      if (t >= 0.0 && endResp.status === PsychoJS.Status.NOT_STARTED) {
        // keep track of start time/frame for later
        endResp.tStart = t;  // (not accounting for frame time here)
        endResp.frameNStart = frameN;  // exact frame index
        
        // keyboard checking is just starting
        psychoJS.window.callOnFlip(function() { endResp.clock.reset(); });  // t=0 on next screen flip
        psychoJS.window.callOnFlip(function() { endResp.start(); }); // start on screen flip
        psychoJS.window.callOnFlip(function() { endResp.clearEvents(); });
      }
  
      if (endResp.status === PsychoJS.Status.STARTED) {
        let theseKeys = endResp.getKeys({keyList: ['space'], waitRelease: false});
        _endResp_allKeys = _endResp_allKeys.concat(theseKeys);
        if (_endResp_allKeys.length > 0) {
          endResp.keys = _endResp_allKeys[_endResp_allKeys.length - 1].name;  // just the last key pressed
          endResp.rt = _endResp_allKeys[_endResp_allKeys.length - 1].rt;
          // a response ends the routine
          continueRoutine = false;
        }
      }
      
      // check for quit (typically the Esc key)
      if (psychoJS.experiment.experimentEnded || psychoJS.eventManager.getKeys({keyList:['escape']}).length > 0) {
        return quitPsychoJS('The [Escape] key was pressed. Goodbye!', false);
      }
      
      // check if the Routine should terminate
      if (!continueRoutine) {  // a component has requested a forced-end of Routine
        return Scheduler.Event.NEXT;
      }
      
      continueRoutine = false;  // reverts to True if at least one component still running
      endComponents.forEach( function(thisComponent) {
        if ('status' in thisComponent && thisComponent.status !== PsychoJS.Status.FINISHED) {
          continueRoutine = true;
        }
      });
      
      // refresh the screen if continuing
      if (continueRoutine) {
        return Scheduler.Event.FLIP_REPEAT;
      } else {
        return Scheduler.Event.NEXT;
      }
    };
  }
  
  
  function endRoutineEnd(trials) {
    return function () {
      //------Ending Routine 'end'-------
      endComponents.forEach( function(thisComponent) {
        if (typeof thisComponent.setAutoDraw === 'function') {
          thisComponent.setAutoDraw(false);
        }
      });
      // the Routine "end" was not non-slip safe, so reset the non-slip timer
      routineTimer.reset();
      
      return Scheduler.Event.NEXT;
    };
  }
  
  
  function endLoopIteration(thisScheduler, loop) {
    // ------Prepare for next entry------
    return function () {
      if (typeof loop !== 'undefined') {
        // ------Check if user ended loop early------
        if (loop.finished) {
          // Check for and save orphaned data
          if (psychoJS.experiment.isEntryEmpty()) {
            psychoJS.experiment.nextEntry(loop);
          }
        thisScheduler.stop();
        } else {
          const thisTrial = loop.getCurrentTrial();
          if (typeof thisTrial === 'undefined' || !('isTrials' in thisTrial) || thisTrial.isTrials) {
            psychoJS.experiment.nextEntry(loop);
          }
        }
      return Scheduler.Event.NEXT;
      }
    };
  }
  
  
  function importConditions(trials) {
    return function () {
      psychoJS.importAttributes(trials.getCurrentTrial());
      return Scheduler.Event.NEXT;
      };
  }
  
  
  function quitPsychoJS(message, isCompleted) {
    // Check for and save orphaned data
    if (psychoJS.experiment.isEntryEmpty()) {
      psychoJS.experiment.nextEntry();
    }
    psychoJS.window.close();
    psychoJS.quit({message: message, isCompleted: isCompleted});
    
    return Scheduler.Event.QUIT;
  }
  