Sonar sensor to arduino to servo

Hello there guys!..
i have here a sonar sensor
http://www.e-gizmo.com/KIT/sonar.htm
and i want to control the servo movement based on the input from the sensor.

here is a link i want to imitate.

can you guys help me. Please give some suggestions on how i can possibly do this.

i came up with an idea of setting the servo into different degrees(0,45,90,135,and 180) and storing the ranges sensed by the sensor into variables. but, i have a hard time experimenting just to achieve my desired output. please please help me.

d storing the ranges sensed by the sensor into variables.

an array would be much simpler.
Post your code and we'll see what we can do.

#include <Servo.h>
const int trigger=3;
const int echo=2;
float distance;
float a,b,c,d,e;
Servo xServo;

void setup()
{
  Serial.begin(9600);
  pinMode(trigger,OUTPUT);
  pinMode(echo,INPUT);
  xServo.attach(10);
  xServo.write(0);
}

void loop()
{

  digitalWrite(trigger,LOW);
  delayMicroseconds(5);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger,LOW);
 
  distance=pulseIn(echo,HIGH);
  distance=distance*0.0001657;
 
  Serial.println(distance);
  delay(50);
 
  xServo.write(0);
  delay(500);
  checkangle();
  xServo.write(45);
  delay(500);
  checkangle();
  xServo.write(90);
  delay(500);
  checkangle();
  xServo.write(135);
  delay(500);
  checkangle();
  xServo.write(180);
  delay(500);
  checkangle();
}
 
void checkangle()
{
  if (xServo.read() == 0)
  {
    distance = a;
    delay(10);
    Serial.println(a);
    delay(50);
  }
  else if (xServo.read()== 45)
  {
    distance = b;
    delay(10);
    Serial.println(b);
    delay(50);
  }
  else if (xServo.read() == 90)
  {
    distance = c;
    delay(10);
  }
  else if (xServo.read() == 135)
  {
    distance = d;
    delay(10);
  }
  else if (xServo.read() == 180)
  {
    distance = e;
    delay(10);
  }
}

Moderator edit: And now with added CODE TAGS

here is my code in storing the values into variables. but its not working. I just programmed it according to what i know.
Please help me..

but its not working.

That's really, really not helpful.
Describe to us what "working" means to you, and how you code's behaviour differs from that definition.

distance = a;

Did you mean:
a = distance; ?

Ow, i'm sorry for that work sir, it's working but it does not stores the data from the sensor. What might be the problem?.
I'm just starting to loose hope here.

but it does not stores the data from the sensor.

See my last comment.

Stop calling me "sir".

Simplified, but uncompiled and untested.

#include <Servo.h>
const int trigger=3;
const int echo=2;
const int nAngles = 5;

Servo xServo;
const int angles [nAngles] = {0, 45, 90, 135, 180};
float ranges [nAngles];

void setup()
{
  Serial.begin(9600);
  pinMode(trigger,OUTPUT);
  pinMode(echo,INPUT);
  xServo.attach(10);
}

void loop()
{
  for (int i = 0; i < nAngles; ++i) {
    xservo.Write (angles [i]);
    delay (500);
    ranges [i] = getRange ();  
    delay(50);
  }

  for (int i = 0; i < nAngles; ++i) {
    Serial.println(ranges [i]);
  }
}

float getRange ()
{
  digitalWrite(trigger,LOW);
  delayMicroseconds(5);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger,LOW);
 
  return pulseIn(echo,HIGH) * 0.0001657;
}

a = distance;

yep sir!.. i'm sorry for that.. this was my first program.

Thank you sir for the code. wait i'm compiling it. but there's a problem that says ('index' was not declared in this scope)..

ow just found the error. It's working sir!.. Thank you.

now my problem is comparing the ranges, and after that the servo will face the nearest range.
just stay there sir. i have more questions :smiley:

ow sorry if i call you sir. i just want to give respect. :))

now my problem is comparing the ranges, and after that the servo will face the nearest range

That is much, much simpler, now that you have an array.

Sir how can i pick-up and compare the value of the ranges of the sensor in the for loop?
actually when array is introduced to me, i have a difficult time in understanding it.
got stuck up here.. :frowning:

Think of arrays as a row of houses.

You can refer to any house simply by specifying the house number (the number in the square brackets).

got stuck up here

I'm sorry, but from here, I can't see what you're stuck on, so you'll have to show me.

#include <Servo.h>
const int trigger=3;
const int echo=2;
const int nAngles = 5;

Servo xServo;
const int angles [nAngles] = {0, 45, 90, 135, 180};
float ranges [nAngles];

void setup()
{
  Serial.begin(9600);
  pinMode(trigger,OUTPUT);
  pinMode(echo,INPUT);
  xServo.attach(10);
}

void loop()
{
  for (int i = 0; i < nAngles; ++i) {
    xServo.write (angles [i]);
    delay (500);
    ranges [i] = getRange ();  
    delay(50);
  }

  for (int i = 0; i < nAngles; ++i) {
    Serial.println(ranges [i]);
  }
  
  if((ranges[0]<ranges[1]) || (ranges[0]<ranges[2]) || (ranges[0]<ranges[3]) || (ranges[0]<ranges[4]))
  {
    xServo.write(0);
    delay(2000);
  }  
  else if((ranges[1]<ranges[0]) || (ranges[1]<ranges[2]) || (ranges[1]<ranges[3]) || (ranges[1]<ranges[4]))
  {
    xServo.write(45);
    delay(2000);
  }
  else if((ranges[2]<ranges[0]) || (ranges[2]<ranges[1]) || (ranges[2]<ranges[3]) || (ranges[2]<ranges[4]))
  {
    xServo.write(90);
    delay(2000);
  }
  else if((ranges[3]<ranges[0]) || (ranges[3]<ranges[1]) || (ranges[3]<ranges[2]) || (ranges[3]<ranges[4]))
  {
    xServo.write(135);
    delay(2000);
  }
  else if((ranges[4]<ranges[0]) || (ranges[4]<ranges[1]) || (ranges[4]<ranges[2]) || (ranges[4]<ranges[3]))
  {
    xServo.write(180);
    delay(2000);
  }
}

float getRange ()
{
  digitalWrite(trigger,LOW);
  delayMicroseconds(5);
  digitalWrite(trigger,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger,LOW);
 
  return pulseIn(echo,HIGH) * 0.0001657;
}

Here is my code sir. The servo is not facing the nearest range. Is my program correct?..
just edit it from the code you gave me sir.. :))

Moderator edit: Please Use CODE TAGS when posting code. Click on the # icon on the editor's toolbar, and paste your code between the tags which appear.

If I gave you a set of five numbered pages, each with a number written on it and asked you to tell which page had the largest number written on it (I don't necessarily need to know the value of the largest number, just which page it is on), how would you do it?

Is my program correct?.

I don't know, it looks a bit too big to me (I don't expect to see the numbers 45, 90, 135 or 180 more than once in that sketch) - what does the compiler say?
Does it do what you want it to do?

Please, please cut out the "sir".

If I gave you a set of five numbered pages, each with a number written on it and asked you to tell which page had the largest number written on it (I don't necessarily need to know the value of the largest number, just which page it is on), how would you do it?

~pointing the address?..

ow.. the program compiles and run but not the output i expect.
Ok.. I'll try to cut the word sir.. :))

What are you getting printed from this?

    Serial.println(ranges [i]);
#include <Servo.h>
 const int trigger=3;
 const int echo=2;
 const int nAngles = 5;

 Servo xServo;
 const int angles [nAngles] = {0, 45, 90, 135, 180};
 float ranges [nAngles];

 void setup()
 {
 Serial.begin(9600);
 pinMode(trigger,OUTPUT);
 pinMode(echo,INPUT);
 xServo.attach(10);
 }

 void loop()
 {
 for (int i = 0; i < nAngles; ++i) {
 xServo.write (angles [i]);
 delay (500);
 ranges [i] = getRange (); 
 delay(50);
 }
 
 for (int i = 0; i < nAngles; ++i) {
 Serial.println(ranges [i]);
 }

 //initial value
 int closestRange = ranges[0];
 int closestRangeIndex = 0;

 for (int i = 0; i < nAngles; i++) {
 if (closestRange > ranges[i]) {
 closestRange = ranges[i];
 closestRangeIndex = i;
 }
 }

 xServo.write (angles [closestRangeIndex ]);
 delay (500);
 }

 float getRange ()
 {
 digitalWrite(trigger,LOW);
 delayMicroseconds(5);
 digitalWrite(trigger,HIGH);
 delayMicroseconds(10);
 digitalWrite(trigger,LOW);

 return pulseIn(echo,HIGH) * 0.0001657;
 }

no error in compiling, but i don't know why. there's an error in uploading.

What are you getting printed from this?

~the ranges of the distance sensor..

jenfer:

What are you getting printed from this?

~the ranges of the distance sensor..

:roll_eyes: Yes, but do they look reasonable?

Yes, but do they look reasonable?

yes sir, for the purpose of checking.