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.
Page Index Toggle Pages: 1
PING to processing (Read 1129 times)
PING to processing
Apr 12th, 2009, 1:13am
 
ok, i'm using 3 ping sensors to control RGB in processing, mostly just to get it to work, so I know how to do it for a bigger project at the end of the summer.  the arduino code is working fine, but when i run the processing program, the serial data in the serial monitor in arduino goes bonkers, and i get string out of bounds exceptions in processing.  

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);
char RGB[] = {'R','G','B'};

Serial.print(RGB[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;
}


Processing code:

Code:
import processing.serial.*;

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

Serial port;

void setup()
{
size(200, 200);

// Print a list in case COM1 doesn't work out
println("Available serial ports:");
println(Serial.list());

//port = new Serial(this, "COM1", 9600);
// Uses the first available port
port = new Serial(this, Serial.list()[0], 9600);
}

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

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);
// 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"
buff = "";
}
}



thanks!
Page Index Toggle Pages: 1