Problem with Ping Sonar

Discussion specific to the 24-pin ZX microcontrollers, e.g. ZX-24r, ZX-24s and ZX-24t.
Post Reply
LtDan80
Posts: 7
Joined: 11 February 2008, 10:58 AM

Problem with Ping Sonar

Post by LtDan80 »

Here is my code,

Option Explicit

Public Sub Main()

Dim Width as Single, Feet as Single, Inches as Single

Do
Call PulseOut(18, 3.0, 1)
Call PulseIn(18, 1, Width)
Feet = Width * 550.0 'assuming 1100 feet per sec
Inches = Feet * 12.0
Debug.Print CStr(Inches)
Call Delay(0.1)
Loop

End Sub

This code works perfectly fine with a basicx chip, I just swapped chips between the basic and the zx and it will not work on the zx. The sonar normally has a green LED that will flash with the pulse, however with the z, it is not receiving the pulse. I am using actual pin 13 since the Ping Sonar is designed for the basic stamp. Any help will be appreciated.

Dan
GTBecker
Posts: 616
Joined: 17 January 2006, 19:59 PM
Location: Cape Coral

Re: Problem with Ping Sonar

Post by GTBecker »

LtDan80 wrote:

Code: Select all

	Call PulseOut(18, 3.0, 1)
	Call PulseIn(18, 1, Width)
I am using actual pin 13...
A three second output pulse is out of range, and the pin number should be 13.
Tom
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

As Tom suggests your output pulse is 3 seconds which at the speed of sound (1100 ft/sec, 344m/s) is 3300 feet. Not surprisingly the return pulse is not received and has long since come and gone. You need to try a much shorter time for the output pulse.

Alternatively you could simply try the SonarExample.bas that is a BasicX example. This code compiles just fine after you update the code to use Debug.Print (or port SerialPort.bas). You will need to convert the output from mm to inches if that is your chosen measurement system.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Problem with Ping Sonar

Post by dkinzer »

GTBecker wrote:A three second output pulse is out of range.
The BasicX documentation states that the results are undefined if the pulse width is out of range. The ZBasic documentation does not specifically address that issue but clearly, any outcome is BasicX-compatible.

If you need a pulse longer than the maximum pulse width you can use the following outline:

Code: Select all

Call PutPin(pin, 1)
Call Delay(width)
Call PutPin(pin, 0)
Note, also, that for PulseOut() to work correctly, you must first set the output level to the "inactive" state. This should be done before the Do loop and again after PulseIn().
- Don Kinzer
LtDan80
Posts: 7
Joined: 11 February 2008, 10:58 AM

Post by LtDan80 »

Thank you, I actually ended up finding the problem. It was not so much the code, as the school computer I have been working on, It actually took off the setting for the zx-24a. Once i went back into that and identified the target chip it worked. I just learned that I need to do that every time I have a new project. Thank you for the suggestions though.

Dan
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

LtDan80 wrote:I just learned that I need to do that every time I have a new project.
If you wish, you can add the target device setting to either the project file or the main module.
- Don Kinzer
Post Reply