I'm controlling a servo with a ping sensor and I can't get the servo to return to an initial position before it starts responding to the sensor. Below is my code, when I mean initial position I'm referring to 180 degrees. I want it to start at 180 and when something comes into contact with the ping sensor at less than 5 inches then it moves over to 0 degrees. I borrowed this code from the University of Pennsylvania so if there is a simpler way please let me know. Thanks!
#include <Servo.h> // include the servo library
Servo servoMotor; // creates an instance of the servo object to control a servo
const int pingPin = 7; // Control pin for Ping Sensor
const int servoPin = 9; // Control pin for servo motor, may only be pin 9 or 10
int moveServoLeft = 0; // variable to control the motion of the Servo Motor to the left
int moveServoRight = 0; // variable to control the motion of the Servo Motor to the right
int servoPosition = 0; // variable to control the position of the Servo Motor
void setup() {
servoMotor.attach(servoPin); // attaches the servo on pin 9 to the servo object
servoMotor.write(0); // set servo to intial position
moveServoLeft = 1; // intial motion of Servo is from 0 to 180 i.e left
servoPosition = 0;
}
void loop() {
// get Distance
long duration, inches, cm;
// to set speakerPin and servoPin mode to OUTPUT
pinMode(servoPin, OUTPUT);
// The PING Sensor is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING Sensor, a HIGH pulse whose duration is the time (in
// microseconds) from the sending of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
delay(200);
// to enable tracking the Servo motor is rotated only if the distance of the objects are less than or equal to 5”
if(inches <= 5) {
servoMotor.attach(servoPin);
servoMotor.write(servoPosition);
delay(15);
// to move left if the motor has reached 0 degrees
if(servoPosition == 0 && moveServoRight == 1){
moveServoLeft = 1;
moveServoRight = 0;
}
//controls how quick it moves
// to move left from 0 degree to 180 degrees
if(moveServoLeft == 1 & servoPosition <= 160)
servoPosition = servoPosition + 20;
// to move right from 180 degrees to 0 degree
if(moveServoRight == 1 & servoPosition >= 0)
servoPosition = servoPosition - 20;
}
// to disable tracking the Servo motor is detached if the distance of the object is greater than 5”
else {
servoMotor.detach();
for(int duration = 0; duration < 100; duration ++){
delayMicroseconds(200);
delayMicroseconds(200);
}
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are 73.746 microseconds per inch (i.e. sound travels at
// 1130 feet per second). This gives the distance travelled by the ping, outbound and return, so we divide by 2 to
// get the distance of the obstacle. See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter. The ping travels out and back, so to find the
// distance of the object we take half of the distance travelled.
return microseconds / 29 / 2;
}