Robot

Hey everybody!
I needed help for a programm to controll the distance sensor because I wanted the robot to make a little turn if the distance is under 15 cm. Someone told me to use this: if (cm <=15 && cm>1) . And then I added the rest of the programm. The reason to ask for such a programm was that the senor sometimes measured a wrong value which he declared as 0 cm (instead it wasn´t 0 cm).
My question: How could I tell the robot to ignore the wrong value of 0 cm, but also tell him to
make a turn if the 0 cm value is real (means that this value occure more than one time (not as the wrong value).
The programm:
int E1 = 6;//M1 Speed Control
int E2 = 5;//M2 Speed Control
int M1 = 8;//M1 Direction Control
int M2 = 7;//M2 Direction Control
const int triggerPin = A0;//Distanzmessgerät-> Auslöservariable analog!!
const int echoPin =A1;//Distanzmessgerät-> Aufnahmevariableanalog!!

void setup()
{
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop()
{
int leftspeed = 255; //255 = max speed
int rightspeed = 255;
analogWrite(E1,255);
digitalWrite(M1,LOW);
analogWrite(E2,235);
digitalWrite(M2,LOW);
delay(100);//!!!

long duration, inches, cm;
digitalWrite(triggerPin, LOW);
delayMicroseconds(1);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(1);
digitalWrite(triggerPin, LOW);

duration = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print(“cm”);
Serial.println();

delay(100);//zu kleine Zeitabstände = öfter 0 cm = Roboter macht Kurve
//empfehlenswert: Roboter soll erst wenden, wenn er das Signal cm<10
//mehr als einmal bekommt->macht dann bei Fehlmeldung keine Wendung

if (cm <=15 && cm>1) {
analogWrite(E1,255);
digitalWrite(M1,HIGH);
delay(250);//!!!
}

digitalWrite(A2, HIGH);
delay(50);
digitalWrite(A2, LOW);
delay(50);
digitalWrite(A3, HIGH);
delay(50);
digitalWrite(A3, LOW);
delay(50);

}
long microsecondsToInches(long microseconds) {

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {

return microseconds / 29 / 2;
}

Thank you;)

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..

How could I tell the robot to ignore the wrong value of 0 cm, but also tell him to
make a turn if the 0 cm value is real

You could save the value returned and only act if it has changed since the last time it was read but if the value is really zero will the robot be able to turn anyway ?

Why all the delay()s in the code ? I would think that you want the robot to respond quickly and the delay()s will slow down the responses.

``````  digitalWrite(A2, HIGH);
delay(50);
digitalWrite(A2, LOW);
delay(50);
digitalWrite(A3, HIGH);
delay(50);
digitalWrite(A3, LOW);
delay(50);
``````

What is this all about ?

``````  int leftspeed = 255; //255 = max speed
int rightspeed = 255;
analogWrite(E1, 255);
digitalWrite(M1, LOW);
analogWrite(E2, 235);
digitalWrite(M2, LOW);
delay(100);//!!!!!!!!!!!!!!!!!!!!!
``````

Why do you do this every time through loop() ?

A2 and A3 are two diodes. If the value is zero, he isn´t able to turn because of my command. Because I am new, could you suggest me playgrounds and tutorials for safing and reusing different values.
Thank you!