i've been trying to read two bluetooth streams into one processing sketch and have been successful (only!) once. mostly i get the following error message. after which (sometimes) the sketch proceeds to open showing only one stream of bluetooth data.
gnu.io.PortInUseException: Unknown Application at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354) at processing.serial.Serial.<init>(Serial.java:136) at processing.serial.Serial.<init>(Serial.java:102) at TwoBluetooth.setup(TwoBluetooth.java:64) at processing.core.PApplet.handleDraw(PApplet.java:1402) at processing.core.PApplet.run(PApplet.java:1327) at java.lang.Thread.run(Thread.java:680) Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.<init>() at processing.serial.Serial.errorMessage(Serial.java:588) at processing.serial.Serial.<init>(Serial.java:148) at processing.serial.Serial.<init>(Serial.java:102) at TwoBluetooth.setup(TwoBluetooth.java:64) at processing.core.PApplet.handleDraw(PApplet.java:1402) at processing.core.PApplet.run(PApplet.java:1327) at java.lang.Thread.run(Thread.java:680)
my setup: i have two arduino mini pros with one sparkfun bluetooth mate mounted on each. each arduino is reading four analog input values and sending these over the serial port. the processing sketch determines which port it is receiving data from, reads the incoming values and visualizes them.
the code i have is pieced together from the "many serial ports" processing example and tom igoe's arduino graph code.
because i've been able to connect to both bluetooth modules and receive data, i think that the problem is with the initialization of the bluetooth ports and/or with timing. i know very little about how the bluetooth connection is established and what goes on while it is active. if anybody has experience and can detect an obvious error in my setup, though process or code, i would very much appreciate any help or advice i can get.
bellow is my arduino and processing code.
processing code:
import processing.serial.*;
Serial rightPort, leftPort; PFont myFont; // font for writing text to the window boolean fontInitialized = false; // whether the font's been initialized int xpos; int leftPS; float leftPSmap; float previousLeftValue; int leftX; // accelerometer int leftY; int leftZ; int rightPS; float rightPSmap; float previousRightValue; int rightX; int rightY; int rightZ; int windowWidth = 1200; int windowHeight = 800; int textBoxHeight = 60;
void setup() { size(windowWidth, windowHeight); //size of application window background(255); // set inital background smooth(); // turn on antialiasing
println(Serial.list()); // prints a list of the serial ports // run the application to read the serial list printout, here you will see what portname/number your bluetooth modules have
//////// LEFT //////// leftPort = new Serial(this, "/dev/tty.RN42-059C-SPP", 115200); ///// BLUETOOTH ///// //leftPort = new Serial(this, "/dev/tty.usbserial-A6007x5s", 9600); ///// FTDI ///// leftPort.clear(); leftPort.bufferUntil('\n'); // don't generate a serialEvent() until you get a newline (\n) byte
//////// RIGHT //////// rightPort = new Serial(this, "/dev/tty.RN42-0592-SPP", 115200); ///// BLUETOOTH ///// //rightPort = new Serial(this, "/dev/tty.usbserial-A700ewYh", 9600); ///// FTDI ///// rightPort.clear(); rightPort.bufferUntil('\n'); // don't generate a serialEvent() until you get a newline (\n) byte
myFont = loadFont("HelveticaNeue-18.vlw"); textFont(myFont, 18); // Set the font and its size (in units of pixels) fontInitialized = true; }
void draw() { strokeWeight(5); stroke(200,0,0); line(xpos, previousRightValue - textBoxHeight, xpos+1, rightPSmap - textBoxHeight); previousRightValue = rightPSmap; stroke(0,0,200); line(xpos, previousLeftValue - textBoxHeight, xpos+1, leftPSmap - textBoxHeight); previousLeftValue = leftPSmap; if (xpos >= width) { // if you've drawn to the edge of the window, start at the beginning again saveFrame(); // save a screen-shot of the application window xpos = 0; background(255); } else xpos+=1; // change the increment of x to control the speed of graphing incoming values if (fontInitialized) { fill(215); noStroke(); rect(0, windowHeight-textBoxHeight, windowWidth, windowHeight); fill(200,0,0); text("Impact: " + int(rightPS), windowWidth/2 + 10, windowHeight - 42); text(" X: " + rightX, 3*windowWidth/4 + 10, windowHeight - 42); text(" Y: " + rightY, 3*windowWidth/4 + 10, windowHeight - 22); text(" Z: " + rightZ, 3*windowWidth/4 + 10, windowHeight - 2); fill(0,0,200); text("Impact: " + int(leftPS), 10, windowHeight - 42); text(" X: " + leftX, windowWidth/4 + 10, windowHeight - 42); text(" Y: " + leftY, windowWidth/4 + 10, windowHeight - 22); text(" Z: " + leftZ, windowWidth/4 + 10, windowHeight - 2); } }
void serialEvent(Serial thisPort) { if (thisPort == rightPort) { String rightString = rightPort.readStringUntil('\n'); // get the ASCII string if (rightString != null) { // if it's not empty rightString = trim(rightString); // trim off any whitespace int rightValues[] = int(split(rightString, ",")); // convert to an array of ints rightPS = 1023 - rightValues[0]; rightPSmap = map(rightValues[0], 0,1023, 0, windowHeight-textBoxHeight); rightX = rightValues[1]; rightY = rightValues[2]; rightZ = rightValues[3]; println("RIGHT: PS: " + rightPS + " X: " + rightX + " Y: " + rightY + " Z: " + rightZ); } } if (thisPort == leftPort) { String leftString = leftPort.readStringUntil('\n'); // get the ASCII string if (leftString != null) { // if it's not empty leftString = trim(leftString); // trim off any whitespace int leftValues[] = int(split(leftString, ",")); // convert to an array of ints leftPS = 1023 - leftValues[0]; leftPSmap = map(leftValues[0], 0,1023, 0, windowHeight-textBoxHeight); leftX = leftValues[1]; leftY = leftValues[2]; leftZ = leftValues[3]; println("LEFT: PS: " + leftPS + " X: " + leftX + " Y: " + leftY + " Z: " + leftZ); } } }
arduino code:
#define numberOfSensors 4 int input [numberOfSensors];
void setup() { //digitalWrite(14, HIGH); // internal pull-up resistor for analog 0 - off, because we use a 4.7K Ohm external pull-up (values from 900-300) digitalWrite(15, HIGH); // internal pull-up resistor for analog 1 = accelerometer X (values from 230-390) digitalWrite(16, HIGH); // internal pull-up resistor for analog 2 = accelerometer Y digitalWrite(17, HIGH); // internal pull-up resistor for analog 3 = accelerometer Z pinMode(11, OUTPUT); // set pin 11 as output digitalWrite(11, HIGH); // write HIGH to pin 11 because it is connected to the + of the accelerometer //Serial.begin(9600); // baud rate (bits per second) for USB serial Serial.begin(115200); // baud rate (bits per second) for bluetooth }
void loop() { for (int count = 0; count < numberOfSensors; count++) { int sensorReading = analogRead(count); // read each sensor Serial.print(sensorReading, DEC); // print its value out as an ASCII numeric string if (count < numberOfSensors -1) Serial.print(","); // if this isn't the last sensor to read then print a comma after it } Serial.println(); // after all the sensors have been read print a newline and carriage return delay(10); // change the delay value (milliseconds) to control the rate of sampling }