So in my code the sensor only reads the 3 exact same numbers but nothing else.
#include <Servo.h>
int mleft1=2;
int mleft2=3;
int mright1=4;
int mright2=5;
Servo s;
int echoPin = 7;
int trigPin = 6;
int x;
int duration = 0;
int durationleft;
int durationright;
int distance = 0;
int distanceleft;
int distanceright;
void setup() {
Serial.begin(9600);
pinMode (mleft1, OUTPUT);
pinMode (mleft2, OUTPUT);
pinMode (mright1, OUTPUT);
pinMode (mright2, OUTPUT);
s.attach(10);
s.write(100);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
drive();
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) *0.03432 ;
Serial.print(distance);
Serial.println("cm");
delay(1000);
if (distance < 20) {
digitalWrite (mleft1, LOW);
digitalWrite (mleft2, HIGH);
digitalWrite (mright1, LOW);
digitalWrite (mright2, HIGH);
delay(4000);
brake();
delay(300);
turn1();
delay(3000);
durationright= pulseIn(echoPin, HIGH);
distanceright = (durationright/2) *0.03432 ;
if (distanceright < 20){
digitalWrite (mleft1, LOW);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, HIGH);
digitalWrite (mright2, LOW);
delay(2000);
}
turn2();
delay(3000);
durationleft= pulseIn(echoPin, HIGH);
distanceleft= (durationleft/2)*0.03432;
if (distanceleft < 20){
digitalWrite (mleft1, HIGH);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, LOW);
digitalWrite (mright2, LOW);
}
else {
drive();
}
}
}
void turn1(){
s.write(100);
delay(1000);
s.write(40);
delay(1000);
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delay(1000);
s.write(100);
}
void drive(){
digitalWrite (mleft1, HIGH);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, HIGH);
digitalWrite (mright2, LOW);
}
void turn2(){
s.write(100);
delay(2000);
s.write(180);
delay(3000);
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delay(3000);
s.write(100);
}
void brake(){
digitalWrite (mleft1, LOW);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, LOW);
digitalWrite (mright2, LOW);
delay(1000);
}

You seem to have cases where you are looking for a pulse on the echo pin without first sending a pulse on the trigger pin. That will result in a distance of zero.
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delay(3000);
Here you trigger the sensor, and then do nothing for three seconds.
Meanwhile, the echo is lost.
Why did you start a new topic?
Use the NewPing library to fix the ultrasonic coding issue.
TheMemberFormerlyKnownAsAWOL:
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delay(3000);
Here you trigger the sensor, and then do nothing for three seconds.
Meanwhile, the echo is lost.
Why did you start a new topic?
Thats not the problem. As i said even here
void loop() {
drive();
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) *0.03432 ;
Serial.print(distance);
Serial.println("cm");
something is wrong
chillimilli:
Thats not the problem.
So why are you here?
something is wrong
Your code.
Again, why did you start a new topic?
yeah but i dont see the wrong part
TheMemberFormerlyKnownAsAWOL:
Again, why did you start a new topic?
simply because its a new problem i have
You don't see that shouting, putting your fingers in your ears for three seconds, and then expecting to hear an echo that died away 2900 milliseconds ago might be a problem?
chillimilli:
simply because its a new problem i have
It looks an awful lot like the original one, with very minor variations.
TheMemberFormerlyKnownAsAWOL:
You don't see that shouting, putting your fingers in your ears for three seconds, and then expecting to hear an echo that died away 2900 milliseconds ago might be a problem?
#include <Servo.h>
int mleft1 = 2;
int mleft2 = 3;
int mright1 = 4;
int mright2 = 5;
Servo s;
int echoPin = 7;
int trigPin = 6;
int x;
int duration = 0;
int durationleft;
int durationright;
int distance = 0;
int distanceleft;
int distanceright;
void setup() {
Serial.begin(9600);
pinMode (mleft1, OUTPUT);
pinMode (mleft2, OUTPUT);
pinMode (mright1, OUTPUT);
pinMode (mright2, OUTPUT);
s.attach(10);
s.write(100);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
drive();
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.03432 ;
Serial.print(distance);
Serial.println("cm");
if (distance < 20) {
digitalWrite (mleft1, LOW);
digitalWrite (mleft2, HIGH);
digitalWrite (mright1, LOW);
digitalWrite (mright2, HIGH);
delay(4000);
brake();
delay(300);
turn1();
delay(3000);
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
s.write(100);
durationright = pulseIn(echoPin, HIGH);
distanceright = (durationright / 2) * 0.03432 ;
Serial.print(distanceright);
Serial.println("cm right");
s.write(100);
if (distanceright < 20) {
digitalWrite (mleft1, LOW);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, HIGH);
digitalWrite (mright2, LOW);
delay(2000);
}
turn2();
delay(3000);
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durationleft = pulseIn(echoPin, HIGH);
distanceleft = (durationleft / 2) * 0.03432;
s.write(100);
Serial.print(distanceleft);
Serial.println("cm left");
if (distanceleft < 20) {
digitalWrite (mleft1, HIGH);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, LOW);
digitalWrite (mright2, LOW);
}
else {
drive();
}
}
}
void turn1() {
s.write(100);
delay(1000);
s.write(40);
}
void drive() {
digitalWrite (mleft1, HIGH);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, HIGH);
digitalWrite (mright2, LOW);
}
void turn2() {
s.write(100);
delay(2000);
s.write(180);
delay(3000);
}
void brake() {
digitalWrite (mleft1, LOW);
digitalWrite (mleft2, LOW);
digitalWrite (mright1, LOW);
digitalWrite (mright2, LOW);
delay(1000);
}
So now there is no delay between the tirgger and the echo but i still get wrong numbers 
And..?
Posting code, without results or observations, is utterly pointless. We don't have your hardware or environment.
In the absence of commented code, it's hard to know your thought processes and reasoning.
I don't know why you didn't take my suggestion on your other topic of writing a single ranging function.
because i don't know how i can get everything in only one ranging function
float rangeFunction ()
{
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
return (duration/2) *0.03432 ;
}
(uncompiled, untested)
There you go - a freebie.
As I suggested on your other thread, you could add a timeout to pulseIn. Maybe return a negative range.
i somehow get only wrong numbers. U used a code for only the distance and here the numbers seem to be right
int trigger=7;
int echo=6;
long duration=0;
long distance=0;
void setup()
{
Serial.begin (9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) * 0.03432;
if (distance >= 500 || distance <= 0)
{
Serial.println("OOPS");
}
else
{
Serial.print(distance);
Serial.println(" cm");
}
}
but in my other code i get weird numbers although i didnt touched the sensor 
You should make ranging measurements no more than about 20 to 30 times a second.
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.03432 ;
Serial.print(distance);
Serial.println("cm");
no matter where I put a delay, i always get wrong values [tt][/tt]
Snippets are useless.
Why aren't you using the function I posted?
as i said im new to arduino and it seems very complicated. i also think that it won't change the values i get