Serial not waiting for another input.

Hi, I have problem with serial not waiting for input after one input is entered. Here is my code and what it should do. I want to control servo and have a menu for functions like speed and angle and stuff like that. I used to have it, it was working, but the code was lost and now I can't get it to work again. The menu is working okay, you enter command, it chooses the right case and do something. But the problem is if I want to set the angle, I enter command for it, but the code doesn't wait for inputing the angle, no matter what. I searched all over the internet back and forth, but no solution worked. So here I am, desperately seeking for help with my little code. Thanks in advance for any ideas. PS: Case letters are for Czech language, so if they don't make any sense to you, thats why.

void setup() 
{
Serial.begin(9600);
Serial.println("Enter command: ");  
}

void loop() 
{
while (Serial.available()>0)
  {
    String cmdStr = Serial.readString();
    char cmd = cmdStr[0];
    if (cmd == 'r') //This is here because of "speed" and "restart" have same initials in Czech language.
    {
      cmd = cmdStr[1];    
    }
    switch (cmd)
    {
//-------------Menu - Home-------------      
      case 'h':

      Serial.println("Home position");

      break;
//-------------Menu - Rotate-------------      
      case 'o':
      Serial.println("Set the angle");
      while (Serial.available()>0)
      {    
      int angle = Serial.parseInt();
      Serial.print("Angle ");
      Serial.println(angle);
      Serial.println("Set the angle");
      }
      break;
//-------------Menu - Speed-------------      
      case 'y':
      Serial.println("speed");
      break;
//-------------Menu - Manual-------------      
      case 'm':
      Serial.println("manual");
      break;
//-------------Menu - Restart-------------      
      case 'e':
      Serial.println("restart");
      break;              
    }    
  Serial.println("Enter command");
  }
}
      while (Serial.available()>0)
      {   
      int angle = Serial.parseInt();
      Serial.print("Angle ");
      Serial.println(angle);
      Serial.println("Set the angle");
      }

Why is this done in a while loop? Why is angle local to the while loop? It won't be available after the loop ends, so the whole loop is pointless.

There is nothing in your code that waits for serial input, except the parseInt() calls that wait for a non-digit to arrive. Where are you expecting the code to wait?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

There is also a simple user-input example in Planning and Implementing a Program

...R

PaulS:

      while (Serial.available()>0)

{  
     int angle = Serial.parseInt();
     Serial.print("Angle ");
     Serial.println(angle);
     Serial.println("Set the angle");
     }



Why is this done in a while loop? Why is angle local to the while loop? It won't be available after the loop ends, so the whole loop is pointless.

There is nothing in your code that waits for serial input, except the parseInt() calls that wait for a non-digit to arrive. Where are you expecting the code to wait?

Well, I forgot to mention, that this part of code works perfectly fine when separated from the menu. Yes, I agree, that "int" is not supposed to be there, but everything else does work. It is waiting for input, after that it shows the angle and waits for another input. The menu is also working fine separated. I've just tested code with few changes. Correct me, if I am wrong, but isn't Serial.parseInt() supposed to stop the code until I enter something? also tried clearing serial via

while (Serial.available() > 0) 
{
Serial.read();
}

but that didn't work either. It just flies through the code like nothing happens. I also tried setting a delay after the reading so I have enough time to input something and it worked. But this is just stupid. So I am asking for ideas why is it happening.

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

There is also a simple user-input example in Planning and Implementing a Program

...R

Thanks for sending me your tutorials. I took a look at them and I found kind of confirmation

Things that are not used in the examples

You will notice that the examples here do not use any of these Arduino functions
Serial.parseInt()
Serial.parseFloat()
Serial.readBytes()
Serial.readBytesUntil()

All of these are blocking functions that prevent the Arduino from doing something else until they are satisfied, or until the timeout expires. The examples here do exactly the same job without blocking. That allows the Arduino to do other things while it is waiting for data to arrive.

, that Serial.parseInt() should stop the code and wait, but it doesn't. Clearing serial via "your" code, that you have in one of yours tutorials, didn't work either.

Kiblik:
Clearing serial via "your" code, that you have in one of yours tutorials, didn't work either.

As you have not posted the programs in which you used those features I cannot comment. The devil is in the detail.

...R

Kiblik:
Correct me, if I am wrong, but isn't Serial.parseInt() supposed to stop the code until I enter something?

From Serial.parseInt() - Arduino Reference

•Parsing stops when no characters have been read for a configurable time-out value, or a non-digit is read;

•If no valid digits were read when the time-out (see Serial.setTimeout()) occurs, 0 is returned;

Robin2:
As you have not posted the programs in which you used those features I cannot comment. The devil is in the detail.

...R

Sorry, my bad. The code is here

String cmdStr;
char cmd;
int angle; 

void setup() 
{
Serial.begin(9600);
Serial.println("Enter command: ");  
}

void loop() 
{
if (Serial.available()>0)
  {
    cmdStr = Serial.readString();
    cmd = cmdStr[0];
    if (cmd == 'r') //This is here because of "speed" and "restart" have same initials in Czech language.
    {
      cmd = cmdStr[1];    
    }
    switch (cmd)
    {
//-------------Menu - Home-------------      
      case 'h':
      Serial.println("Home position");
      break;
//-------------Menu - Rotate-------------      
      case 'o':
      if (!Serial.available())
      {
      Serial.println("Set the angle");
      angle = Serial.parseInt();
      delay(2000);
      Serial.print("Angle ");
      Serial.println(angle);            
      }
      break;
//-------------Menu - Speed-------------      
      case 'y':
      Serial.println("speed");
      break;
//-------------Menu - Manual-------------      
      case 'm':
      Serial.println("manual");
      break;
//-------------Menu - Restart-------------      
      case 'e':
      Serial.println("restart");
      break;              
    }    
  Serial.println("Enter command");
  }
  
}

evanmars:
From Serial.parseInt() - Arduino Reference

•Parsing stops when no characters have been read for a configurable time-out value, or a non-digit is read;

•If no valid digits were read when the time-out (see Serial.setTimeout()) occurs, 0 is returned;

Hi, thanks for your advice. I have tried to set the timeout to be longer. Yes, it is waiting longer, but it's also delaying code after input. I thought, that timeout is supposed to wait longer on input and if you input something, it should continue right after, but that's not the case. Or at least not for me. Did I miss something important?

Did you take careful note of this?

or a non-digit is read;

wildbill:
Did you take careful note of this?

If I understand correctly, it stops when it reads something, that's not digit. But I am not too sure, if I understand the "read" part.

Kiblik:
Sorry, my bad. The code is here

You need to study how the user input is handled in Planning and Implementing a Program.

For user input you need a means to wait until the user responds, almost regardless of how long it takes. A time based system is not suitable, unless you simply want to deal with the situation where the user will never respond.

...R

Robin2:
You need to study how the user input is handled in Planning and Implementing a Program.

For user input you need a means to wait until the user responds, almost regardless of how long it takes. A time based system is not suitable, unless you simply want to deal with the situation where the user will never respond.

...R

Okay, I got what you say. I wanted to have the code as easy as possible, because I bought arduino recently and before that I haven't done much programing, so my knowledge is very little. I took a deeper look at your guide and I decided, that I will make the program from the beginning. Thanks everyone for sending their ideas and helps. Wish you all have a nice day.