HC SR04 distance sensor with servo

Hi,

I want to control the action of a Servo with the read distance from the HC SR04 module. When an object is within 100 cm from the sensor, the sensor will turn a few times. If the distance > 100cm then the servo sets itself to 0.

The problem is that it is not working. The Servo moves a little en then stops. I need a little help in this!

This is my code:

#include <Servo.h> 
 
Servo head2;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 90;    // variable to store the servo position 
 
 
 //afstandsmeter
const int influence = 50; // Distance for action
const int echoPin = 7;
const int trigPin = 8;
int maximumRange = 500;
int minimumRange = 0;
long duration, distance;
int obstacle = afstand();  //afstand=distance in dutch
 
void setup() 
{ 
  head2.attach(9);  // attaches the servo on pin 9 to the servo object 

//afstandssensor
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
} 
 
void loop() 
{ 
  delay(100);
  afstand();
  
  if(obstacle>influence)
{
  turnhead();
}
if(obstacle<influence){
  head2.write(0);
}

 Serial.println(distance);
  delay(5000);
} 

void turnhead(){
   for(pos = 90; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    head2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                     // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    head2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 15ms for the servo to reach the position 

}
 for(pos = 0; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    head2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                     // waits 15ms for the servo to reach the position 
  } 
}

long afstand(){
  /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 distance = constrain(distance, 0, 500);
 
 return duration / 58.2;
 
}

Do you have adequate power supply to the servo ?

You cannot power the servo from the arduino.

The first thing I would try, is making a call to your turnhead( ) function from setup( ), before doing any of the distance-checking stuff.

This will confirm whether or not the turnhead( ) function works. If it doesn't, you will need to change it. Once you have proved that part works, you can worry about the distance measurement logic.

michinyon: Do you have adequate power supply to the servo ?

You cannot power the servo from the arduino.

The first thing I would try, is making a call to your turnhead( ) function from setup( ), before doing any of the distance-checking stuff.

This will confirm whether or not the turnhead( ) function works. If it doesn't, you will need to change it. Once you have proved that part works, you can worry about the distance measurement logic.

Something you don't think about when programming is the ground part. I connected all the ground wires to one point and now it works. It is still not very smooth, but I can work on that in the code! ;)