add_library('serial') grid = [ [-1]*10 for n in range(10)] # list comprehension grid[0][0] = 1 grid[9][9] = 1 w = 70 # width of each cell nsamples = 100.0 # number of samples accumulated WINDOW = 1000 def setup(): #setup the serial port print Serial.list() portIndex = 4 LF = 10 #myPort = Serial(Serial.list()[portIndex], 9600) # this seems to need a first argument referring to the sketch object (analagous to "this" argument in Java) portName = Serial.list()[len(Serial.list()) - 1] # Pick last serial port in list print "Connecting to ", portName global myPort myPort = Serial(this, portName, 9600) myPort.bufferUntil(LF) size(800,600) def draw(): byte2 = 0 byte3 = 0 byte4 = 0 byte5 = 0 byte6 = 0 byte7 = 0 byte8 = 0 byte9 = 0 byte10 = 0 while (myPort.available() > 0): x,y = 0,0 # starting position for row in grid: for col in row: if (myPort.available() > 0): byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = byte5 byte5 = byte6 byte6 = byte7 byte7 = byte8 byte8 = byte9 byte9 = byte10 byte10 = myPort.read() low = myPort.read() med = myPort.read() high = myPort.read() value = abs((256*256*high + 256*med + low)/nsamples) xx = int(.2*WINDOW + (.9-.2)*WINDOW*value/1024.0) value = (255*255*high + 255*med + low)*255 value = (byte1+byte2+byte3+byte4+byte5+byte6+byte7+byte8+byte9+byte10) println(xx) if col == 1: fill(255,255,255) else: fill(value,x,x) rect(x, y, w, w) x = x + w # move right y = y + w # move down x = 0 # rest to left edge def mousePressed(): grid[mouseY/w][mouseX/w] = -1 * grid[mouseY/w][mouseX/w] # integer division is good here! def serialEvent(evt): inString = evt.readString() print inString