View Full Version : Sharp GP2D02 and servos

01-19-2007, 03:09 AM
I was looking at the posted code (at the bottem) and I was wondering how I would go out interfacing the distance mesured with the motion of a servo? Thanks for the help!

' {$STAMP BS2}
' variable declarations
val05· VAR· Byte· ' value where reading is stored
i· VAR· Byte· ' count variable for loop

' constant declarations
cl· CON· 14· ' pin 14 is output (clock)
dt· CON· 15· ' pin 15 is the input (data)

' I/O pin setup for detector
INPUT dt··· ' make pin 15 the input
HIGH cl····· ' make pin 14 output and high
DEBUG "Start", CR· ' indicate beginning
FOR i = 1 TO 40
· GOSUB read05··· ' take reading
· DEBUG DEC val05, CR· ' display result
· PAUSE 100··· ' stall so display readable
DEBUG "Finish", CR· ' indicate end
END····· ' stop Stamp when done

' subroutine read05
' This subroutine takes a reading from the connected
' GP2D05 detector and stores the value in "val05".
' Any two pins can be used.· Set "dt" to the data line
' of the GP2D05 (pin 3 on JST connector, yellow wire).
' Set cl to the clock line of the GP2D05 (pin 1 on the
' JST connector, green wire).· Also, the IN15 needs
' to be set to the correct variable for the data pin
' (data line).
· LOW cl··· ' turn on detector for reading
· PAUSE 56··· ' wait 56ms for detector to read
· val05 = IN15··· ' obtain reading from detector
· HIGH cl··· ' turn off detector
· PAUSE 2··· ' wait for reset

Tom Walker
01-19-2007, 04:00 AM
I am not sure that I understand what it is that you are asking..

Truly Understand the Fundamentals and the Path will be so much easier...

01-19-2007, 04:33 AM
I would like the make the servo move back or forth in regaurd to the distance of my hand to the IR sensor

01-19-2007, 07:16 AM
You want the servo to follow your hand? you will need more than one ir sensor and have the servo turn toward the closest reading.

- Stephen

01-19-2007, 08:39 AM
Can the Ir take a reading say of 20 and make the servo go to 20 degrees and then if the IR sensor gets a reading of 200 can the servo go to another location? A proportional interface between the distance the IR reads and the rotation of the servo.

01-19-2007, 08:46 AM
You could write the code to do that but the IR won't do that on it's own.

- Stephen

01-19-2007, 08:52 AM
Thats true, what would the code look like, I am new to this and trying to learn about these things like interfacing and stuff

01-19-2007, 10:36 AM
well try this
take the val05 ... that is the value you will get from the sharp sensor.. then take some servo code
like this
remember to set x as a variable

I think the servo value can be a variable..??
remember the GP2D02 does not give you a cm or inch value.

INPUT dt ' make pin 15 the input
HIGH cl ' make pin 14 output and high
DEBUG "Start", CR ' indicate beginning
FOR i = 1 TO 40
GOSUB read05 ' take reading
DEBUG DEC val05, CR ' display result
PAUSE 100 ' stall so display readable
FOR i= 1 TO 24
PULSOUT 13, val05 'servo plugged into pin 13
DEBUG "Finish", CR ' indicate end
END ' stop Stamp when done

01-21-2007, 10:23 AM
Where do I place this code for the servo to react to the sensor?

01-22-2007, 02:09 AM
I'm still not clear on what you want to happen. As you move your hand closer or farther away from the sensor you want a servo to move left or right?

- Stephen

01-22-2007, 02:34 AM
I would like the servo to move right as you move your hand closer and left as you move your hand farther away. Thanks soo much for the help this is tough

01-22-2007, 03:00 AM
OK, get a reading from the sensor, use that reading to determine a value to send to the servo (pulse between 1 and 2 ms with a pause of 20 ms between pulses) and send it. loop back to start (get another reading) Right and left and closer and further will become apparant as you test your results.

- Stephen

01-22-2007, 03:48 AM
would I modify the code that is pasted at the top of this thread or make a new one, what would the basics of it look like?

01-22-2007, 12:07 PM
Also the reading on the debug screen is always 255?

01-25-2007, 01:10 PM
What is the constent 255 reading from is because I am using a wrong type of diode, also how can I tie the debug values into servo movements? Thanks soo much I am really in need of help.

01-25-2007, 10:49 PM
hey all
·if you have a boe bot with the regular ir u should be able to modify the" following boebot" code, but first things first you gonna have to get your ir sensor working ,,try this

·· then u could look at modifiying this code

hope this helps

Problems are the "roads" of life,
solutions are only "onramps" to the next problem

············································· "Brad Smith"

01-26-2007, 04:22 AM
thanks soo much Warrlok, we are getting somewhere now. I was wondering with that first code why I would be getting a reading of a constent number of 510, then when I do get the numbers fluctauting to distance how would I interface the servo with the bs2's pins? Thanks again!