Array Index Out of Bound

edited September 2014 in Questions about Code

Guys, i have the following codes:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
float val1, val2, val3, val4;      // Data received from the serial port


PImage img1_1, img1_2, img1_3;           // 
PImage img2_1, img2_2, img2_3, img2_4;   //
PImage img3_1, img3_2;           //

float YAW, PITCH, ROLL;
float index;


void setup() {

  String portName = Serial.list()[1];            // select Arduino serial port 
  myPort = new Serial(this, portName, 9600);


  background(0);
  frameRate(24);
  size(900, 600);

 YAW=0;
 PITCH=0;
 ROLL=0;
 index=0;


  img1_1 = loadImage("1-1.png");  // 
  img1_2 = loadImage("1-2.png");  // 
  img1_3 = loadImage("1-3.png");  //

  img2_1 = loadImage("2-1.png");  // 
  img2_2 = loadImage("2-2.png");  // 
  img2_3 = loadImage("2-3.png");  //
  img2_4 = loadImage("2-4.png");  // 

  img3_1 = loadImage("3-1.png");  // 
  img3_2 = loadImage("3-2.png");  // 





}

void draw() {
   background(0);
   imageMode(CENTER);
  // index=index+0.1;



  if ( myPort.available() > 0) {  // If data in the Arduino's serial port is available,
    val1 = myPort.read()-48;         // read first byte and store it 
    val2 = myPort.read()-48;         // read second byte and store it
    val3 = myPort.read()-48;         // read third byte and store it
  //  val4 = myPort.read();         // read fourth byte and store it
    //use received data     
    YAW = val1;
    PITCH = val2;
    ROLL = val3;
    }

// just animation  
  // YAW=YAW+cos(index/2)/30;     //
  // ROLL=ROLL-cos(index)/30;       //
  // PITCH=PITCH+cos(index*0.5)*2;    //
//





 translate(270, 170);   
 image(img1_1, 0, 0);

 rotate(YAW);
 image(img1_2, 0, 0); 

 rotate(-YAW);
 image(img1_3, 0, 0);

 translate(350, 0);  
 rotate(ROLL);
 image(img2_1, 0, 0);
 rotate(-ROLL);
 image(img2_3, 0, PITCH);
 image(img2_4, 0, 0);
 image(img2_2, 0, 0);

 translate(-500, 300);   
 fill(255, 255, 255); 
 rect(0, 0, 120, 33, 2);
 rect(200, 0, 120, 33, 2);
 rect(400, 0, 120, 33, 2);

 imageMode(CORNER);
 image(img3_1, 600, 0);
 image(img3_2, 650, 0);

 textSize(32);
 fill(0, 102, 153);
 text("YAW", 22, -30);
 text("PITCH", 213, -30);
 text("ROLL", 418, -30); 
 text("A/P", 612, -30); 

 textSize(25);
 fill(0, 0, 0);
 text(YAW, 11, 25);
 text(PITCH, 201, 25);
 text(ROLL, 401, 25); 



}

when i run the following codes, this line got highlighted "String portName = Serial.list()[1];" with error of "ArrayIndexOutOfBoundsException: 1" when i change the number in the bracket to 2,3,4,5,6... the error still occur. the program can run when i change the number to 0, however, my program runs in a random mode and does not display real data from my hardware. I have a 10dof and arduino mega 2560. Does this code only does animation or does it lack of some calculation? Help needed. Thanks in advance

Answers

  • Highlight your code and then click on the "C" button above the text area to format it

  • Im using windows. I couldnt find the "C", however i did try "auto format" under the editing function. The error is still present when i run the codes

  • edited September 2014

    Re-phrasing @asimes: Select your code text block and hit CTRL+K! :ar!

  • Serial.list() returns a list of serial devices. There can be none (eg. on my computer), only 1 or several.

    The error you get means you have only one (at best) device. This is confirmed by the fact you have no error (of this kind) with the index 0.

    BTW, the C button is in this forum, not in the PDE...

Sign In or Register to comment.