Dhruv
YaBB Newbies
Offline
Posts: 1
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