Clearing Arduino buffer

I am sending values to Arduino from Serial monitor repeatedly.

How can I discard those values given to Arduino while it is in delay.

I can not modify sending values repeatedly.

I have tried the following code but it only executes the first instruction. void setup(){

Serial.begin(9600); // other initializations

} void loop(){

inp_var();

Serial.end();

delay(time);

Serial.begin(9600);

}

while(Serial.available()){
     char junk = Serial.read();
}

That code will discard everything in the Serial buffer by reading in the characters and doing nothing with them.

You don't even need to store the result if you do nothing with it

while ( Serial.available() )
    Serial.read();

What about those values that are entered while the Arduino is in delay?

arttp2: What about those values that are entered while the Arduino is in delay?

What about them? Those are the ones you want to get rid of right? If they are sitting in the receive buffer when you run the code above then they will be discarded.

I also want to accept those values entered once the delay() is over. How to do that?

What are you trying to do exactly? Why do you need a delay in your code? Mixing delays and Serial processing isn’t really a good idea.

arttp2: I also want to accept those values entered once the delay() is over. How to do that?

What do you mean? Maybe instead of using delay(), write a function that sits and waits and does nothing but check for serial input and throw it away.

The code given above will clear everything out of the serial buffer. If you use that when you know that everything in the serial buffer needs to be thrown away then you're all good. The problem is that you sound like you can't know that. How will the Arduino know which characters arrived in that buffer during the delay and which ones were before or after? Serial data doesn't come with a time stamp or anything.

I found the solution finally.

There was a line in my code that was messing things up.

Interestingly, for the functionality I require,

Serial.end(); delay(time*1000); Serial.begin(9600);

works fine.