PING to processing

ok, so i’m running 3 ping sensors, and trying to get them to control the RGB in processsing. How can I change this code to make it work? right now I keep getting “StringIndexOutOfBoundsException: String index out of range: 0” or “StringIndexOutOfBoundsException: String index out of range: -1”

arduino code:

int pingPins [] = {7, 5, 3};

void setup()

void loop()
  long duration, inches;

  for (int i=0; i<3; i++)
  pinMode(pingPins[i], OUTPUT);
  digitalWrite(pingPins[i], LOW);
  digitalWrite(pingPins[i], HIGH);
  digitalWrite(pingPins[i], LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPins[i], INPUT);
  duration = pulseIn(pingPins[i], HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);


long microsecondsToInches(long microseconds)
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See:
  return microseconds / 74 / 2;

is there a way I can get the for loop that is running the pins to print different characters at the beginning of each string? or is there some way I can make it work with numbers?

and the processing code:

import processing.serial.*;

String buff = "";
int rval = 0, gval = 0, bval = 0;
int NEWLINE = 10;

Serial port;

void setup()
  size (640, 360);
  String portName = Serial.list()[0];
  port = new Serial(this, portName, 9600);

void draw()
  while (port.available() > 0) {
  background(rval, gval, bval);

void serialEvent(int serial) 
  if(serial != NEWLINE) { 
    buff += char(serial);
  } else {
    char c = buff.charAt(0);
    buff = buff.substring(1);
    buff = buff.substring(0, buff.length()-1);
    if (c == '7')
      rval = Integer.parseInt(buff);
    else if (c == '5')
      gval = Integer.parseInt(buff);
    else if (c == '3')
      bval = Integer.parseInt(buff);
    buff = "";

it’s just the virtual color mixer sketch very slightly modified.

If you sent a letter instead of pin number it would be easer to parse your Processing code

In the Arduino code you could have an array like: char RGB[] = {'R','G','B'};

then in your loop, send the letter indexed by your loop variable

In Processing, check if the incoming character is a letter ( e.g. greater or equal to 'A') Use this to identify the color and then retrieve the digits as you do now.

ok, that's what i thought. unsure how to attach the letters to the numbers with that for loop running

char RGB[] = {'R','G','B'};

// convert the time into a distance inches = microsecondsToInches(duration); Serial.print(RGB*);* * Serial.println(inches);*

I would think that you if put the code above in the code in your first post it will do what you want.


ok, now i've got a new problem. it runs fine until i run the processing sketch, then the serial gets all crazy. watching it in the monitor, it goes from

R114 G107 B45

and so on to:



R2 4




and then i get out of bounds exceptions in processing

baud rates match, btw

You may want to read up on using serial in Processing. I think you either want to read the entire string (readStringUntil(LF) or check that data is available before reading each byte.

thanks for all your help, man. i've gotten this exact sketch to work with potentiometers, not sure what the deal is. thanks again. btw, i know it's probably silly to use an array for 3 pins, i'm just trying to get better at code for future use more than anything.