Problem of a moving circle Interface which corresponds to position XY data provided by Arduino

edited May 2017 in Arduino

Hello everyone,

I'm writing a Processing program displaying a circle which will move according to the X Y position data received from Arduino. However, the problem is that the circle movement doesn't seem to work as I expected (when I move, I have to wait 5 6 seconds later to see the circle really moves slowly. Moreover, the circle always disappears and then appears quickly again, which is annoying to my eyes).

I post my Processing code below, and if you find any problem (due to buffer, background or anything), or if you have a better approach/solution for my objectives, please tell me. I send 2 float data (with 4 floating digits), at baudrate 115200 in Arduino DUE.

Code Processing:

    // 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);
      //The value of xf and yf is normally very small, so I multiply them with a value so I can actually see them moving in the Processing screen
          ellipse(xf*1000+450, yf*1000+450, 40, 40);
      }
      }

Thank you.

Answers

  • Answer ✓

    I am not sure why you have try-catch blocks there if you are still processing and generating values for xf and yf. Check the examples provided: https://processing.org/reference/libraries/serial/index.html

    I suggest implementing the changes below in your code (also follow GoTo's link).

    Related to your last operation, you said that you are multiplying your values by a factor so to be able to see motion. If you describe the nature of your values (description, where they come from and how you use them in Processing) then I could provide a relevant sample code using the map() function instead: https://processing.org/reference/map_.html

    Kf

        void draw() {
    
          if (serialPort.available() > 0) {
            byte[] inBuffer = new byte[30]; // holds serial message
    
            serialPort.readBytesUntil(ENTER, inBuffer);
    
            if (inBuffer==null) {
              return;
            }
    
            String myString = new String(inBuffer);
            String[] nums = split(myString, ' ');
    
            if (nums.length==2) {
              x[0] = float(nums[0]);
              x[1] = float(nums[1]);
    
              //Calculate x and y position based on data sent by Arduino
              xf = 1.508*tan((PI/180)*(121*x[0]+61*(x[0]*x[0])+35*x[1]-171*(x[1]*x[1])+1.7));
              yf = 1.508*tan((PI/180)*(-290*x[1]-799*(x[1]*x[1])+20*x[0]+39.2*(x[0]*x[0])-7.1));
              background(51);
              //The value of xf and yf is normally very small, so I multiply them with a value so I can actually see them moving in the Processing screen
              ellipse(xf*1000+450, yf*1000+450, 40, 40);
            }
          }
        }
    
  • edited May 2017

    I found out my problem. It is because I start my Arduino first and then I start my Processing IDE after a while. Since the Processing will collect all the data from the beginning (when Arduino starts) so my position data when I move is not real time (several seconds delay).

    Is there any way to put a code in Arduino that says "just start the Arduino program when I click button "Run" in Processing?" or something like that to solve my problem above?

    I'm using Arduino DUE.

    Thanks

  • edited June 2017

    That's not the way. Make Processing start by throwing away all that has already arrived. Let's continue this in your later thread. https://forum.processing.org/two/discussion/22602/processing-interface-stops-after-playing-for-2-3-minutes#latest

Sign In or Register to comment.