Arduino Forum

Topics => Robotics => Topic started by: hssathyajith on Aug 13, 2019, 12:01 pm

Title: Serial Monitor input
Post by: hssathyajith on Aug 13, 2019, 12:01 pm
I tried to read 2 inputs from serial monitor, but it takes first input and then moves to next and sudden jump to first input without reading the second input. That means the system reads first input value only.
Please help me to correct this.

//----------------------------------------------------------------------------------

int A;
int B;

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

void loop() {
  Serial.println("Please enter a value for A?")

    A = Serial.read();
    while (Serial.available()){

    }

    Serial.println("Please enter a value for B?")

    B = Serial.read();
    while (Serial.available()){

    }
   if (A>B){
    //do something
   }else{ if (B>A) {
                            //Do another thing
                          }
         
  //Display some thing
 }

endif


  }

//-----------------------------------------------------------------------------

Thank you.
Title: Re: Serial Monitor input
Post by: slipstick on Aug 13, 2019, 12:39 pm
What exactly do you think that "while (Serial.available()){  }" is doing?

To me it looks like you wait until there is nothing available and then try to read() something. Doesn't seem quite right.

Steve
Title: Re: Serial Monitor input
Post by: TomGeorge on Aug 25, 2019, 03:22 pm
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html  (http://forum.arduino.cc/index.php/topic,148850.0.html).
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)