2
votes
import processing.serial.*;
int newval = 0;      //Varible for feeding values onto the end of the 
Serial myPort;
String temp = "0";   //Temporary varible for pulling Strings off the serial connection
float[] vals;        //Array of data to be graphed

void setup() {
  //Setting up size and the serial port
  size(1920,1080);
  smooth();
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  //Initilizing vals 
  vals = new float [width];

  //Setting everything in vals to 0
  for (int i = 0; i < vals.length - 1;i++)
  {
    vals [i] = 0;
  }

}


void draw() {

  background(0);

  //Drawing function
  for (int i = 0; i < vals.length - 1; i += 1)
  {
    stroke(255);
    strokeWeight(1);
    line(i,vals[i], i+1, vals[i+1]);
  }

  //Push everything in vals back one slot
  for (int i = 0; i < vals.length - 1; i++)
  {
    vals[i] = vals[i+1];
  }

  //Pull data from the serial connection
  if ( myPort.available() > 0) 
  {
    temp = (new String(myPort.readBytesUntil('\n'))).trim(); //Take a string off of the serial 
                                                             //port and remove any spaces
    newval = Integer.parseInt(temp);                         //Convert temp to an integer
  }

  vals[vals.length - 1] = newval;                            //Set the last space in vals to newval
  println(newval);                                           //Print newval for debugging purposes

}

Above is the code I'm using.

I have hooked up my Arduino to a potentiometer and then hooked that up to one of the analog input pins, effectively creating a variable voltage divider. This code successfully pulls the first integer piece of data, and graphs it as a continuous horizontal line, failing to get any other data. I have checked, and it does enter the if (myPort.availible > 0 ) but newval doesn't change. The Arduino is behaving perfectly and I checked the serial data via the Arduino IDE and that appears to be behaving fine, therefore it must be a program issue. Please help!

EDIT: I have kept trying. One suggestion was to implement a serialevent function. That yielded identical results.

Arduino code:

#define PIN A0

void setup()
{
  Serial.begin(9600);
}

void loop ()
{
  Serial.print(analogRead(PIN));
  Serial.print('\n');
}

New (working) Arduino code:

#define PIN A0

void setup()
{
  Serial.begin(9600);
}

void loop ()
{
  Serial.print(analogRead(PIN));
  Serial.print('\n');
  delay(20);
}
2
Can you also post your Arduino code or at least the part where you write data to Serial so it's easier to test/debug ?George Profenza

2 Answers

1
votes

I do not know how the serial programming works. But I think I see a possible spot from which this issue could arise up :

temp = (new String(myPort.readBytesUntil('\n'))).trim();

Here, readBytesUntil actually tries to find the new line character. If it is not able to find the character, it returns null.

If temp has null value, newval will lead to a null pointer exception. Try to put up a try catch block around that if loop as follows:

try
{
  if ( myPort.available() > 0) 
  {
    temp = (new String(myPort.readBytesUntil('\n'))).trim();   //Take a string off of the serial     
                                                                 port and remove any spaces
    newval = Integer.parseInt(temp);                            //Convert temp to an integer
  }
}
catch(NullPointerException e)
{
 e.printStackTrace();
}

Now if you encounter a Null pointer exception, it means that your port does not a '\n' character at the end of the possible string that you wish to parse up. In that case, I suppose you have to voluntarily add the new line character while pushing the data.

I seriously do not have any idea about ports and stuff. But if the above description helps you to get out of the issue, I would be glad.

0
votes

Can you try using serialEvent ?

import processing.serial.*;
int newval = 0;      //Varible for feeding values onto the end of the 
Serial myPort;
String temp = "0";   //Temporary varible for pulling Strings off the serial connection
float[] vals;        //Array of data to be graphed

void setup() {
  //Setting up size and the serial port
  size(1920,1080);
  smooth();
  stroke(255);
  strokeWeight(1);
  try{
    String portName = Serial.list()[0];
    myPort = new Serial(this, portName, 9600);
    myPort.bufferUntil(10);//ASCII linefeed 
  }catch(Exception e){
    System.err.println("Error opening serial device\nPlease check if the device is connected !");
  }
  //Initilizing vals 
  vals = new float [width];//initializez with 0s by default

}


void draw() {
  background(0);

  //Drawing function
  for (int i = 0; i < vals.length - 1; i ++)
  {
    line(i,vals[i], i+1, vals[i+1]);
  }


}

void serialEvent(Serial p) { 
  try{
    //Push everything in vals back one slot
    for (int i = 0; i < vals.length - 1; i++)
    {
      vals[i] = vals[i+1];
    }

    temp = p.readString().trim(); 
    newval = Integer.parseInt(temp);

    vals[vals.length - 1] = newval;                              //Set the last space in vals to newval
    println(newval);                                             //Print newval for debugging purposes
  }catch(Exception e){
    println("error parsing serial data: "+temp);
  }
}