import processing.serial.*; // import the serial lib int graphPosition = 0; // horizontal position of the graph int [] vals = new int[1]; // raw values from the sensor float position; // position to translate to Serial myPort; // the serial port boolean madeContact = false; // whether there's been serial data in void setup() { background(255); size(300, 300); // window size // calculate position: position = width/2; // 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'); // settings for drawing arrow: noStroke(); smooth(); } void draw() { background(204); text("N",145,110); text("|",150,120); text("S",146,200); text("|",150,187); text("E",192,155); text("_",180,149); text("W",95,155); text("_",113,149); // 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; // draw an arrow using the heading: drawArrow(a); } void drawArrow(float a) { // text("angle " + angle + " angle", 50,90); // move whatever you draw next so that (0,0) is centered on the screen: translate(width/2, height/2); // draw a circle in light blue: fill(80,200,230); ellipse(0,0,50,50); // make the arrow black: fill(0); // rotate using the heading: rotate(a); // draw the arrow. center of the arrow is at (0,0): triangle(-15, 0, 0, -20, 15, 0); rect(-5,0, 10,20); } 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'); } } }