Controlling a servo using a HC SR04 sensor

Hi there

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

#include <Servo.h>

Servo myservo;

#define trigPin 13
#define echoPin 12

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

void loop(){
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;
Serial.print(distance);
Serial.println(" cm");
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

V frusrated

Martin

Hi there

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


#include <Servo.h>

Servo myservo;

#define trigPin 13
#define echoPin 12

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

void loop(){
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;
Serial.print(distance);
Serial.println(" cm");
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

V frusrated

Martin

Where is the line where the servo is told to move some place?

You're not telling the servo to move: need to do a myservo.write(xxxxx)

You need to insert a servo.write() command in order to make the servo move and I would suggest that using myservo as both the name of the servo and a variable is not a good idea.

Read the sensor
Map the value to the required range putting the result into a variable
servo.write() the mapped value

Topics merged.

Do NOT cross-post.

thanks to you all for your help.

Sorry for Cross posting, i placed it in the first one by mistake and forgot to delete.

i have now managed to get this working, still needs tweaking but getting there.

regards

Martin

Martin,

If the motion is jittery, then try some running average on the sonic ranger reading instead of feeding the value directly to the map function:

http://playground.arduino.cc/Main/RunningAverage

Also, add some delay such as delay(100) in loop().

#include <Servo.h>
#define trigPin 2
#define echoPin 3
Servo servo;
void setup(){
Serial.begin(9600);
Serial.print(“Mesafe olcme…!”);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
servo.attach(6);
}
void loop(){
long duration,distance;
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=duration/58.2;
delay(29);
Serial.println(distance);
distance=map(distance,0,179,0,255);
servo.write(distance);
}

hi all,
I am bahtiyar
I would make it. If you wonder what is my fault?
thanks

distance=map(distance,0,179,0,255);
  servo.write(distance);

That's an unusual range for a servo.

hello

i have brusless motor and hc-sro4 sensor i want to control the motor with the sensor so if any object come near the sensor with 100cm the motor turn on with medium speed i need the code as posbile as fast because i have a school project

i need the code as posbile as fast because i have a school project

How much are you paying?

What have you tried?

When is our homework due?