We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › StringOutOfBoundsException
Page Index Toggle Pages: 1
StringOutOfBoundsException (Read 820 times)
StringOutOfBoundsException
Feb 25th, 2009, 7:48pm
 
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()
{

green_natural_light();
delay(1000);
blue_natural_light();
delay(1000);
red_natural_light();
}

void green_natural_light()
{

  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()
{

  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()
{

 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(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();
}
}


any thoughts friends?

Thank you,
Dhruv
Re: StringOutOfBoundsException
Reply #1 - Feb 25th, 2009, 10:15pm
 
i know nothing about arduino so this may all be wrong but...

what happens if the first character you get is a newline?

it won't do the buf += char(serial) block but will jump to the buff.charAt(0) bit. only there's nothing in the buf because you haven't added anything to it yet.

i would, at the very least, check the buffer size before reading from it. and print all the characters you receive as the first line in serialEvent(); (and put brackets around them so you can see the whitespace)
Page Index Toggle Pages: 1