float xmag, ymag = 0; float newXmag, newYmag = 0; import processing.serial.*; // import the serial lib int [] vals = new int[1]; // raw values from the sensor Serial myPort; // the serial port boolean madeContact = false; // whether there's been serial data in void setup() { size(640, 400, P3D); //smooth(); //noStroke(); // create a font with the second font available to the system: PFont myFont = createFont(PFont.list()[2], 14); textFont(myFont); // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[0], 9600); // only generate a serial event when you get a return char: myPort.bufferUntil('\r'); strokeWeight(2); colorMode(RGB, 1); } void draw() { background(0.5); // if you've never gotten a string from the microcontroller, // keep sending carriage returns to prompt for one: if (madeContact == false) { myPort.write('m'); } // translate(width/2, height/2); float a = 2*PI*vals[0]/3600; pushMatrix(); translate(width/2, height/2, -30); rotateX(-0.4); rotateY(-a); scale(90); beginShape(QUADS); fill(0, 1, 1); vertex(-1, 1, 1); fill(1, 1, 1); vertex( 1, 1, 1); fill(1, 0, 1); vertex( 1, -1, 1); fill(0, 0, 1); vertex(-1, -1, 1); fill(1, 1, 1); vertex( 1, 1, 1); fill(1, 1, 0); vertex( 1, 1, -1); fill(1, 0, 0); vertex( 1, -1, -1); fill(1, 0, 1); vertex( 1, -1, 1); fill(1, 1, 0); vertex( 1, 1, -1); fill(0, 1, 0); vertex(-1, 1, -1); fill(0, 0, 0); vertex(-1, -1, -1); fill(1, 0, 0); vertex( 1, -1, -1); fill(0, 1, 0); vertex(-1, 1, -1); fill(0, 1, 1); vertex(-1, 1, 1); fill(0, 0, 1); vertex(-1, -1, 1); fill(0, 0, 0); vertex(-1, -1, -1); fill(0, 1, 0); vertex(-1, 1, -1); fill(1, 1, 0); vertex( 1, 1, -1); fill(1, 1, 1); vertex( 1, 1, 1); fill(0, 1, 1); vertex(-1, 1, 1); fill(0, 0, 0); vertex(-1, -1, -1); fill(1, 0, 0); vertex( 1, -1, -1); fill(1, 0, 1); vertex( 1, -1, 1); fill(0, 0, 1); vertex(-1, -1, 1); endShape(); popMatrix(); } void serialEvent(Serial myPort) { // if serialEvent occurs at all, contact with the microcontroller // has been made: madeContact = true; // read the serial buffer: String myString = myPort.readStringUntil('\n'); // if you got any bytes other than the linefeed: if (myString != null) { myString = trim(myString); // split the string at the commas //and convert the sections into integers: int sensors[] = int(split(myString, ',')); // if you received all the sensor strings, use them: if (sensors.length >= 1) { vals[0] = sensors[0]; delay(100); println(sensors[0]); // send out the serial port to ask for data: myPort.write('m'); } } }