Pages: [1]   Go Down
 Author Topic: PING to processing  (Read 1309 times) 0 Members and 1 Guest are viewing this topic.
0
Offline
Newbie
Karma: 0
Posts: 37
 « on: April 12, 2009, 12:48:30 am » Bigger Smaller Reset

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:

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

void setup()
{
Serial.begin(9600);
}

void loop()
{
long duration, inches;

for (int i=0; i<3; i++)
{
pinMode(pingPins[i], OUTPUT);
digitalWrite(pingPins[i], LOW);
delayMicroseconds(2);
digitalWrite(pingPins[i], HIGH);
delayMicroseconds(5);
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);
Serial.print(pingPins[i]);
Serial.println(inches);

}

delay(100);
}

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: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
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:
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.

 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6252
Have fun!
 « Reply #1 on: April 12, 2009, 01:40:59 am » Bigger Smaller Reset

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.
 Logged

0
Offline
Newbie
Karma: 0
Posts: 37
 « Reply #2 on: April 12, 2009, 01:48:33 am » Bigger Smaller Reset

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

London
Offline
Tesla Member
Karma: 10
Posts: 6252
Have fun!
 « Reply #3 on: April 12, 2009, 01:51:20 am » Bigger Smaller Reset

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

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

 « Last Edit: April 12, 2009, 01:52:22 am by mem » Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6252
Have fun!
 « Reply #4 on: April 12, 2009, 02:04:37 am » Bigger Smaller Reset

I would think that you if put the code above in the code in your first post it will do what you want.
 « Last Edit: April 12, 2009, 02:11:03 am by mem » Logged

0
Offline
Newbie
Karma: 0
Posts: 37
 « Reply #5 on: April 12, 2009, 02:05:10 am » Bigger Smaller Reset

thanks!
 Logged

0
Offline
Newbie
Karma: 0
Posts: 37
 « Reply #6 on: April 12, 2009, 02:55:02 am » Bigger Smaller Reset

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:

G

1

R2
4

B

14

6

and then i get out of bounds exceptions in processing
 Logged

0
Offline
Newbie
Karma: 0
Posts: 37
 « Reply #7 on: April 12, 2009, 02:57:10 am » Bigger Smaller Reset

baud rates match, btw
 Logged

London
Offline
Tesla Member
Karma: 10
Posts: 6252
Have fun!
 « Reply #8 on: April 12, 2009, 03:02:26 am » Bigger Smaller Reset

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.
 Logged

0
Offline
Newbie
Karma: 0
Posts: 37
 « Reply #9 on: April 12, 2009, 03:09:30 am » Bigger Smaller Reset

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.
 Logged

 Pages: [1]   Go Up