PsychoPy-Arduino Exercise #3 Using the calibrated Arduino light sensor to measure the LATENCY of a screen stimulus onset (i.e., brief WHITE screen stimulus). In Arduino Exercise #1, we built a PsychoPy project to calibrate the Black and White levels of the Arduino light sensor. In Exercise #3, we will extend this project with code to allow us to measure the onset latency of a video screen stimulus. ------------- Intro Routine ------------- Custom code|Begin Experiment Tab ------------------------------- Add the following code to your tab that creates the 'arduino' serial communication object: #synchronous screen-flip trigger routine #example usage: win.callOnFlip(send_trigger,'l') import time global trigger_time def send_trigger(char): global trigger_time trigger_time = globalClock.getTime() arduino.write(bytes(char,'utf-8')) #screen latency global usdLatency = 0.0 NEXT, CREATE/ADD 2 NEW ROUTINES TO YOUR PSYCHOPY PROJECT. NAME THEM: 'BlackScreen' and 'WhiteScreen' ------------------- BlackScreen Routine ------------------- Add a Polygon/Rectangle component the fills the entire screen. Set its fill color to Black and its time duration to 30 frames. No custom code needed for this routine. ------------------- WhiteScreen Routine ------------------- This routine generates the stimulus whose onset latency will be measured. Create a Polygon/Rectangle component. The rectangle should fill the entire screen and its fill color = White. Set its Start Time to Frame=0 and its duration to 60 frames. Add the following custom code to the routine: Custom code|Each Frame Tab -------------------------- #send synchronous screen trigger to Arduino if frameN == 0: win.callOnFlip(send_trigger,'l') #request Latency mesurement ('l' = ell) #scan for Arduino LATENCY measurement 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) usdLatency = serial_data print("LATENCY:",serial_str) continueRoutine = False #end WhiteScreen routine #exit if no Arduino ressponse within 30 frames if frameN >= 30: usdLatency = 0.0 print("WHITE screen timed-out") continueRoutine = False Custom code|End Routine Tab --------------------------- #save trigger time thisExp.addData('trigger',trigger_time) thisExp.addData('latency',usdLatency) Custom code|End Experiment Tab ------------------------------ arduino.close() Run the PsychoPy script and check the console and Excel output file for the results of the latency measurement.