The text draws over itself without clearing so it quickly just becomes a blob in the top left corner. I tried drawing a rectangle over the area but the flicker was awful. Why? How can I update text without flicker?
Code://PROCESSING
// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, works on tab deliminated values followed by a new line by buffering up to newline in setup
// then reading each element in to a variable by incrementing through the tab characters in the string variable
// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// This example code is in the public domain.
// Modified for 2D memsic accelerometer by C Rand April 2010.
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
PFont font; //variable to hold font
void setup () {
// set the window size:
size(640,480);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 57600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
//load my font
font = loadFont("Consolas-24.vlw");
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the first ASCII string:
String inStringX = myPort.readStringUntil('\t');
if (inStringX != null) {
// trim off any whitespace:
inStringX = trim(inStringX);
// convert to an int and map to upper half of the screen height:
float inByteX = float(inStringX);
println(inStringX);
inByteX = map(inByteX, 0, 1023, height/2 , height);
// draw the X line by varying line height:
stroke(50,55,200);
line(xPos, height/2, xPos, height - inByteX);
// get the second ASCII string:
String inStringY = myPort.readStringUntil('\n');
// convert to an int and map to lower half the screen height:
float inByteY = float(inStringY);
inByteY = map(inByteY, 0,1023, 0, height/2);
// draw the Y line by varying line height:
stroke(50,100,255);
line(xPos, height, xPos, height - inByteY);
//Draw readings as text
//Trying to draw readings at text here
//Makes program readings lag behind the accelerometer motion by 5 seconds
//great response time if you comment out the following text chunk
textFont(font);
fill(0, 102, 153);
text(inStringX, 15, 30);
fill(0, 102, 153);
text(inStringY, 15, 60);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}