简体   繁体   中英

Processing Serial Read from Arduino causing Java PApplet error

I am reading serial data from an arduino sketch that I uploaded to a teensy 2.0 / this also occurs when running a regular arduino. I am then splitting the strings and converting into an integer array with Processing's int() function, in order to be able to trigger sounds. The arduino sketch is reading in capacitive sensor data. It runs well for about 10-15s and then starts throwing up the following error.


 Exception in thread "Animation Thread" java.lang.NullPointerException
        at processing.core.PApplet.parseInt(PApplet.java:9127)
        at processing.core.PApplet.parseInt(PApplet.java:9113)
        at NightGames_Tree.draw(NightGames_Tree.java:59)
        at processing.core.PApplet.handleDraw(PApplet.java:2266)
        at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
        at processing.core.PApplet.run(PApplet.java:2140)
        at java.lang.Thread.run(Thread.java:695)

My code is as follows: In Arduino:

 #include <CapacitiveSensor.h>

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        

void setup()                    
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);
}

void loop()                    
{
    long start = millis();
    long total1 =  cs_4_2.capacitiveSensor(30);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    

    Serial.print(total1);                  // print sensor output 1
    Serial.println("\t");

    delay(100);                            
}

In Processing:

    import ddf.minim.spi.*;
    import ddf.minim.signals.*;
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.ugens.*;
    import ddf.minim.effects.*;

    import processing.serial.*;

    Minim minim;
    AudioPlayer tree1;
    AudioPlayer tree2;

    Serial myPort;
    String val;

void setup(){

    String portName = Serial.list()[12]; // calls on the port which teensy is on
    myPort = new Serial(this, portName, 9600);
    println(Serial.list()); // prints list of ports that processing can access

    //setup sound

    minim = new Minim(this);
    tree1 = minim.loadFile("tree1.aif");

}

void draw()
{


    if (myPort.available() > 0) {
        val = myPort.readStringUntil('\n');
        int[] list = int(split(val, '\t')); // splits string into list based on tab after
    int sum = 0;
    int sum1 = 0;
 //  print(list.length);

      sum = sum + list[0];
      sum1 = sum1 + list[1];

    print(sum);
    print('\t');
    print(sum1);
    print('\t');
    print('\n');

   //print(val);

    if (sum1 > 500) {
      tree1.play(0);
    } else if (sum1 <500){
    }

  }
}

When the error shows up after running for a while, Processing highlights this line of code

int[] list = int(split(val, '\t'));

Thanks in advance for any help. I am running this on a Mac.

I ran this on a Windows box, using Processing 3.0a4 and Arduino 1.6.3 On Arduino, I commented out everything having to do with the capacitive sensor library because I didn't have it. `

//#include <CapacitiveSensor.h>
//CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        
void setup()                    
{
// cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     
// turn off autocalibrate on channel 1 - just as an example

Serial.begin(9600); }

void loop()                    
{
long start = millis();
// long total1 =  cs_4_2.capacitiveSensor(30);

Serial.print(millis() - start);        
// check on performance in milliseconds
Serial.print("\t");                    

//Serial.print(total1);                  // print sensor output 1
Serial.println("\t");

delay(100);                            
}

In Processing I commented out the line with the sound file. The error I got pointed to this line:

int[] list = int(split(val, '\\t'));

It seems possible that there is a type mismatch that allows the list to stream for a short bit but them jams up the processor.

Turns out processing was detecting a null object so adding a check for null objects solved the problem. The condensed code is:

import processing.serial.*;

Serial myPort;
String val;

void setup(){
  String portName = Serial.list()[12]; // calls on the port which teensy is on
  myPort = new Serial(this, portName, 9600);
} 

void draw()
{
  if (myPort.available() > 0) {
    val = myPort.readStringUntil('\n');
    if (val!=null){
    int[] list = int(split(val, '\t')); // splits string into list based on tab 

    int sum = 0;
    int sum1 = 0;
    int sum2 = 0;

    sum = sum + list[0];
    sum1 = sum1 + list[1];
    sum2 = sum2 + list[2];

    print(sum);
    print('\t');
    print(sum1);
    print('\t');
    print(sum2);
    print('\t');
    print('\n');
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM