How to use Serial.read() 2 times

This my code

#include <SoftwareSerial.h>
String readString5,readString1;
int num,num1;
void setup() 
{
      Serial.begin(9600);
}

void loop() 
{

        while (Serial.available()) 
        {
        delay(10);  
        if (Serial.available() >0) 
          {
          char c = Serial.read();
          readString5 += c;
          
          }
        }
          
        num = atoi(readString5.c_str());
        readString5="";
        Serial.flush();
        
        while (Serial.available()) 
        {
       delay(10);  
    
        if (Serial.available() >0) 
         {
          char c = Serial.read();
         readString1 += c;
         }
        num1 = atoi(readString1.c_str());
        readString1="";
       }
      if(num!=NULL&&num1!=NULL)
        {
          Serial.println( num );
          Serial.println( num1 );
        }

I get value in num,but num1 I can’t get it.

 if(num!=NULL)
        {
          Serial.println( num );
          Serial.println( num1 );
        }

num is correct value.but num1 value is 0.
I need get 3 value. for set delay control .

Hmm, what are you actually hoping to achieve? Calling Serial.flush() is almost certainly a mistake, and adding delays for no reason raises alarm bells too...

The improperly posted, poorly formatted code does something. You want it to do something. I think that it is safe to assume that if the two somethings were the same thing, that you would not have posted here. So, I think it is safe to assume that what the code does is not what you want.

What it does, and what you want, are mysteries that only you can solve.

Please Help Me, I can’t solve.
I need get 3 value from android. for set delay control

Please Help Me, I can't solve.

Until you can answer the questions asked, you will not be getting help.

I get value in num,but num1 I can't get it.

 if(num!=NULL)
        {
          Serial.println( num );
          Serial.println( num1 );
        }

num is correct value.but num1 value is 0. I need get 3 value. for set delay control .

Simpllify it a litttle, see what 3 characters are coming in to start:

byte num;
byte num1;
byte num2;

void setup() 
{
Serial.begin(9600);
}

void loop() 
{

if (Serial.available()>2){ // at least 3 bytes received?
num = Serial.read();
num1 = Serial.read();
num2 = Serial.read();
Serial.print ("3 bytes received: ");
Serial.print (num);
Serial.print (" ");
Serial.print (num1);
Serial.print (" ");
Serial.println (num2);
Serial.print ("ASCII to decimal conversion: "); // see www.asciitable.com
Serial.print (num - 0x30);  // convert character '0' to a number 0, same for 1 to 9
Serial.print (" ");
Serial.print (num1 - 0x30);
Serial.print (" ");
Serial.println (num2 - 0x30);
} // send incoming data check
} // end loop

my value isn’t over 9999
num = 9999
num1 = 9999
num2 = 9999

        if (Serial.available() >0) 
          {
          char a = Serial.read();
          char b = Serial.read();
          char c = Serial.read();
          readString1 += a;
          readString2 += b;
          readString3 += c;
          }  
          num1 = atoi(readString1.c_str());
          readString1="";
          Serial.flush();
          num2 = atoi(readString2.c_str());
          readString2="";
          Serial.flush();
          num3 = atoi(readString3.c_str());
          readString3="";
          Serial.flush();

input 123 456 789
output is 1 0 0 2 3 0 4 5 6 9 0 0

if(num1!=NULL)
        {
          Serial.print("GET ");
          Serial.println( num1 );
          Serial.println( num2 );
          Serial.println( num3 );
        }

How does what work?

Capture.JPG

Why don't you try the code in CrossRoads's answer ? :roll_eyes:

in your code :

        if (Serial.available() >0) 
          {
          char a = Serial.read();
          char b = Serial.read();
          char c = Serial.read();
          readString1 += a;
          readString2 += b;
          readString3 += c;
          }

you read 3 chars, as soon as just 1 is there ! what make you think it will wait for you to enter the other 2 ??

You ask for help, you get answers, one of them has code inside it, try it and you'll probably understand a bit more ;)

input 123 456 789 output is 1 0 0 2 3 0 4 5 6 9 0 0

So no surprise that is exactly what you told the code to do.

Input 1 arrives, you see this and your code reads this and two others. The two others are 0xff which means the buffer is empty so the atoi can't see a value in the string so returns zero. You then print these three values out.

Input 2 arrives you see this and your code reads this and two others. The two others are 0xff which means the buffer is empty so the atoi can't see a value in the string so returns zero. You then print these three values out.

and so on and so on.

I send 123 output is num1 =1 num2=0 num3=0 and num1=2 num2=3 num3=0 I don't understand.

i need input 1234(send) 4567(send) 7890(send) output num1=1234 num2=4567 num=7890

I don't understand.

Then read my reply again I have just told you why!

input 1234(send) 4567(send) 7890(send) output num1=1234 num2=4567 num=7890

what will i do?

It depends on what you want to do, you have not said.

thank you I can do it

Often its handy to define a blocking version of Serial.read(), something like:

int blocking_read ()
{
  while (Serial.available () == 0)
  {}
  return Serial.read () ;  // there must at least one character to read now...
}

Of course this version will block if there is no input, so you have to be careful. Its best used when the sketch is under remote command via serial, then there is clearly nothing to be done till the command has been read and parsed...

If you have a sketch that does several tasks, one of which is to read serial input, then the code can be quite complex (blocking_read will not work) - reading a whole line into a buffer can simplify the program structure since the parsing of the input can be delayed until the newline....

blinkboat: input 1234(send) 4567(send) 7890(send) output num1=1234 num2=4567 num=7890

Are those numbers fixed at four numbers. If not you have to have a way of knowing when a number is finished. The conventional way is with a CR or LF. You need to detect these then you know your input string is ready for conversion. Just implement this as a function which returns one number and call it three times.