Can you help me finding my fault? (readStringUntil ArrayIndexOutOfBoundsException)

edited February 2018 in Arduino

Hello strangers, I tried to programm a ball running on my screen in the direction of my Joystick (connected to the Arduino). I wrote the full programm but now I got an error that says: "ArrayIndexOutOfBoundsException: 1". The confusing about this error is, that like every 5 tries to start the programm are a success and I can play my little game as long as I want without any issues. I have no idea what could be wrong so maybe someone of you can help me.

The error has to be in line 28 of my Processing Code.

Arduino Code:

const int xPin = 0;
const int yPin = 1;

int xVal;
int yVal;

String data;

void setup() {
  Serial.begin(9600);
  pinMode(xPin,INPUT);
  pinMode(yPin,INPUT);
}

void loop() {
  xVal = analogRead(xPin);
  yVal = analogRead(yPin);
  data = dataNormalize(xVal,yVal);
  Serial.println(data);
  delay(50);
}

String dataNormalize(int x,int y) {
  String xString = String(x);
  String yString = String(y);

  String data = xString+","+yString;
  return data;
}

Processing Code:

import processing.serial.*;

Serial mySerial;

String myString;
int nl = 10;
float xVal;
float yVal;

int x=500;
int y=50;

void setup() {
  frameRate(20);
  size (1000,1000);
  String myPort = Serial.list() [0];
  mySerial = new Serial (this, myPort, 9600);
}

void draw() {
  background(#5EC67C);

  while (mySerial.available() > 0) {
    myString = mySerial.readStringUntil(nl);
    if (myString != null) {
      String [] data = splitTokens(myString,",");
      xVal = (float(data [0])-513)/50;
      yVal = (float(data [1])-509)/50;   
    }
  }
  stroke(0);
  fill(255,0,0);
  ellipse(x,y,20,20);
  x += xVal;
  y += yVal;
  println("X-Position: "+x+" Y-Position: "+y+"     xVal: "+xVal+" yVal: "+yVal);
  }

Answers

  • Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Kf

  • Thank you, hope that's ok now :)

  • Check data.length. Ensure it has two items before you try to access its elements.

    Kf

  • something about ARDUINO...I don't know if you are using UNO, MEGA or what but usually PIN 1 and 0 are the same of the Serial that connect ARDUINO ad PC by USB cable...if you have to let ARDUINO connected with the PC change the PIN...other thing PIN 0 and 1 are not analog but digital...A0 and A1, that, in ARDUINO UNO you can also call 14 and 15, are analog INPUT (as default)

  • I checked data.length and there are two items. I also changed the Pins to A2 and A3 but nothing worked.

    The curious thing about the error is, that it happens not all the time only like 4 of 5 times and at the fifth time i can control everything as long as I want.

  • Try printing out the data string. That way, when the code fails you can see the input and work out why it failed.

    (Do they not teach people how to debug anymore?)

  • In line 36 of my Processing code I already print out the data string but couldn't find any issue or discontinuities when the error appears.

  • Ok now I'm even more confused... I commented line 36 out, gave it a few tries without any issue at all. Then I placed line 36 in again and again no Problem at all... I'm not sure what happened because my Code is still the same. :))

  • edited February 2018

    line 36 of my Processing code I already print out the data

    But the code stops in line 28 if there's an error, so printing out the data on line 36 just guarantees you're printing out working data whereas you want the bad data.

  • no Problem at all

    You've said yourself that the problem is intermittent. Fix it properly!

Sign In or Register to comment.