add_library('serial') port = "COM5" def setup(): size(512, 512) #setup the serial port print Serial.list() LF = 10 print "Connecting to ", port myPort = Serial(this, port, 115200) myPort.bufferUntil(LF) x, y = -1, -1 # current values of X and Y (-1 => not yet set) px, py = -1, -1 # previous values of X and Y (-1 => not yet set) def draw(): global x, y # X and Y are not local variables if x >= 0 and y >= 0: # X and Y have been set by serialEvent() global px, py # PX and PY are also not local variables if px >= 0 and py >= 0: # PX and PY have been set from valid X and Y line(px, py, x, y) # draw a line from previous X,Y to current X,Y px, py = x, y # copy valid X,Y to previous X,Y (even if no line was drawn) def serialEvent(evt): xy = evt.readString().strip().split() if len(xy) == 2: global x, y x, y = int(xy[0])/2, int(xy[1])/2