X,Y coordinate cartesian plane code

My code is printing some pretty wild results and im not sure what in my code is causing this.

An example for what my serial monitor reads is

"
Enter a value for x
Your x coordinate is 2
Enter a value for y
Your y coordinate is 3
2 is in quadrant 1
2 is in quadrant 3
2 is on the x-axis "

Why all the different outputs?

Thanks in advance

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //setup serial communication

}

void loop() {
  // put your main code here, to run repeatedly:
  int x, y;

  Serial.println("\nEnter a value for x");
  while (Serial.available() <= 0);
  x = Serial.parseInt();
  Serial.print("Your x coordinate is ");
  Serial.print(x);

  Serial.println("\nEnter a value for y");
  while (Serial.available() <= 0);
  y = Serial.parseInt();
  Serial.print("Your y coordinate is ");
  Serial.println(y);
 

   if (x != 0 && y != 0)
  {
    if (x > 0 && y > 0)
    {
    Serial.print(x,y);
    Serial.println(" is in quadrant 1");
  }
  else if (x > 0 && y < 0)
  {
    Serial.print(x,y);
    Serial.println(" is in quadrant 4");
  }
  else if (x < 0 && y > 0)
  {
    Serial.print(x,y);
    Serial.println(" is in quadrant 2");
  }
  else (x < 0 && y < 0);
  {
    Serial.print(x,y);
    Serial.println(" is in quadrant 3");
  }
  }
  
  if (x == 0 && y != 0)
  {
    Serial.print(x,y);
    Serial.println(" is on the y axis");
  }
  else if (x != 0 && y == 0);
  {
    Serial.print(x,y);
    Serial.println(" is on the x axis");
  }

}

The old "extraneous semicolon" trick :slight_smile:

  else (x < 0 && y < 0);

Pete

while (Serial.available() <= 0);

This waits for just one character to be available. Is that what you intended?

No, i need to get an ordered pair from the user ,x and y value. do I need to change that command? what would that look like?

The lineSerial.print(x,y);does not print x and y, but x in the numerical system y.

Google "arduino parse serial" for lots of examples and discussion.

and here:

  else if (x != 0 && y == 0);

Pete

It's worse than that

Arghhh. I didn't see that. Maybe it's missing the 'if'.

Pete

Delta_G:

else (x < 0 && y < 0);

It's worse than that. An else shouldn't have a condition at all.

So the 'condition' gets parsed as an expression.

What is happening here is that he put the (unnecessary and syntactically incorrect) condition in, got a compile error, and found that adding a semicolon made the compile error go away.

I don't think the condition is unnecessary. He's missing "if" in front of it so that it is "else if(condition)".

Pete