PsychoPy-Arduino Exercise #1 Calibrating the Arduino light sensor via a Builder project Intro Routine ------------- Create an introductory routine to display instructions and prompt for a keyboard response before continuing. Add the following custom code to the routine: Custom_code|Begin Experiment tab #Initialize serial connection to Arduino import serial com_port = "COM18" #Windows USB serial port address #com_port = "/dev/cu.usbmodem14101" #Mac USB serial port address try: arduino=serial.Serial(com_port,baudrate=115200,timeout=0.015) except: print("Unable to open serial port:", com_port) Black Calibration Routine ------------------------- Next, create a routine to fill the screen with a black rectangle. Set the duration of the rectangle to infinity. It will be terminated via custom code (see below) when the Arduino microcontroller finishes the requested Black Level calibration operation. Custom_code|Each Frame tab: #request Arduino BLACK level baseline scan at Routine start if frameN == 2: arduino.write(bytes('b','utf-8')) #service Arduino acknowledgment that scan has started if frameN == 6: if arduino.in_waiting > 0: serial_input = arduino.readline() #service serial input buffer print("BLACK_CAL_ACK:",serial_input) #exit Routine when BLACK level scan completed if frameN > 6: if arduino.in_waiting > 0: serial_input = arduino.readline() #service serial input buffer print("BLACK_CAL_RESULT:", serial_input) continueRoutine = False #exit Routine White Calibration Routine ------------------------- Next, create a routine to fill the screen with a white rectangle. Set the duration of the rectangle to infinity. It will be terminated via custom code (see below) when the Arduino microcontroller finishes the requested White Level calibration operation. Custom_code|Each Frame tab: #request Arduino WHITE level baseline scan at Routine start if frameN == 2: arduino.write(bytes('w','utf-8')) #service Arduino acknowledgment that scan has started if frameN == 6: if arduino.in_waiting > 0: serial_input = arduino.readline() #service serial input buffer print("WHITE_CAL_ACK:",serial_input) #exit Routine when WHITE level scan completed if frameN > 6: if arduino.in_waiting > 0: serial_input = arduino.readline() #service serial input buffer print("WHITE_CAL_RESULT:", serial_input) continueRoutine = False #exit Routine Custom_code|End Experiment tab: #terminate serial connection to Arduino arduino.close() Note: After running the PsychoPy Builder experiment, check the PsychoPy Runner console for the feedback messages generated by the 'print' statements in the custom code.