import processing.serial.*; PFont font; String buff = ""; int val = 0; int NEWLINE = 10; int xPos,yPos,zPos = 0; int displaySize = 2; int an1, an2, an3; int colorChange = 0; Serial port; void setup(){ font = loadFont("AlbaMatter-48.vlw"); background(80); size(900,600); smooth(); port = new Serial(this, "COM9", 19200); } void draw(){ // new background over old fill(0,2); noStroke(); rect(0,0,width,height); // wipe out a small area in front of the new data // check for serial, and process while (port.available() > 0) { serialEvent(port.read()); } } void serialEvent(int serial) { if(serial != '\n') { buff += char(serial); } else { int curX = buff.indexOf("X"); int curY = buff.indexOf("Y"); int curZ = buff.indexOf("Z"); if(curX >=0){ String val = buff.substring(curX+1); an1 = Integer.parseInt(val.trim()); xPos++; if(xPos > width) xPos = 0; fill(0); noStroke(); } if(curY >=0){ String val = buff.substring(curY+1); an2 = Integer.parseInt(val.trim()); yPos++; if(yPos > width) yPos = 0; } if(curZ >=0){ String val = buff.substring(curZ+1); an3 = Integer.parseInt(val.trim()); zPos++; if(zPos > width) zPos = 0; rect(xPos+20,0,250,height); sensorTic(xPos,an1-350,"x-axis "+an1,0,255,0,0,255,0); sensorTic(yPos,an2-175,"y-axis "+an2,255,0,0,255,0,0); sensorTic(zPos,an3,"z-axis"+an3,0,0,255,0,0,255); } // Clear the value of "buff" buff = ""; } } void sensorTic(int x, int y, String txt, int sr, int sg, int sb, int fr, int fg, int fb){ stroke(sr,sg,sb); fill(fr,fg,fb); ellipse(x,y,displaySize,displaySize); textFont(font); textSize(40); text(txt,x+25,y); }