no Graph with data from Arduino?

edited December 2015 in Arduino

tried a simple example to draw a Graph from arduino data. No graph visible? Running on Windows 10

import processing.serial.*;

Serial myPort; // The serial port int xPos = 0; // horizontal position of the graph

void setup () { // set the window size: size(400, 600);

// List all the available serial ports println(Serial.list());

// open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[1], 9600); // myPort = new Serial(this, "COM8", 9600);

// don't generate a serialEvent() unless you get a newline character: myPort.bufferUntil('\n'); // set inital background: //background(0); schwarzer Hintergrund background (100,100,255); } void draw () { // everything happens in the serialEvent() }

void serialEvent (Serial myPort) { // get the ASCII string: String inString = myPort.readStringUntil('\n');

if (inString != null) { // trim off any whitespace: inString = trim(inString); // convert to an int and map to the screen height: float inByte = float(inString);

inByte = map(inByte, 0, 1023, 0, height); println(inString); // to see if datas are incoming

// draw the line: stroke(0,0,0); line(xPos, height, xPos, height - inByte);

// at the edge of the screen, go back to the beginning: if (xPos >= width) { xPos = 0; background(0); //background (216,225,165); } else { // increment the horizontal position: xPos++; } } }

Answers

  • You'll get a better response if you format your code. Here's how:

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • Answer ✓

    I didn't test your code, but you are obviously drawing stuff inside of serialEvent().
    That used to work in previous versions of processing and you are not the first on with this arduino-graph problem. In the current version, you should only manipulate values from serialEvent() and do the drawing inside of draw().

  • edited December 2015

    Even before P3, drawing outside the "Animation" Thread could end up corrupting the canvas.
    It's just that P3 is more fragile for such misbehavior! >-)

  • Thank you benja. Yes you are right, this example with drawing inside of serialEvent() doesn't work. :(

  • Answer ✓

    But it is not too hard to change the code. Just store incoming values and set a flag, that new data has arrived in serialEvent(). Then in draw you can check if there is new data, and draw the visuals you want.

    import processing.serial.*;
    
    Serial myPort; 
    int xPos = 0;
    float currentValue;
    boolean newData = false;
    
    void setup () { 
      size(400, 600);
      myPort = new Serial(this, Serial.list()[2], 9600);
      myPort.bufferUntil('\n'); 
      background(255); 
      stroke(0);
    } 
    void draw () { 
    
      if (newData) {
        // draw the line: 
        line(xPos, height, xPos, height - currentValue);
    
        // at the edge of the screen, go back to the beginning: 
        if (xPos >= width) { 
          xPos = 0; 
          background(255);
        } else { 
          xPos++;
        }
    
        newData =false;
      }
    }
    
    void serialEvent (Serial myPort) { 
      String inString = myPort.readStringUntil('\n');
    
      if (inString != null) { 
        inString = trim(inString); 
        currentValue = float(inString);
        currentValue = map(currentValue, 0, 1023, 0, height); 
        newData = true;
      }
    }
    
  • Bingo! Thank you very much. Have a nice weekend. Guido

  • edited December 2015

    When using bufferUntil(), readStringUntil() is redundant inside serialEvent().
    Just a simpler readString() is enough. Don't forget trim() in order to remove \n!

    P.S.: Of course, no need to check for null either! :>

  • I tried your method, but still get the same error, disabling port.

    What am I doing wrong?

    Thanks

    import processing.serial.*; Serial port; int BaseEncGlobal; int ElbowEncGlobal; int ShoEncGlobal; int VertRotEnc; int HorRotEnc; int GripEncGlobal;

    double X_axis; double Y_axis; double Z_axis; double GripAngle;

    String data; boolean newData = false;

    PFont font;

    void setup() { size(1280,800); //port = new Serial(this, "/dev/cu.usbserial-A50285BI", 115200); port = new Serial(this, "/dev/cu.usbmodem2574541", 115200); port.bufferUntil('\n'); font = loadFont("AgencyFB-Bold-200.vlw"); textFont(font, 40); }

    void draw()

    { if (newData == true) {

    int spaceDown = 55; background(0,0,0); fill(46, 209, 2); text(BaseEncGlobal, 70, spaceDown); fill(0, 102, 153); text(ShoEncGlobal, 70, spaceDown2); fill(0, 102, 153); text(ElbowEncGlobal, 70, spaceDown3); fill(0, 102, 153); text(VertRotEnc, 70, spaceDown4); fill(0, 102, 153); text(HorRotEnc, 70, spaceDown5); fill(0, 102, 153); text( GripEncGlobal, 70, spaceDown*6);

    text(Double.toString(X_axis/10), 270, spaceDown ); newData =false; }

    }

    void serialEvent (Serial port) {

    data = port.readStringUntil('\n'); if (data != null) { data = trim(data);

    int[] nums = int(split(data, ',')); BaseEncGlobal = nums [1]; ShoEncGlobal = nums [2]; ElbowEncGlobal = nums [3]; VertRotEnc = nums [4]; HorRotEnc = nums [5]; GripEncGlobal = nums [6]; X_axis = nums [7]; Y_axis = nums [8]; Z_axis = nums [9]; GripAngle = nums [10]; //println(Double.toString(X_axis/10));

    println(data);

    newData = true; }

    }

  • There is a New forum

    Please as there as well

    You could link back to this thread

    Thank you

Sign In or Register to comment.