Need help with Sharp Distance Sensor and Servomotor

Hello everyone,

I am new in the forum and in the arduino world. I am making a project that involves a Sharp distance sensor GP2DP12 and a servomotor. Since I am learning it all through google (Thank God for Google) I could manage to make the servo change from 0, 90, 180 degrees according to the distance. The sensor I calibrate using a formula I found online which reduce the read to 0,1,2 (2 is the closest and 0 the farthest distance) So when:

  • distance < 0 servomotor = 0 degrees
  • distance > 1 servomotor = 90 degrees
  • distance > 2 servomotor = 180 degrees

I am sure the calibration is not correct since the higher number is the closest distance (lol) but at least it works a bit of like I wanted. However, my real idea is that the servomotor will not only open and close but it will also do it according to the speed that you approach. I know the sensor only sense distance but I was wondering if there was a function that I could say for example:

If distance go from 0 to 1 in 500 (1/2 second) servomotor = 90 degrees.
But
If distance go from 1 to 2 in 250 (1/4 second) servomotor = 0 degrees. (Closes again cause the sensor read changes too fast)
Or
If you approach slowly it will open smoothly.

So I could made it work also according to how fast the sensor reads changes to have a response in the servomotor. Is this possible to do with this type of sensor?? Or I am trying impossible?

I figure I could change the sweep speed calibrating the steps with a for function meaning if it increases in 1 is slower than in 3. But I have spent almost a week and a half trying to figure how to do it with the sensor. I have checked timers, switch/case function but it is not really clear for me if that could help me cause I couldn’t made it work. Someone advice me to use a kinect camera instead of the Sharp distance sensor but I have no idea how that works and how to program that; but I will to ask and research further about it I guess…

This is the code I develop (very plain since a am really new in this):

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int sensepin = 2;  // analog pin used to connect the sharpsensor
int dist;    // variable to read the value from the analog pin 
int pos = 0;

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
Serial.begin (9600);
} 
 
void loop() 
{ 
  float dist = analogRead(sensepin)* 0.0048828125;            // to calibrate it from 0 to 2 
  
  if (dist <1){
    pos = 0;
    myservo.write(pos);   // sets the servo position according to the scaled value 
  
  }
  if (dist>1){
    pos = 90;
    myservo.write(pos);
  }
  if (dist>2){
    pos = 180;
    myservo.write(pos);
  } 
  
  Serial.println(dist);
  delay(200);                           
}

Any advice, help or guidance would be really appreciate it :slight_smile:
Thanks for reading

Pleas go back and modify the post, select the code and hit the # icon, then it is formatted right.
This bit:-

if (dist>1){
    pos = 90;
    myservo.write(pos);
  }
  if (dist>2){
    pos = 180;
    myservo.write(pos);
  }

Will write to the servo 90 and then 180 if the dist is greater than 2. Use compound if statements like this:-

if(dist>1 && dist<2) myservo.write(90);

Is this possible to do with this type of sensor?

Sure. Read the distance more than once, and the difference in values divided by the difference in times will be the speed of approach. The stuff you are doing to map the reading to a 0 to 5 range has to go, though.

  if (dist <0){

How would this be possible? The lowest value returned by analogRead is 0. Multiplying a non-negative variable value by a positive number can not possibly result in a negative value.

  if (dist>1){
    pos = 90;
    myservo.write(pos);
  }
  if (dist>2){
    pos = 180;
    myservo.write(pos);
  }

So, if dist is 2, position the servo to 90 then position it to 180. Doesn’t really seem necessary.
Swap the order of operations, and use else:

if(dist > 2)
  // do something
else if(dist > 1)
  // do something else

To include the speed test, you’ll need to take a reading, wait a bit, and take another reading, as ints. Divide the difference in readings by how long you waited, to get speed.

Moving the servo to one of three defined positions is not moving smoothly, though, so you need to re-think that bit.

Hello,

Thanks for the replies… As I said I am very new at this… somehow I manage to calibrate the sensor again and change it so it will go from 0 to 180 in steps of 5, and I applied that to the servo so now it works a bit with speed cause it goes faster or slower according to how you approach…
-Unfortunately it keeps jumping back and forward a bit. Can anyone help me to make it more accurate it?? like the knob example but with the distance sensor.

  • Sometimes when it reach 0 or 180 it makes strange noises…Can anyone explain me why?.

  • I want to also be capable to say that when I approach too fast the servo will go to zero. I will try further the speed formula that PaulS advice me… Right now it made it jump even more but i think I am using it wrong…

Well this is the code:

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int sensepin = 2;  // analog pin used to connect the sharpsensor
int val;    // variable to read the value from the analog pin 

int valfinal;
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
Serial.begin (9600);


} 
 
void loop() 
{ 
  val = analogRead(sensepin);
  val= map(val, 200, 500, 0, 36);// mapping from 0 to36
  val = min(val,36);// to mainting the values between 0 and 36
  val = max(val,0);
  valfinal = val * 5;// to make the values from 0 to 180

  myservo.write(valfinal);
  Serial.println(valfinal);
  delay(100);        

}

Any advice,comment or observations will be welcome :slight_smile: