Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help on addPoint syntax #12

Open
mag-immo opened this issue Mar 29, 2019 · 2 comments
Open

Need help on addPoint syntax #12

mag-immo opened this issue Mar 29, 2019 · 2 comments

Comments

@mag-immo
Copy link

Hi,
I would like to plot a simple graph in real time from two sensors values sent by an arduino based on the mouse position example.
My problem is that I don't understand the syntax of the example.
The code below works perfectly with the mouse position but when I try to change it with my values , I get
The function "addPoint()" expects parameters like : "addPoint(GPoint)"

public void setup(){
   plot1 = new GPlot(this);
   plot1.setPos(350, 150);
   plot1.setDim(700, 500);
   plot1.getTitle().setText("Effet tactile");
   plot1.getXAxis().getAxisLabel().setText("Déplacement (mm)");
   plot1.getYAxis().getAxisLabel().setText("Force (N)");
}
public void draw(){   
   plot1.addPoint(mouseX, -mouseY, "(" + str(mouseX) + " , " + str(mouseY) + ")");
}

Could you please help me ?

Regards,
Yves

@jagracar
Copy link
Owner

Hi Yves,

I tried your example and it works for me:

import grafica.*;

GPlot plot1;

public void setup() {
  size(900, 700);
  background(150);

  plot1 = new GPlot(this);
  plot1.setPos(10, 10);
  plot1.setDim(700, 500);
  plot1.getTitle().setText("Effet tactile");
  plot1.getXAxis().getAxisLabel().setText("Déplacement (mm)");
  plot1.getYAxis().getAxisLabel().setText("Force (N)");
}
public void draw() {   
  plot1.addPoint(mouseX, -mouseY, "(" + str(mouseX) + " , " + str(mouseY) + ")");
  // Also possible
  plot1.addPoint(mouseX, -mouseY);

  plot1.defaultDraw();
}

There are several addPoint methods in Grafica. You can see them listed here:

https://github.com/jagracar/grafica/blob/master/src/grafica/GPlot.java#L1928

@mag-immo
Copy link
Author

mag-immo commented Apr 1, 2019

Hi Javier,
Thank you for your answer
I tried using this method

public void addPoint(float x, float y)

It works but the program crashes at the start with this error ArrayIndexOutOfBoundsExeption : 1
Could you please take a look at my code ?

  import grafica.*;
  import processing.serial.*;    // Importing the serial library to communicate with the Arduino 
  Serial myPort;      // Initializing a vairable named 'myPort' for serial communication
  
  public GPlot plot1;
  public GPointsArray polygonPoints;
  
  String valeurs;
  String deplacement;
  String val_force ;
  String values;

public void setup(){
  
  size(1200, 800, JAVA2D);
  
  myPort  =  new Serial (this, "COM4",  9600); // Set the com port and the baud rate according to the Arduino IDE

  myPort.bufferUntil('\n');
   
  plot1 = new GPlot(this);
  plot1.setPos(350, 150);
  plot1.setDim(700, 500);
  plot1.getTitle().setText("Effet tactile");
  plot1.getXAxis().getAxisLabel().setText("Déplacement (mm)");
  plot1.getYAxis().getAxisLabel().setText("Force (N)");
}

public void draw(){    
    valeurs = myPort.readStringUntil('\n');   
    String values[] = split(valeurs,' ');   
    if (values.length > 0) {
      deplacement = values[0];
      val_force = values[1];
    }    
   
   plot1.addPoint(float(deplacement),float( val_force));

   // Reset the points if the user pressed the space bar
   if (keyPressed && key == ' ') {
    plot1.setPoints(new GPointsArray());
   }
  
  plot1.beginDraw();
  plot1.drawBackground();
  plot1.drawBox();
  plot1.drawXAxis();
  plot1.drawYAxis();
  plot1.drawTitle();
  plot1.drawGridLines(GPlot.BOTH);
  plot1.drawLines();
  plot1.drawPoints();
  plot1.endDraw();
}

Thank you
Regards,
Yves

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants