Pages: [1] 2   Go Down
Author Topic: Sonar sensor to arduino to servo  (Read 802 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
self study!!.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
self study!!.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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..
« Last Edit: March 15, 2012, 03:04:10 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
distance = a;
Did you mean:
Code:
a = distance;
?
« Last Edit: March 15, 2012, 03:12:11 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
self study!!.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but it does not stores the data from the sensor.
See my last comment.

Stop calling me "sir".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simplified, but uncompiled and untested.
Code:
#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;
}
« Last Edit: March 15, 2012, 03:29:34 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
self study!!.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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-grin


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



« Last Edit: March 15, 2012, 03:48:34 am by jenfer » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
self study!!.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.. smiley-sad
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
self study!!.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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.. smiley)

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.
« Last Edit: March 15, 2012, 05:22:00 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24326
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Quote
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".
« Last Edit: March 15, 2012, 05:37:39 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 19
self study!!.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.. smiley)
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What are you getting printed from this?
Code:
    Serial.println(ranges [i]);

Logged

Pages: [1] 2   Go Up
Jump to: