Arduino Serial Port Lag
in
Integration and Hardware
•
2 years ago
Hi i'm working plotting some sensors data from my arduino, it works but the problem is that the serial comunication is so so slow, dont know exactly what the problem is.. here's my code:
thanks!
import processing.serial.*;
import controlP5.*;
Serial myPort; // Create object from Serial class
float val,x,y,x2,y2; // Data received from the serial port
String sdata;
String f[],d[];
//ArrayList arraydata= new ArrayList(); //create array instance
ArrayList arrayf= new ArrayList();
ArrayList arrayd= new ArrayList();
float plotX1, plotY1;
float plotX2, plotY2;
float labelX, labelY; //f,d
float minx,maxx,miny,maxy;
PFont plotFont;
PFont labelFont;
PrintWriter output;
ControlP5 controlP5;
// ####### Setup ###################
void setup()
{
size(800, 600);
myPort = new Serial(this, "/dev/ttyUSB0", 9600);
strokeWeight(2);
background(100);
//graph
plotX1 = 120;
plotX2 = width - 80;
labelX = 50;
plotY1 = 60;
plotY2 = height - 70;
labelY = height - 25;
plotFont = createFont("SansSerif", 20);
labelFont = createFont("SansSerif", 8);
textFont(plotFont);
//plot
rectMode(CORNERS);
fill(0,0,0);
rect(plotX1, plotY1, plotX2, plotY2);
//firts data are boundaries of the graph
arrayf.add(0,plotY2);
arrayd.add(0,plotX1);
//output file
// Create a new file in the sketch directory
output = createWriter("positions.txt");
//gui
controlP5= new ControlP5(this);
controlP5.addButton("Plot",1,60,20,40,30); //parameters : name, value (float), x, y, width, height
controlP5.addButton("Save Image",1,120,20,80,30);
}
// ########################
// ####### DRAW ###################
void draw()
{
if ( myPort.available() > 0) { // If data is available
sdata=myPort.readStringUntil(13);
if (sdata != null){
if (sdata.indexOf("f")>=0) //sensor 1
{
f=split(sdata,' ');
y=float(f[1]);
println(f[1]);
}
else if (sdata.indexOf("d")>=0)//sensor 2
{
d=split(sdata,' ');
x=float(d[1]);
println(d[1]);
}
}
}
stroke(204, 102, 0);
//myPort.clear();
if (checkdata(arrayf,arrayd,y,x)==false) //if the incoming data is not present in the array
{
UpdateArray(arrayf,y);
UpdateArray(arrayd,x);
}
//println(sdata);
//maxy=getmaxvalue(arrayf); //get data max and min values
//miny=getminvalue(arrayf);
//maxx=getmaxvalue(arrayd);
//minx=getminvalue(arrayd); //can put these in the plotfunction to reduce processing...
//draw2();
//Plotarray(arrayd,arrayf);
//labelx();
}
// #####################
// ####### Functions ###################
private void UpdateArray(ArrayList array1, float d1)
{
array1.add(d1);
}
private void Plotarray(ArrayList array1, ArrayList array2)
{
stroke(204, 102, 0);
float xplot,x2plot,yplot,y2plot ; //remapped values
for(int i=0;i<array1.size();i++)
{
if(i>1){
xplot=map((Float)array1.get((i-1)),0,maxx,plotX1, plotX2);
x2plot=map((Float)array1.get(i),0,maxx,plotX1, plotX2);
yplot=map((Float)array2.get((i-1)),0,maxy,plotY2, plotY1);
y2plot=map((Float)array2.get(i),0,maxy,plotY2, plotY1);
line(xplot,yplot,x2plot,y2plot);
output.println((Float)array1.get((i))+" "+(Float)array2.get((i)));
}
if(i==array1.size()-1)
{
output.flush();
output.close();
}
}
}
//if the next data is the same as the previous store skip it
private boolean checkdata(ArrayList array1,ArrayList array2 , float d2, float d1)
{
//if the last index equals the incoming value
if((((Float)array1.get(array1.size()-1))==d2) && (((Float)array2.get(array2.size()-1))==(d1)))
{
return true;
}
else
{
return false;
}
}
private float getmaxvalue(ArrayList array1)
{
float maxval=0;
for(int i=0;i<array1.size();i++)
{
if(maxval<(Float)array1.get(i))
maxval=(Float)array1.get(i);
}
return maxval;
}
private float getminvalue(ArrayList array1)
{
float minval=(Float)array1.get(0);
for(int i=0;i<array1.size();i++)
{
if(minval>(Float)array1.get(i))
minval=(Float)array1.get(i);
}
return minval;
}
private void labelx()
{
float distx= (plotX2-plotX1)/10;
float disty= (plotY2-plotY1)/10;
float incrementox=(maxx-minx)/10;
float incrementoy=(maxy-miny)/10;
for(int m=0;m<10;m++)
{
textFont(labelFont);
if(m==0)
{
text(0,plotX1,plotY2+20);
text(0,plotX1-25,plotY2);
}
else
{
text(incrementox*(m),plotX1+(distx)*(m),plotY2+20);
stroke(180,0,0);
line(plotX1,plotY1+(disty)*m,plotX2,plotY1+(disty)*m); //lineas horizontales
text(incrementoy*(m),plotX1-25,plotY2-(disty)*(m));
line(plotX1+(distx)*m,plotY1,plotX1+(distx)*m,plotY2);
}
}
}
private void draw2()
{
strokeWeight(2);
background(100);
stroke(0);
//graph
plotX1 = 120;
plotX2 = width - 80;
labelX = 50;
plotY1 = 60;
plotY2 = height - 70;
labelY = height - 25;
//plot
rectMode(CORNERS);
fill(0,0,0);
rect(plotX1, plotY1, plotX2, plotY2);
}
//gui
public void controlEvent(ControlEvent theEvent) {
println(theEvent.controller().name());
if(theEvent.controller().name()=="Save Image")
{
//create graphics
save("test.jpg"); //.tif
println("Image Saved Succesfully");
}
if(theEvent.controller().name()=="Plot")
{
maxy=getmaxvalue(arrayf); //get data max and min values
miny=getminvalue(arrayf);
maxx=getmaxvalue(arrayd);
minx=getminvalue(arrayd); //can put these in the plotfunction to reduce processing...
draw2();
Plotarray(arrayd,arrayf);
labelx();
}
}
thanks!
import processing.serial.*;
import controlP5.*;
Serial myPort; // Create object from Serial class
float val,x,y,x2,y2; // Data received from the serial port
String sdata;
String f[],d[];
//ArrayList arraydata= new ArrayList(); //create array instance
ArrayList arrayf= new ArrayList();
ArrayList arrayd= new ArrayList();
float plotX1, plotY1;
float plotX2, plotY2;
float labelX, labelY; //f,d
float minx,maxx,miny,maxy;
PFont plotFont;
PFont labelFont;
PrintWriter output;
ControlP5 controlP5;
// ####### Setup ###################
void setup()
{
size(800, 600);
myPort = new Serial(this, "/dev/ttyUSB0", 9600);
strokeWeight(2);
background(100);
//graph
plotX1 = 120;
plotX2 = width - 80;
labelX = 50;
plotY1 = 60;
plotY2 = height - 70;
labelY = height - 25;
plotFont = createFont("SansSerif", 20);
labelFont = createFont("SansSerif", 8);
textFont(plotFont);
//plot
rectMode(CORNERS);
fill(0,0,0);
rect(plotX1, plotY1, plotX2, plotY2);
//firts data are boundaries of the graph
arrayf.add(0,plotY2);
arrayd.add(0,plotX1);
//output file
// Create a new file in the sketch directory
output = createWriter("positions.txt");
//gui
controlP5= new ControlP5(this);
controlP5.addButton("Plot",1,60,20,40,30); //parameters : name, value (float), x, y, width, height
controlP5.addButton("Save Image",1,120,20,80,30);
}
// ########################
// ####### DRAW ###################
void draw()
{
if ( myPort.available() > 0) { // If data is available
sdata=myPort.readStringUntil(13);
if (sdata != null){
if (sdata.indexOf("f")>=0) //sensor 1
{
f=split(sdata,' ');
y=float(f[1]);
println(f[1]);
}
else if (sdata.indexOf("d")>=0)//sensor 2
{
d=split(sdata,' ');
x=float(d[1]);
println(d[1]);
}
}
}
stroke(204, 102, 0);
//myPort.clear();
if (checkdata(arrayf,arrayd,y,x)==false) //if the incoming data is not present in the array
{
UpdateArray(arrayf,y);
UpdateArray(arrayd,x);
}
//println(sdata);
//maxy=getmaxvalue(arrayf); //get data max and min values
//miny=getminvalue(arrayf);
//maxx=getmaxvalue(arrayd);
//minx=getminvalue(arrayd); //can put these in the plotfunction to reduce processing...
//draw2();
//Plotarray(arrayd,arrayf);
//labelx();
}
// #####################
// ####### Functions ###################
private void UpdateArray(ArrayList array1, float d1)
{
array1.add(d1);
}
private void Plotarray(ArrayList array1, ArrayList array2)
{
stroke(204, 102, 0);
float xplot,x2plot,yplot,y2plot ; //remapped values
for(int i=0;i<array1.size();i++)
{
if(i>1){
xplot=map((Float)array1.get((i-1)),0,maxx,plotX1, plotX2);
x2plot=map((Float)array1.get(i),0,maxx,plotX1, plotX2);
yplot=map((Float)array2.get((i-1)),0,maxy,plotY2, plotY1);
y2plot=map((Float)array2.get(i),0,maxy,plotY2, plotY1);
line(xplot,yplot,x2plot,y2plot);
output.println((Float)array1.get((i))+" "+(Float)array2.get((i)));
}
if(i==array1.size()-1)
{
output.flush();
output.close();
}
}
}
//if the next data is the same as the previous store skip it
private boolean checkdata(ArrayList array1,ArrayList array2 , float d2, float d1)
{
//if the last index equals the incoming value
if((((Float)array1.get(array1.size()-1))==d2) && (((Float)array2.get(array2.size()-1))==(d1)))
{
return true;
}
else
{
return false;
}
}
private float getmaxvalue(ArrayList array1)
{
float maxval=0;
for(int i=0;i<array1.size();i++)
{
if(maxval<(Float)array1.get(i))
maxval=(Float)array1.get(i);
}
return maxval;
}
private float getminvalue(ArrayList array1)
{
float minval=(Float)array1.get(0);
for(int i=0;i<array1.size();i++)
{
if(minval>(Float)array1.get(i))
minval=(Float)array1.get(i);
}
return minval;
}
private void labelx()
{
float distx= (plotX2-plotX1)/10;
float disty= (plotY2-plotY1)/10;
float incrementox=(maxx-minx)/10;
float incrementoy=(maxy-miny)/10;
for(int m=0;m<10;m++)
{
textFont(labelFont);
if(m==0)
{
text(0,plotX1,plotY2+20);
text(0,plotX1-25,plotY2);
}
else
{
text(incrementox*(m),plotX1+(distx)*(m),plotY2+20);
stroke(180,0,0);
line(plotX1,plotY1+(disty)*m,plotX2,plotY1+(disty)*m); //lineas horizontales
text(incrementoy*(m),plotX1-25,plotY2-(disty)*(m));
line(plotX1+(distx)*m,plotY1,plotX1+(distx)*m,plotY2);
}
}
}
private void draw2()
{
strokeWeight(2);
background(100);
stroke(0);
//graph
plotX1 = 120;
plotX2 = width - 80;
labelX = 50;
plotY1 = 60;
plotY2 = height - 70;
labelY = height - 25;
//plot
rectMode(CORNERS);
fill(0,0,0);
rect(plotX1, plotY1, plotX2, plotY2);
}
//gui
public void controlEvent(ControlEvent theEvent) {
println(theEvent.controller().name());
if(theEvent.controller().name()=="Save Image")
{
//create graphics
save("test.jpg"); //.tif
println("Image Saved Succesfully");
}
if(theEvent.controller().name()=="Plot")
{
maxy=getmaxvalue(arrayf); //get data max and min values
miny=getminvalue(arrayf);
maxx=getmaxvalue(arrayd);
minx=getminvalue(arrayd); //can put these in the plotfunction to reduce processing...
draw2();
Plotarray(arrayd,arrayf);
labelx();
}
}
1