Simulation error

hello! i have been working on a simple simulation code, but i cant get it to work. when i try to use this code to set the location[0] or location[1], nothing happens:

while(Serial.available() > 0) {
serialRead = Serial.read();
while (location[0] == 0 && serialRead == 'x') {
Serial.println("locationx =");
location[0] = Serial.read();
Serial.println(location[0]);
}

while (location[1] == 0 && location[0] != 0 && serialRead == 'y') {
Serial.println("locationy =");
location[1] = Serial.read();
Serial.println(location[1]);
}
}

if (location[1] != 0 && location[0] != 0){
on = true;
}

any help would be nice, Thank you!

Why?

serialRead = Serial.read();

Did you run out of variable names?

You should post your entire sketch.

int time = 0;
int location[] = {0, 0};
int currentLocation[] = {0, 0};
boolean on = false;
int distx;
int disty;
float obsx;
float obsy;
int Speed;
char serialRead;


void setup() {
  Serial.begin(115200);
}

void loop() {
  
  getData();
  
  if(on) {
    if (location[0] != 0 || location[1] != 0) {
      Move();
    }
  }
    
  while(on) {
    time++;
    delay(10);
  }
  
  //display
  while(on) {
  Serial.print("(");
  Serial.print(currentLocation[0]);
  Serial.print(", ");
  Serial.print(currentLocation[1]);
  Serial.print(") - ");
  Serial.print(time);
  Serial.println("");
  }

}
void getData() {
  
  while(Serial.available() > 0) {
    serialRead = Serial.read();
    while (location[0] == 0 && serialRead == 'x') {
      Serial.println("location x =");
      location[0] = Serial.read();
      Serial.println(location[0]);
    }
    
    while (location[1] == 0 && location[0] != 0 && serialRead == 'y') {
      Serial.println("location y =");
      location[1] = Serial.read();
      Serial.println(location[1]);
      }
    }
    
    if (location[1] != 0 && location[0] != 0){
      on = true;
    }
        
    distx = location[0];
    disty = location[1];
 
}

void Move() {
  Speed = 1; //yards per second
    
  while(distx > 0) {
      distx -= 1;
      currentLocation[0] += 1;
      delay(Speed * 1000);
    }
  
  
  
    while(disty > 0) {
      disty -= 1;
      currentLocation[1] += 1;
      delay(Speed * 1000);
    }
  
}

that is my entire sketch

while (location[0] == 0 && serialRead == 'x') {
      Serial.println("locationx =");
      location[0] = Serial.read();
      Serial.println(location[0]);
    }

It makes no sense to have serialRead == 'x' as part of the while statement. It might make sense to have an if statement containing the while statement.

But, not really. It makes no sense to keep reading serial data ad storing every thing in location[0]. location[0] can only hold one byte.

You need to read and store whatever serial data has arrived, until the end of packet marker arrives. Then, parse the data. Not sending an end of packet marker? Start sending one or forget ever writing code that works.

jarhead42:
hello! i have been working on a simple simulation code, but i cant get it to work.

It would be a great help if you were to explain what the code is supposed to do.

...R

while(Serial.available() > 0) {
    serialRead = Serial.read();
    while (location[0] == 0 && serialRead == 'x') {
      Serial.println("locationx =");
      location[0] = Serial.read();

So, let's say the first character in the buffer is an 'x'.
Let's also say that the output buffer is empty, so the Serial.println takes almost no time at all.
Then you read another character.
Oops.