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);
}
}