Reputation: 417
I'm trying to plot some sensor data that's being read in through the serial port. I found this Processing example code of a 2D plotter, and I'm trying to alter it to work for my application.
The programming is a bit over my head though. I can see that it's getting data for the three lines from the three equation classes. I'd like to replace the part where is reads the mouseX with a variable or something that points to the newest sensor update. The sensor data is continuously updated in the void Draw()
loop.
/**
* RollingGraph
* This sketch makes ise of the RollingLine2DTrace object to
* draw a dynamically updated plot.
*/
import org.gwoptics.graphics.graph2D.Graph2D;
import org.gwoptics.graphics.graph2D.traces.ILine2DEquation;
import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace;
class eq implements ILine2DEquation{
public double computePoint(double x,int pos) {
return mouseX;
}
}
class eq2 implements ILine2DEquation{
public double computePoint(double x,int pos) {
return mouseY;
}
}
class eq3 implements ILine2DEquation{
public double computePoint(double x,int pos) {
if(mousePressed)
return 400;
else
return 0;
}
}
RollingLine2DTrace r,r2,r3;
Graph2D g;
void setup(){
size(600,300);
r = new RollingLine2DTrace(new eq() ,100,0.1f);
r.setTraceColour(0, 255, 0);
r2 = new RollingLine2DTrace(new eq2(),100,0.1f);
r2.setTraceColour(255, 0, 0);
r3 = new RollingLine2DTrace(new eq3(),100,0.1f);
r3.setTraceColour(0, 0, 255);
g = new Graph2D(this, 400, 200, false);
g.setYAxisMax(600);
g.addTrace(r);
g.addTrace(r2);
g.addTrace(r3);
g.position.y = 50;
g.position.x = 100;
g.setYAxisTickSpacing(100);
g.setXAxisMax(5f);
}
void draw(){
background(200);
g.draw();
}
Upvotes: 1
Views: 17880
Reputation: 917
Not exactly sure if that code you have is what you need, but here is a simple program that takes in serial input and makes a line graph. Hope this helps.
import processing.serial.*;
Serial myPort;
int x = 0;
void setup() {
size(600, 400);
println(Serial.list()); //list of available serial ports
String portName = Serial.list()[0]; //replace 0 with whatever port you want to use.
myPort = new Serial(this, portName, 9600);
}
void draw() {
}
void serialEvent(Serial myPort) {
int inByte = myPort.read();
println(inByte);
stroke(90, 76, 99);
//vertical line with height varying according to input
line(x, height, x, height - inByte);
if (x >=width) {
x=0;
background(0);
}
x++;
}
Upvotes: 2