insert delay from serial...

Hello guys.

I was needed to insert delay time for my other program I mean to pause it.

So I come up with this code... But I really don't like it.

So is there any other elegant way to write it. Thanks.

int emount = 0;
int delay1 = 0;
String Str;


 if(Serial.available()>0)
    {
    Str = Serial.readString();   
    emount = Str.length();

               if(emount == 1)
               {
                delay1 = Str[0] -48;
               }            
               
               if(emount == 2)
               {
                delay1 = (Str[0]-48) * 10 + Str[1]-48;
               }

               if(emount == 3)
               {
                delay1 = (Str[0] -48) * 100 + (Str[1] -48) *10 + (Str[2]-48);
               }

               if(emount == 4)
               {
                delay1 = (Str[0] -48) * 1000 + (Str[1] -48) *100 + (Str[2]-48)*10 +Str[3] -48 ;
               }
  
              if(emount == 5)
               {
                delay1 = (Str[0] -48) * 10000 + (Str[1] -48) *1000 + (Str[2]-48)*100 + (Str[3]-48)*10 + Str[4] -48 ;
               }

               
      Serial.println(delay1);
 if(Serial.available()>0)
    {
    long delay1 = Serial.parseInt();
    Serial.println(delay1);
  }
if(Serial.available()>0)
    {
    long delay1 = Serial.parseInt();
    Serial.println(delay1);
  }

:slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Thanks

ha ha!!!

You may be interested in the examples in Serial Input Basics

...R

For why johnwasser's code is better, in addition to being shorter, consider what happens in your code if the user types "Hello" instead of "10000".