Pages: [1]   Go Down
Author Topic: Processing to Arduino problem  (Read 1529 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've simplified this down to it's most basic form so that the error might be easier to fix.

So all this code should do is when I hit RUN on the Processing sketch, and LED turns on.

The problem I keep running into is that every time I hit RUN on the Processing sketch I get this error message: 'ArrayIndexOutOfBoundsException 2'
and also this in the window:
Quote
Display 0 does not exist, using the default display instead.
WARNING:  RXTX Version mismatch
   Jar version = RXTX-2.2pre1
   native lib Version = RXTX-2.2pre2
  • "COM3"
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 2
   at ptoa02.setup(ptoa02.java:28)
   at processing.core.PApplet.handleDraw(Unknown Source)
   at processing.core.PApplet.run(Unknown Source)
   at java.lang.Thread.run(Thread.java:662)

I read somewhere about this being fixed by creating a 'symlink', but that was on Linux. Would this apply here?
Topic: http://forum.processing.org/topic/ubuntu-10-04-processing-serial-rxtx-fhs-lock-error-creating-lock-file

I am running Windows 8: Processing 1.5.1 and Arduino 1.0.4. with an Arduino UNO.

Any help would be much appreciated.

Arduino Code:
Code:
int ledPin = 13;
int val = 0;

void setup(){
 
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  val = Serial.read();
 
  if (val == 'A'){
    digitalWrite(ledPin, HIGH);
  }
  if (val == 'a'){
    digitalWrite(ledPin, LOW);
  }
}

Processing Code:
Code:
import processing.serial.*;

Serial myPort;

println(Serial.list());

myPort = new Serial(this, Serial.list()[2], 9600);

myPort.write(65);

Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you actually have 3 serial ports ?  Do you really want the third one ?  I think that is where your problem resides.

Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  String[] portNames = Serial.list();
  int len = portNames.length ;
  println("portNames has "+len+" elements");
 
  for (int i=0 ; i<len ; i++ )
  {
    println(portNames[i]);
  }
 
  String portName4 = new String( portNames[1]) ;  // usual needs to be portNames[1] for arduino
 
  myPort = new Serial(this, portName4, 115200 );
  myPort.clear() ;

This is what my code to open a serial port looks like.   It took a long time to get it to work,   quite a while
ago and I don't remember all the issues I had.

The Serial.list() function returns an array of strings.   You are calling that function twice in your code,
which is unwise for two reasons.   Firstly,  it's wasting time trying to get the information twice,  from the
operating system of your PC.   Secondly,   the second time you use it,   in the constructor for the new Serial object,
you are trying to attach an array index to a function call,  which may or may not be syntactically legal but
is asking for trouble for number of reasons.

You are better off in my opinion calling Serial.list() once,  assigning the result of the function call to an actual
array in your program,  and then using one of those array elements as the parameter to the new Serial() constructor.

Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looking at your actual output,   you have the RXTX b/s  which I don't know what it is about.  I get that also.

And then it says "COM3"   which I suspect is the output of your statement   println( Serial.list() )

Now I don't know what the println() function will do with an array of String,  so I am not sure if it would
actually print all of them.   But it looks likely,  there is actually only one element in this array of strings,
that is,  you have one serial port,  which is COM3.

You then go and try to access  the third element (  [2]  )  of an array of strings,  where that array
probably only has one element in it.   
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The error told you what the problem is. Minus 1 from the array list. Serial.list[0] should be the one you're using.
« Last Edit: March 25, 2013, 12:14:15 am by mistergreen » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks to all of you. I had read online in one explanation that eventually lead to trying 'Serial.list()[2]', I had done the same code with but with Serial.list()[0] and it didn't work. I just tried it again now and it worked perfectly fine.

I'll just put it down to tech being tech.

Thanks again.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll just put it down to tech being tech.

No, put it down to you using arrays incorrectly and happening to get lucky sometimes.

Before using an element of the array you should ensure that the element is within the bounds of the array and that it contains the item you want. Here you have no reason to assume that there are at least three serial ports or that the third one corresponds to the Arduino. If you are actually trying to assume that the Arduino will be on the serial port named "COM3" then IMO you might just as well hard-code that value rather than assume that the right name is at index [2] in the list of port names.

(Better would be to provide some way for the user to see and select from the list of available ports.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'll just put it down to tech being tech.

You need to develop an understanding of what you are doing.   

Different computers have different numbers of serial connections ( or , these days,  usually a USB connection pretending to be a serial connection ).   With both the Arduino and Processing,  the serial connections vary all the time on my computer and it is one of the most annoying aspects of using the Arduino for me.

In your case,   the error message you got was actually pretty clear to me,   and I am as far as it is possible to be from being a Processing expert, having only used it once,  and disliking it.
Logged

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I like processing. It's pretty flexible. If you know JAVA, you can extend its power like if you know C++ for the Arduino IDE.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, I do understand the theory and I had actually originally had it written as it is now (and working), but it wasn't working at the time. So I followed some other stuff online and the only thing I changed was the 2 in the array. When I changed it back it worked. That's what I meant by tech being tech, I didn't mean to sound like I had no appreciation for what was happening so long as it worked.
Logged

Pages: [1]   Go Up
Jump to: