Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 747

Can you help me finding my fault?

$
0
0

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);
  }

Viewing all articles
Browse latest Browse all 747

Latest Images

Trending Articles



Latest Images