Wanted to get help to program using an UltraSonic Sensor + Servo Motor

Hey,

This is my first time using Arduino's.

I wanted to write a code in which I use a ultrasonic sensor that detects movement say less than 50cm. In which that will trigger the servo motor to move 90 degrees.

Here is what I have so far

#include <Servo.h>
#define trigPin 13
#define echoPin 12

Servo myservo;

void setup () {
myservo.attach(9);
myservo.write(0);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop(){
int duration, distance,pos=0,i;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) /29.1;
Serial.print (distance);
Serial.print(" cm");
if (distance < 50) // This is where the Servo On/Off happens
{
myservo.write(180);
}
else{
myservo.write(0);
}
delay(100);

}

After I finish uploading all I get is, the motor to continuously move forever.

I just want the servo to move 90 degrees once, when the ultrasonic sensor has detected movement for say 50 cm or less.

First time with Arduino's and coding. Help is appreciated.

First off, how much time are you giving the servo to move to its destination :wink:
100ms is a bit fast I think

is Serial.print (distance); printing the correct value?
How much is your servo moving?

The Sensor was reporting the correct value. But the Servo kept moving non-stop.

This is a different code I am trying.

#include <Servo.h> //Load Servo library

int trigPin=13; // Sensor trigPin is connected to Arduino,13.
int echoPin=12; //Sensor echoPin is connected to Arduino, 12.
int servoControlPin=6; //Servo Control pin conncted to Arduino pin 6

float pingTime; //Time for ping to travel to target and back.
float targetDistance; //Distance from sensor to target.
float speedofSound= 776.5; //Speed of Sound in Miles/Hour (Temp is 77 degrees F)
float servoAngle; // Variable for the angle of the Servo.

Servo myservo; //Create a Servo object called myservo

void setup(){
Serial.begin(9600); //Turn on Serial Port.
pinMode(servoControlPin, OUTPUT); //Servo controlPin is an output.
pinMode(trigPin, OUTPUT); //Sensor trigpin is an outpout.
pinMode(echoPin, INPUT); //Sensor echopin is an input.

myservo.attach(servoControlPin); //Tell Arduino where the servo is connected.
}

void loop(){
digitalWrite(trigPin, LOW); //Set trigger pin LOW.
delayMicroseconds(2000); //Pause to let signal settle.
digitalWrite(trigPin, HIGH); //Set trigger pin HIGH
delayMicroseconds(15); //Pause in high state
digitalWrite(trigPin, LOW); //Bring Trigpin back low.

pingTime = pulseIn(echoPin, HIGH); //Measure pingTime at echoPin in microseconds.
pingTime = pingTime/1000000.; //Converts ping time to seconds.
pingTime = pingTime/3600.; //Converts ping time to hours.

targetDistance = speedofSound * pingTime; //Calculates distance in miles.
targetDistance = targetDistance/2; //Accounts for round trip of ping to target.
targetDistance = targetDistance*63360; //Convert targetDistance to inches (63360 inches in a mile)

Serial.print("The distance to the target is: ");
Serial.print(targetDistance);
Serial.print("inches");
delay(100); //we can take this off, to respond faster.

servoAngle= (90.) * targetDistance +37; //Calculate servoAngle based on targetDistance
myservo.write(servoAngle); //Write servoAngle to the servo myservo
delay(100); // delay to slow things down

}

float pingTime; //Time for ping to travel to target and back.

Time is measured in microseconds. That is, you get an integral number of microseconds. Storing that value in a float is silly.

  pingTime = pulseIn(echoPin, HIGH); //Measure pingTime at echoPin in microseconds.
  pingTime = pingTime/1000000.; //Converts ping time to seconds.
  pingTime = pingTime/3600.; //Converts ping time to hours.

More silliness. You seem to have some unrealistic expectations of the Arduino's ability to do math. A completely different approach, something more along the lines of how everybody else does it, is needed to get a meaningful distance in inches.

pinMode(servoControlPin, OUTPUT); //Servo controlPin is an output.

Which the Servo library takes care of. Quit diddling with the pin you told the Servo instance was its.

Comment out all the crap with getting a distance for the servo. Hard-code a value. If the servo does not hold still, it isn't a code problem.

What kind of servo is it? Is it moving in one direction, at a constant speed, or is it moving back and forth?