Go Down

Topic: Serial Monitor input (Read 135 times) previous topic - next topic

hssathyajith

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.

slipstick

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

TomGeorge

#2
Aug 25, 2019, 03:22 pm Last Edit: Aug 25, 2019, 03:22 pm by TomGeorge
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 .
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.. :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up