0

. I used the inbuilt basic example of measuring the distance to test my ultrasonic sensor. the object is kept at 10 - 15 cm. form the ultrasonic sensor but still the readings show 135 cm only

#include <Ultrasonic.h>
int trigPin = 5;    
int echoPin = 6; 
#include <Servo.h>   //servo library
Servo servo;        
int servoPin = 7;
long duration, dist, average;   
long aver[3];   //array for average


void setup() {       
    Serial.begin(9600);
    servo.attach(servoPin);  
    pinMode(trigPin, OUTPUT);  
    pinMode(echoPin, INPUT);  
    servo.write(0);         //close cap on power on
    delay(100);
    servo.detach(); 
} 

void measure() {  
 digitalWrite(100,HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(100);
digitalWrite(trigPin, HIGH);
delayMicroseconds(150);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2.0) / 29.1;    //obtain distance
}
void loop() { 
  for (int i=0;i<=2;i++) {   //average distance
    measure();               
   aver[i]=dist;            
    delay(100);              //delay between measurements
  }
 dist=(aver[0]+aver[1]+aver[2])/3;    

if ( dist<15 ) {
//Change distance as per your need
 servo.attach(servoPin);
  delay(1);
 servo.write(0);  
 delay(2500);       
 servo.write(100);    
 delay(500);
 servo.detach();      
}
Serial.print(dist);
}

#include <Ultrasonic.h>
int trigPin = 5;    
int echoPin = 6; 
#include <Servo.h>   //servo library
Servo servo;        
int servoPin = 7;
long duration, dist, average;   
long aver[3];   //array for average

void setup() {       
    Serial.begin(9600);
    servo.attach(servoPin);  
    pinMode(trigPin, OUTPUT);  
    pinMode(echoPin, INPUT);  
    servo.write(0);         //close cap on power on
    delay(100);
    servo.detach(); 
} 

void measure() {  
    digitalWrite(100,HIGH);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(100);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(150);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    dist = (duration/2.0) / 29.1;    //obtain distance
}

void loop() { 
  for (int i=0;i<=2;i++) {   //average distance
    measure();               
   aver[i]=dist;            
    delay(100);              //delay between measurements
  }
  dist=(aver[0]+aver[1]+aver[2])/3;    

  if ( dist<15 ) {
    //Change distance as per your need
    servo.attach(servoPin);
    delay(1);
    servo.write(0);  
    delay(2500);       
    servo.write(100);    
    delay(500);
    servo.detach();      
  }
  Serial.print(dist);
}
6
  • 1
    No, we are not able to, because you didn't give us enough information. You should edit your question to include your complete code (format it correctly please) and your wiring. Also you should explain, how you are testing the project. An ultrasonic sensor needs a rather hard surface in front of it, so that the ultrasonic waves get reflected enough. Soft materials like fabric, or surfaces, that are not parallel to the sensor, will make the measurement more difficult/less accurate.
    – chrisl
    Nov 20, 2019 at 12:44
  • i have sent my code Nov 20, 2019 at 13:20
  • 1
    To provide additional information, please always edit your question. There is an edit button for this. Your code is not the answer for your question, thus it should not be posted as answer. As you are a new contributor (welcome!), you might find the Tour rather helpful. High quality question have way more probability to get good answers.
    – chrisl
    Nov 20, 2019 at 13:26
  • I edited your question for you
    – chrisl
    Nov 20, 2019 at 13:27
  • 1
    Re “digitalWrite(100, HIGH);”: There is no pin 100 on the Uno. Nov 20, 2019 at 15:02

1 Answer 1

1

You have a lot of redundant code:

  1. include Ultrasonic.h is unnecessary.
  2. servo.attach and servo.detach without reusing pin is unnecessary
  3. The ultrasonic code is not correct. It should be:
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // measure duration of pulse from ECHO pin
    duration_us = pulseIn(echoPin, HIGH);

    // calculate the distance
    distance_cm = 0.017 * duration_us;

Refer to the Ultrasonic sensor tutorial and Servo motor tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.