i am relatively new to Arduino and have taken my first stab at my own program to make a servo move relative to the distance an object is from the sensor.
See code below
#define trigPin 13
#define echoPin 12
int duration, distance;
digitalWrite(trigPin, HIGH); // sends a soundwave
delayMicroseconds(1000); // for 1 second
digitalWrite(trigPin, LOW); // turns off the soundwave
duration = pulseIn(echoPin, HIGH); // receives the echo
distance = (duration/2) / 29.1;
int myservo = map(distance, 0, 150, 255, 0);
Now i know that the sensor is working as if you pull up the serial monitor the sensor is recording the distance, yet the servo only moves when you connect the power and only to the same position.
thanks for looking