Wait for User input inside "loop"

I am working on a sketch that requires user input within the "void loop(){}". I thought that I could halt program execution using some boolean variables and while statements.

The desired functionality (inside the loop() function) is as follows:

  1. wait for user input
  2. when user input is detected, print something
  3. wait for a second user input
  4. when user input is detected, print something else
  5. end (no printing upon further iterations of "loop()")

Here's my code:

boolean flag = false;
boolean isWaiting1 = true;     // wait for user input 1
boolean isWaiting2 = true;     // wait for user input 2

void setup()
{
  Serial.begin(9600);         // begin serial comm
}

void loop()
{
  while(isWaiting1 == true)             // enter user input 1 block
  { 
    while (flag == false)               // enter "monitor" loop
    {  
      while(Serial.available() == 0){}  // continuously monitor serial port for any data; 
      Serial.println ("A");             // if (Serial.avaialable != 0) then print "A"
      flag = true;                      // break while(flag == false)
    }
    flag = false;                       // reset flag
    isWaiting1 = false;                 // break while(isWaiting1 == true)
  }
 
  delay(1000);                          // wait 1 second
  
  while(isWaiting2 == true)             // enter user input 2 block
  { 
    while (flag == false)               // enter "monitor" loop
    {  
      while(Serial.available() == 0){}  // continuously monitor serial port for any data; 
      Serial.println ("B");             // if (Serial.avaialable != 0) then print "B"
      flag = true;                      // break while(flag == false)
    }
    flag = false;                       // reset flag
    isWaiting2 = false;                 // break while(isWaiting2 == true)
  }
}

I should mention that I am using a Teensy 3.1 on Arduino 1.0.6 (w/ Teensyduino 1.21-test)

What ends up happening with this sketch is it will...

  1. wait for the first user input
  2. detect it upon sending something to the serial monitor
  3. does not wait for the second user input
  4. proceeds to print without waiting
  5. end

I suspect that the issue stems from the following line of code:

while(Serial.available() == 0){}  // continuously monitor serial port for any data;

It seems that after the first user input the line of code above is not TRUE; which explains why there is no waiting for the second user input. I tried manually assigning "Serial.avaiable()" to zero after receiving the first user input, but to no avail. Also tried using flush() and memset(). No luck. Any guidance would be much appreciated!

Bear in mind that
a) Serial.read is really slow, so while you are still sending the first sentence (multiple characters) to the Arduino, the read buffer is emptied by your first while(isWaiting1...) function and the following characters may end ub in the second while(isWaiting2...) loop.

b) depending on your terminal setting, you append '\n' and / or '\r' whenever you hit send.

Serial data arrives one byte at a time. If the user enters more than one character or if you have a line ending character being sent from the Serial monitor then there will still be data in the serial buffer after the first character and the second wait for serial will not happen. Serial.flush() flushes the transmit buffer, not the receive one.

What user input are you expecting ? One character or more ? What do you have Line ending set to in the Serial monitor ?

UKHeliBob:
One character or more ? What do you have Line ending set to in the Serial monitor ?

The majority of the inputs that I expect are one character. I will need to handle a couple of inputs that are a maximum of 3 chars. Line ending is/has been set to "No line ending".

I am working on a sketch that requires user input within the "void loop(){}".

No your not! There is no such program!. All programs can be written without ever having to wait in "loop".

Some times we do wait in loop (or something called from loop) but only if the wait is very short (a mill or 2 eg analogRead()). Problems start when you wait (block) longer than this.

Change your way of thinking!

Read this post

http://forum.arduino.cc/index.php?topic=223286.0

Mark

@ Holmes:

I am making a sign-language glove (using piezo-resistive sensors). The project requires building a training set (i.e. database) of sensor values which need to be "recorded" at distinct "orientations". Suppose I want to make a training set for K classes. My sensor array is N, and suppose further that I require M examples of each class.

So we have K, MXN matricies that need to be populated with M, independent "examples" of the sensor array, of whatever class, K, I am currently training for. Perhaps this can be handled in the setup().

I am making a sign-language glove (using piezo-resistive sensors). The project requires building a training set (i.e. database) of sensor values which need to be "recorded" at distinct "orientations". Suppose I want to make a training set for K classes. My sensor array is N, and suppose further that I require M examples of each class.

So we have K, MXN matricies that need to be populated with M, independent "examples" of the sensor array, of whatever class, K, I am currently training for. Perhaps this can be handled in the setup().

So what!Read the post I linked you to!

Mark