Serial Monitor for Program Configuration data

First, thanks to all you guys that post to the forum regularly, as I have spent days reading that stuff, and it is so helpful.

I am just getting back into programming, so I am a little rusty and need some help with serial communications. I have got the bulk of my code working for the operation, but am having a devil of a time getting the user interface portion to function the way I want.

Objective:

Use the setup() section to ask the user questions (via serial monitor) and take those responses in as variables. I have successfully been able to enter data on the serial monitor, move it from the serial buffer to a character buffer and convert to my desired data and store. Where I am having trouble, is doing this for different variables, only once, and not continually looping.

Example: (as viewed from the monitor)

How many break points do you want?
6 (user entered 6 and pressed enter)
Enter break point time 0:
0 (user entered 0 and pressed enter)
Enter break point time 1:
115 (user entered 115 and pressed enter)
.
.
.
Enter break point time 5:
10000 (user entered 10000 and pressed enter)

Would you like to run the operation now? (Y/N)
Y (user entered Y and pressed enter)

**program now runs contents in void loop()

I toyed with putting empty while looks after each question until the answer has been entered, but can't get that to work either.

Surely I am not the first person to try this, and I am hoping someone can save me some dev time and point me towards an example?

Code below is highly truncated, and does not even come close to what I am looking for, but every time I read a noob post, they get yelled at for not posting their code, so here it is.

/* Reflow Controller User Interface
wdenny
2012.05.31_r1
*/

int b_points = 0;
String readString;
float test;
boolean stringComplete = false;

void setup() {
Serial.begin(9600);
Serial.println("Reflow Controller User Interface"); // identify begining of programing and help with debugging
Serial.println("2012.05.31_r1");
Serial.println(" ");
Serial.println("How many break points? (integer values)");

}

void loop(){
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(readString);
// clear the string:
readString = "";
stringComplete = false;
}
}

void serialEvent(){
while (Serial.available()) {
delay(10);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;
}

}

// char buf[readString.length()+1];
// readString.toCharArray(buf,readString.length()+1);
// test = atof(buf);
stringComplete = true;

}

State machine.

Declare a state variable in the global scope:

uint8_t state = 0;

When you complete the parsing for the response, check what the current state is (i.e. if (state==0) ) and set the appropriate variable. Display the next prompt, increment the state variable and repeat for the next response.

An excellent idea. I looked at that, but will investigate it further...

I suspect I will still have some issues, as the answer to the first question (how many break points?) determines how many states and questions there are... and I will be parsing the data into an array...

wdenny:
I suspect I will still have some issues, as the answer to the first question (how many break points?) determines how many states and questions there are... and I will be parsing the data into an array...

It sounds like after the first question, you ask a single question per the amount entered for the first question (linear relationship). In that case, you can just keep an index within your "prompt for break point time(s)" state, that once it reaches the number you entered for question 1, moves onto the next state.

First of all, ditch using the C++ string class. It makes hash out of your Very Limited Heap Space. What is convenient on a PC is in this case bad practice on an MCU.

Second, the 10 ms delays in serialEvent() might be long enough that all of an input string gets captured and buffered and it might not always be so even though it should.
That's not to mention that serial data transmission has no guarantees and I don't see any error handling code in your program. Murphy was an optimist. If you have to assume anything, assume that something will go wrong! At least that way your code has a chance of not breaking.

Can you please post the outcome of this? I am having the same problems and it would really help to see your functioning code! Thank you,

ArduinoZeb