How to have two seperate inputs from the serial port

I'm new to programming so try to bear with me. I'm able to input one set of numbers from the serial mode using Serial.read().

My question is how to input another set of numbers into a different function?

For example, the question is "How tall is something?". I want to input a set of values for that question in the void loop() function.

Then I have another question that is "How long is something?". Then I want to input another set of values for that question in another function so it can be called upon later.

Do I use serial.read for both of them, or what do I do?

HELP PLEASE

Do I use serial.read for both of them, or what do I do?

Yes. Suppose that you write a function, called getAnswer(). You'd have another function call askQuestion().

Once you call askQuestion(), you'd set a state that defined where the value from getAnswer() goes. When getAnswer() reports that is has an answer (because the function blocks waiting for the answer or because the function sets a flag indicating that the answer is complete), the state variable defines where the answer goes (height = getAnswer(); or length = getAnswer();).

So I will only use the serial.read() in the getAnswer() function? Not in both, is that what you're saying?

Also,
would it be better to make each of those out of the void loop or try to keep one of the functions occurring in the void loop?

So I will only use the serial.read() in the getAnswer() function? Not in both, is that what you're saying?

Yes, you would use Serial.available() and Serial.read() in getAnswer(). There is no need to read anything in askQuestion().

would it be better to make each of those out of the void loop or try to keep one of the functions occurring in the void loop?

There is no void loop. There is a loop() function that has a return type of void. No one talks of the "int analogRead". No one should talk of the "void loop".

Creating functions is something every developer should be able to do. This is a good opportunity for you to learn how.

okay cool,

I really appreciate it.

Have you ever used the serial.event() command?

zwood14:
okay cool,

I really appreciate it.

Have you ever used the serial.event() command?

I think you mean serialEvent().

Don't use it. It is some sort of interrupt function with no documentation. More likely to cause problems than solve them.

...R

It is some sort of interrupt function with no documentation.

No, it isn't. There are interrupts involved in getting serial data into the buffer, but the serialEvent function, if defined, is called at the end of each pass through loop(), if there is serial data to be read.

It does not make dealing with serial data any easier. After all, you could add:

if(Serial.available() > 0)
   handleSerialData();

to the end of loop(), and then define a function called handleSerialData(), and accomplish exactly the same thing.

I'm having problems getting any other functions to work that is not inside the initial loop() function. Can anyone help with this?

Probably. You'll need to post your code first though.

PaulS:

It is some sort of interrupt function with no documentation.

No, it isn't. There are interrupts involved in getting serial data into the buffer, but the serialEvent function, if defined, is called at the end of each pass through loop(), if there is serial data to be read.

It does not make dealing with serial data any easier. After all, you could add:

if(Serial.available() > 0)

handleSerialData();



to the end of loop(), and then define a function called handleSerialData(), and accomplish exactly the same thing.

At the very least you have supported my assertion that there is no documentation.

The Arduino Reference just says

Called when data is available. Use Serial.read() to capture this data.

and that sounds very much like an interrupt process to me.

If your version of how it works is correct (and I'm very willing to learn) where would I find the evidence? I was studying HardwareSerial.cpp earlier today and it certainly seemed to be an interrupting routine.

My worry is that serialEvent() gets called a second or third time before the first run-through completes.

...R

At the very least you have supported my assertion that there is no documentation.

Sure there is. You quoted from it.

and that sounds very much like an interrupt process to me.

Have you looked at the base code that the IDE adds to your sketch? It is a simple test using SerialN.available() that determines whether, or not, to call the serialEvent() method. There is nothing interrupt-driven about it. You could, as I mentioned, do exactly the same thing in your loop() function.

If your version of how it works is correct (and I'm very willing to learn) where would I find the evidence?

Enable verbose mode during compiling. Look in the build time directory, for the cpp file created from your ino file.

My worry is that serialEvent() gets called a second or third time before the first run-through completes.

Nope. Can't happen.