# Multiple clarifications needed

I’m an absolute beginner to coding and arduino. By “beginner” I mean I’ve been doing this for the last few hours and that’s it. My code here is what I thought would be an easy starter that I could build on to as time goes on and I learn more. I want it to give as much mathematical information around a given coordinate, in the future of course. For now, I’ve been struggling with just reading what I put into the serial monitor.
Here are my questions-

1. Is there a more efficient way to read what I give in the serial input?
2. In this case, what’s the difference between Serial.read and Serial.parseInt? When I use serial read x always comes out as 50 and y always comes as 10.
3. Why does it wait for my input for x, but not y.
4. Why is there a line skipped in the output?
5. For that Serial.println for y, when I change it to just serial.print I get an exit 1 error.
6. Is the rest of the code functional?
My code and its output are attached.
Thank you in advance!

void setup() {
Serial.begin(9600);

}

void loop() {
int x,y;
Serial.println(“Enter X Value”);
while(Serial.available() == 0){};
if(Serial.available() >= 0){
x = Serial.parseInt();
Serial.print(“Your X value is “);
Serial.print(x);
}
Serial.println(”\nEnter Y Value”);
while (Serial.available() == 0){};
if(Serial.available() >= 0){
y = Serial.parseInt();
Serial.println("\nYour Y value is “);
Serial.print(y);
}
if (y != 0 && x != 0){
if (y > 0 && x > 0){
Serial.println(”\nYour coordinate is in quadrant 1");
}
if (y > 0 && x < 0){
Serial.println("\nYour coordinate is in quadrant 2");
}
if (y < 0 && x < 0){
Serial.println("\nYour coordinate is in quadrant 3");
}
if (y < 0 && x > 0){
Serial.println("\nYour coordinate is in quadrant 4");
}}
else {
Serial.println("\nYour coordinate is on an axis");
}

}

Images from Original Post so we don't have to download them. See this Simple Image Posting Guide

...R

Please don't post pictures of text. Just copy and paste the text as then it can be copied to a text editor to examine it.

Also, please post a complete program in one piece.

To answer some of your questions
{1} Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

{2} Serial.read() takes a single byte from the Serial Input Buffer. Serial.parseInt() waits until it gets what it thinks is the text for a valid number. If none arrives it times-out after some period that you can choose. Waiting for data to arrive can be a waste of Arduino resources as it can do a lot in the time interval between two bytes arriving.

{3) Without being able to view your code in my text editor I don't have a specific answer. In general however, program code for user input can be complicated - and is often the longest and most complicated part of a program. There is a simple user-input example in Planning and Implementing a Program

...R

Simple serial echo test code you can try with the serial monitor.

``````//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
Serial.begin(9600);
Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

while (Serial.available()) {
char c = Serial.read();  //gets one byte from serial buffer
readString += c; //makes the String readString
delay(3);  //slow looping to allow buffer to fill with next character
}

if (readString.length() > 0) {
Serial.println(readString);  //so you can see the captured String
readString = "";
}
}
``````

Sorry, my computer wasn't letting me copy and paste earlier idk why. I'll change the original post. I looked through the link you sent. I still don't understand why it waits for the x but not the y.

"Sorry, my computer wasn't letting me copy and paste earlier idk why. I'll change the original post. I looked through the link you sent. I still don't understand why it waits for the x but not the y."

A free running loop can read the serial buffer much faster than the bytes can arrive in the buffer to be read. You need to capture the individual bytes and from them back into the string of bytes sent.

When posting code please use the code button </>

so your code looks like this

``````void setup() {
Serial.begin(9600);

}

void loop() {
int x,y;
Serial.println("Enter X Value");
while(Serial.available() == 0){};
if(Serial.available() >= 0){
x = Serial.parseInt();
Serial.print("Your X value is ");
Serial.print(x);
}
Serial.println("\nEnter Y Value");
while (Serial.available() == 0){};
if(Serial.available() >= 0){
y = Serial.parseInt();
Serial.println("\nYour Y value is ");
Serial.print(y);
}
if (y != 0 && x != 0){
if (y > 0 && x > 0){
Serial.println("\nYour coordinate is in quadrant 1");
}
if (y > 0 && x < 0){
Serial.println("\nYour coordinate is in quadrant 2");
}
if (y < 0 && x < 0){
Serial.println("\nYour coordinate is in quadrant 3");
}
if (y < 0 && x > 0){
Serial.println("\nYour coordinate is in quadrant 4");
}}
else {
Serial.println("\nYour coordinate is on an axis");
}

}
``````

and is easy to copy to a text editor See How to use the Forum

...R

Fox_089:
I still don't understand why it waits for the x but not the y.

When it is looking for the X value it starts with the Serial Input Buffer empty. But when it starts looking for the Y there is probably a byte left in the buffer from the X value - perhaps a linefeed or carriage-return character

Try emptying the buffer before looking for the Y value with this code

``````while (Serial.available >0) {
byte dumpByte = Serial.read();
}
``````

...R

Thank you so much. I didn't realize the buffer needed to be cleared. You all were so helpful!