Make delay for receive data from Serial monitor

Hello
I want receive numbers from serial monitor of Arduino and when the numbers are more than 850 do something like myservo.write(180); and when the numbers after a 4 secend stay "0" do something like myservo.write(); and when numbers be more than "0" myservo.write(180); again.
please help ...

String servo;
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(3);
Serial.setTimeout(2);
}
void loop() {
while (!Serial.available()); // Wait for characters
double incomingValue = Serial.parseFloat();
if (incomingValue>850) {
myservo.write(180);
}
if (incomingValue==0) {
myservo.write(0);
else{
myservo.write(180);
}
}
}

}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

You can add a timeout to any of the examples fairly easily. When newData is set to true (which happens when a message is received) save the value of millis() - something like this

timeLastMessageReceived = millis();

and then in loop() you can have a test like this

if (millis() - timeLastMessageReceived >= 4000) {
   // it has taken too long for the next message to arrive
   // code to move servo
}

...R

thanks for answer
I did it but not working after 4 second

String servo;
#include <Servo.h>
Servo myservo;

void setup() {
Serial.begin(9600);
myservo.attach(3);
Serial.setTimeout(2);

}
void loop() {

while (!Serial.available()); // Wait for characters
double incomingValue = Serial.parseFloat();

if (incomingValue>850) {
myservo.write(180);
}

if (incomingValue<1) {
if (millis() - incomingValue >= 4000) {
myservo.write(-180);

}

}
}

farsad:
I did it but not working after 4 second

The code in your Reply #2 looks nothing like what I recommended.

...R

So before 4 seconds, it waits for "851" or higher but after 4 seconds, "1" is high enough?

We need more detail on what you expect it to do.