I´m building on a little Robot and I would like to include in this little project a distance sensor(HC-SR04). But I have a problem with my programm because the HC-SR04 isn´t very exact. I told him to make a 90° curve if the distance is under 10 cm. The problem is that the HC-SR04 sometimes measures 0 cm in real its not true. Maybe its a measuring error. But how could I tell the robot (I´m using an if-function to make a curve) to ignore the 0 cm measured by the HC-SR04? Could you explain me some ways how to do it?
The programm:
if(cm <=10){
analogWrite(E1,255);
digitalWrite(M1,LOW);
delay(675);
}
E1 is a servo.
M1 is the direction-> backwards
Please help me!!!
I would try to use the expression:
if (cm <=10 && cm>1) {
....
}
It's not an error as such: they give a value of 0 when there's no return from anything. So 0 means you're either right on top of the obstacle or as far as can be, and no way of knowing which.
Johncoffee:
I would try to use the expression:if (cm <=10 && cm>1) {
....
}
Thank you very much, I didn t know this command.
Note: You should not use analogWrite() with a servo motor. Use the Servo library. Look at File->Examples->Servo->Knob for an example of how to use the Servo library.