So you've made your first robot with an infra red sensor and decide to upgrade it to ultra sonic. after a trip to Radioshack you look at the sensor you bought and realize it only has 3 pins instead of 4. That's because the signal pin on your Parallax Ping))) serves as both an input and output. How economical! The only problem is, as both input and output, where the hell do you hook it up, and how?
The pixaxe 28x1 and most other picaxe chips have some pins that can serve as both digital input and output at the same time. In my case I use input pin 0. (pin 11)
Hook up your Ping)))'s signal to the pin shown above. Just like how it is shown in the image below...
Now that your signal is hooked up to "in 0", You can make this pin both input or output at any time. Since by default, in 0 is an input pin you must call either:
HIGH PORTC 0 or LOW PORTC 0 to make in 0 an output pin.
Why do we care about input/output? It's because the sensor needs your permission to send out an ultrasonic pulse, like a bat. You give it permission by having the board send your sonar a short signal on pin 'in 0'. but you cant "send" stuff through this pin if it currently an input pin. so first we make it an output pin and send the signal like so...
LOW PORTC 0
HIGH PORTC 0
PAUSE 1
LOW PORTC 0
notice the signal stays on HIGH for 1 millisecond, according to Ping)) documentation, this is enough time for the sonar to confirm that it needs to send out its ultrasonic pulse. Normally there is a command called PULSOUT that does the low-high-low for us, but sadly this cannot work on 28x1's pins 11 through 18. Which is why getting the Parallax Ping))) to work is a pain since it has only 1 pin for both input and output.
Cool so the pulse is send and is out there in the world bouncing off nearby objects...
now we need to convert pin in 0 back to input to receive the ultrasonic waves we sent out. Do this by calling...
LET DIRSC = %00000000
This resets all input pins back to... input pins! Now all we need to do is determine the time it takes before our waves make their round trip back to the sonar. The time it takes these waves come back determines the distance of the object ahead.
PULSIN 0, 1, W0 '(make sure the second parameter is 1!)
when this command has been executed we now have our distance stored in W0!
Troubleshooting:
If you get bad readings heres a couple of thigs to check:
if your range seems too short (in my case W0 never had a value over 84), it means that your sonar is somehow detecting residual waves from the pulse it just sent before the waves make their round trip... or residual waves from the previous pulse it sent out form the last iteration. Try placing a cloth over the sonar or a thin layer of packaging foam like this...
------>
then see if there is improvement.
if you get no readings..
1. check your pin connections
2. make sure you high state stays on long enough (1 millisecond) for the sonar to confirm it needs to send out a wave pulse.
3. see if you pulsins are happening one after another too quickly, try putting some kind of delay after each pulsin.
4. see if your sonar is getting 5v of power, use a multimeter. It could be underpowered.. (mine is getting about 5.4V)
5. Are your batteries are dead?
6. Try a Low-high only instead of a low-high-low...
LOW PORTC 0
HIGH PORTC 0
PAUSE 1
LOW PORTC 0
LET DIRSC = %00000000
PULSIN 0, 1, W0