Object Detection with HC-SR04 ranging device

I am trying to set the sensor so that it will read the distance of an object and when it detects a large change in distance, it reads "New Object Detected" but I cannot figure out how to implement that to my code. I have it so that it only detects objects within a certain distance, so it will skip the empty spaces in between and I want it to display that it has found a new object after skipping that space. My code is attached.

#include "newping.h"
#include <Servo.h>
#include "AverageValue.h"

#define TRIGGER_PIN 3
#define ECHO_PIN 2

#define MAX_DISTANCE 70

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int angle= 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);

}

void loop() {
  for (angle = 0; angle<= 180; angle+= 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(angle);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position

  }
  for (angle = 180; angle>= 0; angle-= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(angle);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position

   if (sonar.ping_cm() > 0) {
    Serial.print("  Distance = ");
  Serial.print(sonar.ping_cm());
  Serial.println(" cm");
    Serial.print(" Angle = " );
  Serial.print(angle);
   }

  delay(500);
  }
  }

seems that as you sweep the servo you should capture the max and min distances along with the angle of the min distance.

presumably there is an obstacle if either you specify some threshold distance or that the difference between the max and min is some threshold

you can probably sweep the servo more than a single degree. I believe the sensor is sensitive to a 15 degree aperture, so you might sweep every 5 deg until something is detected and then sweep every 3 deg within a smaller range to locate the angle more precisely

if you're going to sweep from 180 to 0 while making measurements, you might as well just move the servo from 0 to 180 in one step. Of course you could probably sweep back and forth, clockwise and counter clockwise by using a dir variable (+/- 1) to change the angle and invert the direction when 0 OR 180 is reached, or 2 limit values when trying to zero in

you actually have a 515 msec delay which mean a 92 sec sweep period. could be much fast. sounds travels ~ 1ft/msec. you need to wait for the energy in the previous ping to dissipate before pinging again.

waiting 100 msec between pings would account for an object 50 ft away. And if advancing 5 deg / pin, would require 3.6 sec per sweep

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.