Help needed please.

Oooops. seem to have posted in the wrong forum. Sorry about that.

Hello, can anyone help with this? :frowning:

I'm running the following processing code to get the 3 axis values from an accelerometer. The Arduino is sending the values properly.

import processing.serial.*;

Serial sp; //Class declaration
byte[] buff; //Declares buff as array of bytes
int lf = 10; // ASCII linefeed
int[] xval = new int[900];
int[] yval = new int[900];
int[] zval = new int[900];
int arraypointer = 0;

void setup() { //Setup is executed once
sp = new Serial(this, "COM4", 9600); // Instantiates sp as Class 'serial'
}

void draw(){ //continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called
int i;
buff = new byte[128];
background(0);
int bytesread = sp.readBytesUntil(lf, buff);
// println ("Bytes read = " + bytesread); //Debugging only
String mystr = (new String(buff, 0, bytesread)).trim(); // trim removes lf char from end of string
println("Mystr = " + mystr); //Debugging only
String[] vals = split(mystr, ' ');
println ("Vals = " + vals); //Debugging only
xval[arraypointer] = int (vals[0]); //Store in arrays as ints
yval[arraypointer] = int (vals[1]);
zval[arraypointer] = int (vals[2]);
println("v0 = " + vals[0] + " int = " + xval[arraypointer] + " Array index = " + arraypointer); //Debugging only
println("v1 = " + vals[1] + " int = " + yval[arraypointer]); //Debugging only
println("v2 = " + vals[2] + " int = " + zval[arraypointer]); // + "Arrayindex = " + arraypointer); //Debugging only
if (arraypointer >= 800){
arraypointer = 0;
}
else{
arraypointer++;
}
}

I get the following error at the line
yval[arraypointer] = int (vals[1]);
The error condition is with the vals[1] array. The previous line (vals[0]) does not error.

Stable Library

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Mystr =
Vals = [Ljava.lang.String;@1035079 (I have no idea why this is returned)

processing.app.debug.RunnerException: ArrayIndexOutOfBoundsException: 1
at processing.app.Sketch.placeException(Unknown Source)
at processing.app.debug.Runner.findException(Unknown Source)
at processing.app.debug.Runner.reportException(Unknown Source)
at processing.app.debug.Runner.exception(Unknown Source)
at processing.app.debug.EventThread.exceptionEvent(Unknown Source)
at processing.app.debug.EventThread.handleEvent(Unknown Source)
at processing.app.debug.EventThread.run(Unknown Source)
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 1
at model_manip.draw(model_manip.java:48)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)

Many thanks :slight_smile:

This command:

String[] vals = split(mystr, ' ');

is extracting tokens from mystr, where the token separator is the space character, and putting the values into an array.

Before you use the values in the array, you really should make sure that the index (0, 1, and 2) is less than the length of the array. If there really were 2 spaces in mystr, the array length would be 3, so 0, 1, and 2 would be valid indexes.

Since you are getting an exception thrown when trying to use 1 as an index, that tells me that the array length is 1, and that the only valid index is 0.

So,

The Arduino is sending the values properly.

is clearly wrong.

Show us the Arduino code, and the debug output of the Processing sketch.

Hello Paul, and many thanks for the quick response.

The Arduino is running the example sketch adxl345_to_Arduino_to_Serial.pde,

and I observe the following strings returned in PuTTY connected to COM4

21 -19 -236
25 -20 -234
20 -20 -229
22 -22 -232
19 -26 -227
19 -20 -232

There are indeed two spaces in the string.

The debug output is at the bottom of my post. e.g processing.app.debug.RunnerException: ArrayIndexOutOfBoundsException: 1

The array index returned by 'split' seems to be [Ljava.lang.String;@1035079

I see, now, that you printed mystr. How many spaces are in the value you printed?

Paul,

I think you have put your finger on the problem.
It looks as if there is a long delay before the Arduino sends valid data. Until that happens, it returns empty strings, so the split is only getting one value (lf) to deal with.
As ever, discussing a problem brings clarity.
Many thanks for your time and expertise.