Problem to get int form arduino into processing

Hello, I have set up an SRF05 range finder ( with my arduino dicemilia, and got it up and running just fine with this sketch for the arduino:

  Ultrasonic Sensor sketch

  This program reads a Devantech SRF04 ultrasonic distance sensor
  The SRF04 sensor's pins are connected as described below.
  Created 9 November 2006
  By Tom Igoe and Neilson Abeel

#define echoPin 2             // the SRF04's echo pin
#define initPin 3             // the SRF04's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse

void setup() {
  // make the init pin an output:
  pinMode(initPin, OUTPUT);
  // make the echo pin an input:
  pinMode(echoPin, INPUT);
  // initialize the serial port:

void loop() {
  // send the sensor a 10microsecond pulse:
  digitalWrite(initPin, HIGH);
  digitalWrite(initPin, LOW);

  // wait for the pulse to return. The pulse
  // goes from low to HIGH to low, so we specify
  // that we want a HIGH-going pulse below:

  pulseTime = pulseIn(echoPin, HIGH);
  // print out that number

I get a perferct reading on the serial monitor window.

But I have a hard time to get this reading of the distance inprocessing (it will be udes to scale an image)

I just want to get this distance reading in an int variable to use it as a multiplier.

I have tried many examples, this one is the closest to working:

import processing.serial.*; 
Serial port;            // Create object from Serial class 
int val;                // Data received from the serial port  
 String portname = "/dev/cu.usbserial-A4001LGU";  // find the name of your serial port in your system setup!
  int baudrate = 9600;  //  set baudrate here 
void setup() { 
  size(200, 200); 
  frameRate(10);                 // Run 10 frames per second 
  // Open the port that the board is connected to and use the same speed (9600 bps) 
  port = new Serial(this,portname, 9600); 
void draw() { 
  while (port.available() > 0) {
    String inBuffer = port.readString();   
    if (inBuffer != null) {

I do get the reading but I can’t seem to use this value stored in “inbuffer” to put in an int variable since it is not a number but a string (well this is where I think the problem is…but I am quite a newbee so it is a guess)

I also tried:

if (0 < port.available()) {    // If data is available to read,  
    val =;     // read it and store it in val 

But then the numbers are crazy, nothing in common with the sensor reading…

Yes and I also tried this ( and other similar pieces of code) which seems to be the right way

  // Process each one of the serial port events
  while (port.available() > 0) {

void serialEvent(int serial) 
  if(serial != 10) { 
    buff += char(serial);
  else {
    buff = buff.substring(1, buff.length()-1);
    // Capture the string and print it to the commandline
    // we have to take from position 1 because 
    // the Arduino sketch sends EOLN (10) and CR (13)
    if (val == 0) {
      val = 255;
    else {
      val = 0;
    // Clear the value of "buff"
    buff = "";

but than I get no reading, but an error each time:

java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.substring(
at Temporary_3761_4938.serialEvent(

Try holding down the reset button on the Arduino while running the Processing program, then letting go of the reset button after Processing starts. I need to fix the Graph example to be more robust.

I did that and still gets the same error message, we have this exhibition with my students this saturday, and I still can't get it to work, do you have an idea, isn't it possible to "change" the srting to an integer in procesing any other way?

Check that the buffer is non empty before calling parseInt().

I know I am too late to help, but this may help others with this problem. c. kazah writes "I get a perferct reading on the serial monitor window." And that seems to be the problem. With the graph processing sketch, at least, you cannot have the serial monitor on in the arduino window - it seems to make the serial output of Arduino erratic. Processing starts getting spurious strings that are empty or corrupted concatenations of the right reading. And those spurious strings are reflected in the Arduino serial monitor.

First thing, try disabling the serial monitor, then hold down reset button until Processing is running again. That is the only way I could get it running properly again after I had also turned on the serial monitor.

Mellis, is there a fix for this?