import processing.serial.*; Serial myPort; String portnum; String outString = ""; String inString = ""; int receivedLines = 0; int bufferedLines = 10; int myWidth = 600, myHeight = 600; int xPos = 0, yPos = 0, zPos = 0, prevX = 0, prevY = 0, prevZ = 0, dx, dy, dz, masterDelta; byte strokeColor = 0; void setup() { frameRate(50); size(myWidth, myHeight); smooth(); background(0); PFont myFont = createFont(PFont.list()[2], 14); textFont(myFont); println(Serial.list()); portnum = Serial.list()[0]; myPort = new Serial(this, portnum, 9600); } void draw() { // background(0); dx = xPos - prevX; dy = yPos - prevY; dz = zPos - prevZ; masterDelta = max(abs(dx), abs(dy), abs(dz)); copy(0, 1, myWidth, myHeight-1, 0, 0, myWidth, myHeight-1); // text("xPos: " + ((xPos + 4096) / 32), 10, 20); // line(10, 10, 200, 200 - (xPos / 16)); stroke(0); line(0, myHeight-1, myWidth-1, myHeight-1); stroke((xPos + 4096) / 32, (yPos + 4096) / 32, (zPos + 4096) / 32); line(280 - masterDelta / 8, 599, 320 + masterDelta / 8, 599); /* text("Serial port: " + portnum, 10, 20); text("typed: " + outString, 10, 40); text("received:\n" + inString, 10, 80); */ /* prevX = xPos; prevY = yPos; prevZ = zPos; */ } /* void keyPressed() { switch (key) { case '\n': myPort.write(outString + "\r"); outString = ""; break; case 8: // backspace outString = outString.substring(0, outString.length() - 1); break; case '+': myPort.write(key); outString += key; break; case 65535: break; default: outString += key; break; } } */ void serialEvent(Serial myPort) { int inByte = myPort.read(); inString += char(inByte); if (inByte == '\r') { int nums[] = int(split(inString, ' ')); prevX = xPos; prevY = yPos; prevZ = zPos; xPos = nums[0]; yPos = nums[1]; zPos = nums[2]; inString = ""; /* inString += '\n'; receivedLines++; if (receivedLines > bufferedLines) { deleteFirstLine(); } */ } } /* void deleteFirstLine() { int firstChar = inString.indexOf('\n'); inString = inString.substring(firstChar+1); } */