Go Down

Topic: Sonar sensor to arduino to servo (Read 1 time) previous topic - next topic

jenfer

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.
http://www.youtube.com/watch?v=_r1knQbsKbU

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.

AWOL

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

jenfer

#2
Mar 15, 2012, 09:02 am Last Edit: Mar 15, 2012, 09:04 am by AWOL Reason: 1
Code: [Select]
#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..

AWOL

#3
Mar 15, 2012, 09:06 am Last Edit: Mar 15, 2012, 09:12 am by AWOL Reason: 1
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: [Select]
distance = a;
Did you mean:
Code: [Select]
a = distance; ?
"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.

jenfer

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.

AWOL

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

Stop calling me "sir".
"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.

AWOL

#6
Mar 15, 2012, 09:26 am Last Edit: Mar 15, 2012, 09:29 am by AWOL Reason: 1
Simplified, but uncompiled and untested.
Code: [Select]
#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;
}
"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.

jenfer

#7
Mar 15, 2012, 09:39 am Last Edit: Mar 15, 2012, 09:48 am by jenfer Reason: 1
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 :D


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




AWOL

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

jenfer

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.. :(

AWOL

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

jenfer

#11
Mar 15, 2012, 11:15 am Last Edit: Mar 15, 2012, 11:22 am by AWOL Reason: 1
Code: [Select]
#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.

AWOL

#12
Mar 15, 2012, 11:25 am Last Edit: Mar 15, 2012, 11:37 am by AWOL Reason: 1
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".
"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.

jenfer

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

dxw00d

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


Go Up