CAN ANYONE HELP WITH MALFUNCTIONING OF ULTRASONIC SENSOR WITHOUT ANY OBSTACLE

I want my ultrasonic sensor to rotate (which is placed on top of servo motor) 0 to 180 angle, at a 90-degree angle it works properly but when my motor rotates in any different angle the sensor reads the signal as obstacle present though there is no obstacle.

The following code is pasted below kindly have look and correct me if am wrong.

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(10); // waits 10ms for the servo to reach the position
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//delayMicroseconds(10);

duration = pulseIn(echoPin, HIGH);
distanceCM= duration*0.034/2;

if ((distanceCM<=distanceTotal)){
if((distanceCM<=distanceTotal/2)){
// digitalWrite(buzzer,HIGH);
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
//digitalWrite(buzzer,HIGH);
delay(50);
//digitalWrite(buzzer,LOW);
lcd.setCursor(0,0);
lcd.print(" “);
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Distance: “);
lcd.print(distanceCM); // Prints the distance value from the sensor
lcd.print(” cm”);
//delay(500);
lcd.setCursor(0,1);
lcd.print(” “);
lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Angle: “);
lcd.print(pos); // Prints the distance value from the sensor
lcd.print(” deg”);
delay(2000);
}
else{
digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
//digitalWrite(buzzer,HIGH);
delay(50);
// digitalWrite(buzzer,LOW);
delay(500);
//digitalWrite(buzzer,HIGH);
delay(50);
// digitalWrite(buzzer,LOW);
lcd.setCursor(0,0);
lcd.print(” “);
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Distance: “);
lcd.print(distanceCM); // Prints the distance value from the sensor
lcd.print(” cm”);
//delay(500);
lcd.setCursor(0,1);
lcd.print(” “);
lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Angle: “);
lcd.print(pos); // Prints the distance value from the sensor
lcd.print(” deg”);
delay(2000);
}
}
else {
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
// digitalWrite(buzzer,LOW);
lcd.setCursor(0,0);
lcd.print(” “);
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Distance: “);
lcd.print(distanceCM); // Prints the distance value from the sensor
lcd.print(” cm”);
delay(10);
lcd.setCursor(0,1);
lcd.print(” ");
lcd.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print(“Angle: “);
lcd.print(pos); // Prints the distance value from the sensor
lcd.print(” deg”);
delay(50);
}

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.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

I think your keyboard is broken.
Your subject line is in all caps.

The other possibility is that you are shouting.
Since that is very rude i figure you're keyboard is broken.

I think the obstacle to getting answers is the all caps.

Bottom right at the bottom of your post is the more modify button that would allow you to fix that

Remember that pulseIn() returns zero (0) when there's no return signal. If you're only checking that the distance calculation is less than some set value, you're going to trip up on the case where there was no echo.

Try adding a check if( duration > 0 && ... before proceeding with all the other checks.

dave-in-nj:
I think the obstacle to getting answers is the all caps.

Bottom right at the bottom of your post is the more modify button that would allow you to fix that

dave-in-nj, Karma++

In another forum I used to help in many of the very skilled regulars would not help the ALL CAPS RUDE -

as well of the I need help RIGHT NOW! - many of the entitled ones expected help right now - forgetting that most of the people helping in the forums are unpaid volunteers