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
}