Serial1.avaialble inside Serial.available

Hello , please i need your help :frowning: :frowning:
i’m using Arduino Mega , and i’m trying to use two serial one inside the other ,
the firs one is working , but the second one when i choose one of the options but it does not do any thing
so please if any one know how i can fix this problem :smiley: :smiley:
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.println(“WELCOME”);
delay(15);
Serial.print(“if you need Right or Left press 1 , if you need to Stop press 1”);
}
void loop()
{
if(Serial.available()>0)
{
char x= Serial.read();
Serial.println(x);
if (x==‘1’)
{
Serial.println(“press 5 for Right or 6 for Left”);
if(Serial1.available()>0)
{
char z= Serial1.read();
Serial.println(z);
if (z==‘5’)
{ Serial.println(“Go Right”);}
if (z==‘6’)
{Serial.println(“Go Left”);}
}
}
if (x==‘2’)
{Serial.println(“Stop”);}
}
}

two_Serial.available.ino (643 Bytes)

.available() simply tells you if there's input waiting to be read. Therefore, after you detect a "1" on Serial and print the instructions, you give the user no time to send anything on Serial1... nothing is available yet, so the check fails.

One option is to wait until something becomes available:

while (Serial1.available() == 0) ;

But be caferul, because this is blocking code - it won't let you do anything else while you're waiting. For that you'll need slightly more advanced techniques.

sorry but i tried what you say but also does not work :( code : void setup() { Serial.begin(9600); Serial1.begin(9600); Serial.println("WELCOME"); delay(15); Serial.println("if you need Right or Left press 1 , if you need to Stop press 2"); } void loop() { if(Serial.available()>0) { char x= Serial.read(); Serial.println(x); if (x=='1') { Serial.println("press 5 for Right or 6 for Left");

while(Serial1.available()>0) { char z= Serial1.read(); Serial.println(z); if (z=='5') { Serial.println("Go Right");} else if (z=='6') {Serial.println("Go Left");} } }

if (x=='2') {Serial.println("Stop");} } }

In the while loop, I wrote " == 0", you wrote " > 0"... that's exactly the opposite ;)

really i'm so so so so sorry but also it does not work :( :( please could you help me what can i do so it can work :D code: void setup() { Serial.begin(9600); Serial1.begin(9600); Serial.println("WELCOME"); delay(15); Serial.println("if you need Right or Left press 1 , if you need to Stop press 2"); } void loop() { if(Serial.available()>0) { char x= Serial.read(); Serial.println(x); if (x=='1') { Serial.println("press 5 for Right or 6 for Left");

while(Serial1.available()==0); { char z= Serial1.read(); Serial.println(z); if (z=='5') { Serial.println("Go Right");} else if (z=='6') {Serial.println("Go Left");} } }

if (x=='2') {Serial.println("Stop");} } }

Ugh... where does the input to Serial*1* come from? As long as you're working with the serial monitor, you only have access to Serial...

ummm , so the serial monitor only work with one Serial !!

EmanSarkez: ummm , so the serial monitor only work with one Serial !!

Yes. Serial Monitor is connected through USB to Serial. Unless you have something connected to Serial1 you won't get any characters from Serial1. Maybe this is what you meant:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("WELCOME");
  delay(15);
  Serial.println("if you need Right or Left press 1 , if you need to Stop press 2");
}
void loop()
{
  if(Serial.available()>0)
  {
    char x= Serial.read();
    Serial.println(x);
    if (x=='1')
    {
      Serial.println("press 5 for Right or 6 for Left");
      while(Serial.available()==0);  // wait for input
      char z = Serial.read();
      Serial.println(z);
      if (z=='5') { 
        Serial.println("Go Right");
      }
      else if (z=='6') {
        Serial.println("Go Left");
      }
    }
    if (x=='2') {
      Serial.println("Stop");
    }
  }
}

Yes Yes, this is what i meant and what i need to do :D thank you very very very much for your help :D