Stringoutofboundsexception arduino to processing

Hello guys I have connected 3 rgb leds to arduino and I want processing to read its value and set it as background.

Here is the arduino code which works perfectly fine(by the way its analog fading led program)

// Fading LED
// Dhruv Adhia - Right now trying to send colors to processing

int value = 0; // variable to keep the actual value
int green_ledpin = 9; // light connected to digital pin 9
int blue_ledpin = 10;
int red_ledpin=11;

void setup()
{

/pinMode(green_ledpin,OUTPUT);
pinMode(red_ledpin,OUTPUT);
pinMode(blue_ledpin,OUTPUT);
/
Serial.begin(9600);

}

void loop()
{
/* for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
Serial.print(“G”);
Serial.println(analogRead(green_ledpin));
digitalWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
Serial.print(“G”);
Serial.println(analogRead(green_ledpin));
digitalWrite(ledpin, value);
delay(30);
} */
green_natural_light();
delay(1000);
blue_natural_light();
delay(1000);
red_natural_light();
}

void green_natural_light()
{

/*Serial.print(“G”);
Serial.println(analogRead(green_ledpin));
digitalWrite(green_ledpin, HIGH); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
Serial.print(“G”);
Serial.println(analogRead(green_ledpin));
digitalWrite(green_ledpin, LOW);
delay(30); */
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
Serial.print(“G”);
Serial.println(analogRead(green_ledpin));
analogWrite(green_ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
Serial.print(“G”);
Serial.println(analogRead(green_ledpin));
analogWrite(green_ledpin, value);
delay(30);
}
}

void red_natural_light()
{

/*Serial.print(“R”);
Serial.println(analogRead(red_ledpin));
digitalWrite(red_ledpin, HIGH); // sets the value (range from 0 to 255)
delay(60); // waits for 30 milli seconds to see the dimming effect

Serial.print(“R”);
Serial.println(analogRead(red_ledpin));
digitalWrite(red_ledpin, LOW);
delay(60); */

for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
Serial.print(“R”);
Serial.println(analogRead(red_ledpin));
analogWrite(red_ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
Serial.print(“R”);
Serial.println(analogRead(red_ledpin));
analogWrite(red_ledpin, value);
delay(30);
}
}

void blue_natural_light()
{

/Serial.print(“B”);
Serial.println(analogRead(blue_ledpin));
digitalWrite(blue_ledpin, HIGH); // sets the value (range from 0 to 255)
delay(45); // waits for 30 milli seconds to see the dimming effect
Serial.print(“B”);
Serial.println(analogRead(blue_ledpin));
digitalWrite(blue_ledpin, LOW);
delay(45);
/

for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
Serial.print(“B”);
Serial.println(analogRead(blue_ledpin));
analogWrite(blue_ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
for(value = 255; value >=0; value-=5) // fade out (from max to min)
{
Serial.print(“B”);
Serial.println(analogRead(blue_ledpin));
analogWrite(blue_ledpin, value);
delay(30);
}

}

and here is the processing program where I get "StringIndexOutOfBoundsException: String index out of range: 0"

int targetColor;
import processing.serial.*;
String buff = “”;
int rval = 0 , gval = 0 , bval = 0;
int NEWLINE =10;
color c1;

Serial port;

void setup() {
size(640,480);
println(“Available serial ports:”);
println(Serial.list());
port = new Serial(this, Serial.list()[0],9600);
}

void draw() {
while(port.available()>0) {
serialEvent(port.read());
}
background(c1);
}

void serialEvent(int serial)
{
// If the variable “serial” is not equal to the value for
// a new line, add the value to the variable “buff”. If the
// value “serial” is equal to the value for a new line,
// save the value of the buffer into the variable “val”.
if(serial != NEWLINE) {
buff += char(serial);
} else {
// The first character tells us which color this value is for
char c = buff.charAt(0); // here is the place where it shows an error
// Remove it from the string
buff = buff.substring(1);
// Discard the carriage return at the end of the buffer
buff = buff.substring(0, buff.length()-1);
// Parse the String into an integer
if (c == ‘R’)
rval = Integer.parseInt(buff);
else if (c == ‘G’)
gval = Integer.parseInt(buff);
else if (c == ‘B’)
bval = Integer.parseInt(buff);
// Clear the value of “buff”
c1=color(rval,gval,bval);
buff = “”;
port.clear();
}
}

/void mouseMoved() {
fill(rval,gval,bval);
noStroke();
smooth();
ellipse(mouseX,mouseY,5,5);
}
/

any thoughts friends?

Thank you,
Dhruv

Dhruv,

I'm no Java expert, but I think in the case where serial == NEWLINE, you are trying to extract a character ("char c = buff.charAt(0);") from a buffer that may be empty (""). For example, if the first character that arrives is a NEWLINE, or if you somehow get two NEWLINEs in a row, you will get this error.

Mikal

Hey Mikal,

Condition for NEWLINE is already there. How should I give condition for two NEWLINES? nested if?

Thank you, Dhruv