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();
// ####### 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(); } // #####################
//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; } }
//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(); } }