PsychoPy-Arduino Exercise #2 Using the calibrated Arduino light sensor to measure the duration of a screen stimulus (i.e., brief WHITE screen stimulus). In Arduino Exercise #1, we build a PsychoPy project to calibrate the Black and White levels of the Arduino light sensor. In Exercise #2, we will extend this project with code to allow us to measure the duration of a video screen stimulus. ------------- Intro Routine ------------- Cusom code|Begin Experiment Tab ------------------------------- Add the following global variable declaration: #screen duration global usdDuration = 0.0 NEXT, CREATE/ADD 4 NEW ROUTINES TO YOUR PSYCHOPY PROJECT. NAME THEM: 'BlackScreen', 'WhiteScreen' and 'BlackScreen2' ------------------- BlackScreen Routine ------------------- Add a Polygon/Rectangle component the fills the entire screen. Set its fill color to Black and its time duration to infinity. Add the following custom code component to send a command to the Arduino microconroller. Custom code component|Each Frame Tab ------------------------------------ #send Duration Measurement request to Adruino if frameN == 5: arduino.write(bytes('d','utf-8')) #exit routine after 15 frames if frameN == 15: continueRoutine = False ------------------- WhiteScreen Routine ------------------- This routine generates the stimulus whose duration will be measured. Create a Polygon/Rectangle component that fills the entire screeen. The rectangle should fill the entire screen and its fill color = White. Set its Start Time to Frame=0 and its duration to 10 frames. No custom code used in this routine. ------------------- BlackSreen2 Routine ------------------- Create a Polygon/Rectangle that fills the screen. Set the fill color to Black and the duration to 2 seconds. Next, create a custom code component and add the following code snippets to the appropriate code tabs: Custom code component|Each Frame Tab ------------------------------------ #collect Arduino Duration data (if available) #then exit BlackScreen2 routine if arduino.in_waiting > 0: serial_input = arduino.readline() if len(serial_input) > 0: serial_str = serial_input.strip().decode() serial_data = int(serial_str) usdDuration = serial_data print("DURATION (micros):", serial_str) continueRoutine = False #exit BlackScreen2 routine #exit routine if no Arduino response within 60 frames if frameN > 60: arduino.write(bytes('s','utf-8')) #abort Duration measurement usdDuration = 0.0 print("WHITE SCREEN timed-out") continueRoutine = False #exit BlackScreen2 routine Custom code component|End Routine Tab ------------------------------------- #log the Duration Measurement thisExp.addData('Duration', usdDuration) Custom code component|End Experiment ------------------------------------ #Close serial port connection to Arduino arduino.close() After successfully testing the project outlined above, check the PsychoPy console and/or the PsychoPy output file for the results of the duration measurement.