Serial communication (float message sending) between Arduino and Processing too slow (not real time)

edited May 2017 in Arduino

Hello,

I'm writing a Processing program that collects the data from Arduino (from a sensor), calculate the data to reconstruct X Y position and then makes a circle move according to the XY position. However, the problem is that I send 2 float data (with 3 floating digits) and when I open the Processing Interface, the circle can move according to how I move my sensor but it is very slow in comparison with the speed I move (and there are some delays).

I use Arduino DUE; Baud Rate 128000.

Is there any way to solve this problem? I publish my Processing code below:

    // import libraries
    import java.awt.Frame;
    import java.awt.BorderLayout;
    import processing.serial.*;

    // Serial port to connect to
    String serialPortName = "COM3";
    float[] x= new float[2];
    float xf;
    float yf;
    /* SETTINGS END */

    Serial serialPort; // Serial port object


    void setup() {
      size(900,900);
      background(51);
      noStroke();
      //frameRate(100);
      serialPort = new Serial(this, serialPortName, 115200); 
    }

    void draw() {

      if (serialPort.available() > 0) {
        byte[] inBuffer = new byte[30]; // holds serial message
          try {
            serialPort.readBytesUntil('\r',inBuffer);
          }
          catch (Exception e) {
          }
        String myString = new String(inBuffer);
        String[] nums = split(myString,' ');
            try {
                x[0] = float(nums[0]);
                x[1] = float(nums[1]);
            }
            catch (Exception e) {
            }    
      //Calculate x and y position based on data sent by Arduino
        xf = 1.508*tan((3.14/180)*(121*x[0]+61*(x[0]*x[0])+35*x[1]-171*(x[1]*x[1])+1.7));
        yf = 1.508*tan((3.14/180)*(-290*x[1]-799*(x[1]*x[1])+20*x[0]+39.2*(x[0]*x[0])-7.1));
      background(51);
     //Draw data
        float x_m = map(xf,-0.4,0.4,0,900);
        float y_m = map(yf,-0.4,0.4,0,900); 
        ellipse(x_m,y_m,50,50)
      }
      }
Tagged:
Sign In or Register to comment.