Trying to use code component in Builder to trigger arduino vibration motor for 3 AFC experiment

Hi,
This is my first time using psychopy to put together an experiment. I’m on a Windows 10 machine with Psychopy3 standard standalone. I’m trying to have psychopy trigger a vibration motor via an arduino uno using the serial port. The experiment involves psychopy telling the arduino to turn on the vibrate motor for 2 durations consecutively with a 1 second break between and then have the participant tell via the keyboard if one is longer than the other or if they are the same (3 choices). I have got the arduino to talk to psychopy, my issue is now with the code component within the builder. I have set up my conditions file in excel with two duration columns and a correct answer column. Both the arduino script and this excel file are in the same folder as my experiment.

My question is this, should I have two code components and then a key response component to ensure correct presentation of the stimuli? If this is correct then how do I do it? I’m sorry if these are basic questions but i can’t seem to find an answer despite much searching.

My code is not working, I dont know why so its below.

Any help would be very much appreciated, I know there is a way to do this, I’m just not sure of what it is.

In the begin experiment tab I have


ser = serial('COM3', 9600)
serial.flush()

def get_stimulation_onset_time(): #we can use this to keep track of stimulation onset
    global stimulation_onset_time
stimulation_onset_time = core.getTime()
type or paste code here

In the begin routine tab I have 
```serial.flush()
serial.write('$time_1')
serial.sleep (1)

serial.flush
serial.write('$time_2')
serial.sleep(1)

I'm stuck and hope someone can help. 

Paula