Problem with code,,,, Please help....

Started by ribron32 June 18, 2005
Below is some sample code I am trying to use. It takes a ADC value
from a shapr GP2D12 IR and converts it to ,"CM". What I'd like to do
is to implement a conditinal like ... If ( Range < 150 ) then
BlinkGreenLED
But I keep getting setax errors. I know it's something I'm doing
wrong, but I just don't know what.... Thank in advance _Duane

Option Explicit

Public Sub Main()
Dim value as single

Dim Voltage As byte Dim Range As Single
Dim Success As Boolean

Debug.Print
Debug.Print "Sharp IR object detector"
Debug.Print

Do
Call GetRange(Range, Success)

Debug.Print "Range = ";

If (Success) Then

Debug.Print CStr( CInt(Range) ); " cm"
Value = Range
If ( Range <= 150 ) then
call BlinkGreenLED Else
' Out of range.
Debug.Print " ***"
End If

Delay 0.3
Loop

End Sub
Public Sub GetRange( _
ByRef Range As Single, _
ByRef Success As Boolean)

Dim Voltage As Single
Const MinVolt As Single = 0.4
Const MaxVolt As Single = 2.8

Const InputPin As Byte = 16

Call GetADC(InputPin, Voltage)

' Convert to voltage.
Voltage = Voltage * 5.0

Range = VoltageToRange(Voltage)

' Check for legal voltage.
If (Voltage >= MinVolt) And (Voltage <= MaxVolt) Then
Success = True
Else
Success = False
End If

End Sub Public Function VoltageToRange( _
ByVal V As Single) As Single

' Returns distance in units of cm.

Const A As Single = 0.0082712905
Const B As Single = 939.57652
Const C As Single = -3.3978697
Const D As Single = 17.339222

' Curve fit.
VoltageToRange = (A + B * V) / (1! + C * V + D * V * V)

End Function
' Led Flash routine. ' Private Sub BlinkRedLED()

Const RedLED As Byte = 25
Const LEDon As Byte = 0
Const LEDoff As Byte = 1

' Red pulse.
Call PutPin(RedLED, LEDon)
Call Delay(0.10)
Call PutPin(RedLED, LEDoff)
Call Delay(0.10)

End Sub Private Sub BlinkGreenLED()

Const GreenLED As Byte = 26
Const LEDon As Byte = 0
Const LEDoff As Byte = 1
' Green pulse.
Call PutPin(GreenLED, LEDon)
Call Delay(0.10)
Call PutPin(GreenLED, LEDoff)
Call Delay(0.10)
End Sub


--- In basicx@basi..., "ribron32" <ribron32@y...> wrote:
> But I keep getting setax errors. I know it's something I'm doing
> wrong, but I just don't know what....

There are two errors in your code that I can see. The line

If ( Range <= 150 ) then

is incorrect because Range is of type Single and the constant to
which you are comparing it is integral. You can fix it thusly:

If ( Range <= 150.0 ) then

The second error is that the If statement that begins around line 21
has no corresponding End If. I suspect that it should be placed
just before the Delay call:

End If
Delay 0.3
Loop



Hi, When Ifirst bought my Chip and Robodyssey board, I once aswell
wanted to do the exact same thing that you did, so the lights light
up to tell you if the object is to far/to cloae/ or just right
without having to refer to the Computer BX Program to get that
reading...
'I call this Sensor to far/close LED lite up ;-)
' this should tell you via: BX-24 RED/GREEN/RG lights that your
Sensor has an object, to close, to far, or just right Option Explicit
Dim value as Integer
Public Sub Main()
Do
Call SensorIn
Loop
End Sub Sub SensorIN()
Do

value=GetADC(13) ' gets value from sensor
if value > 450 Then
Call Toclose
Sleep(0.3)
End if
If value <150 Then
Call Tofar
Sleep(0.3)
end if

sleep (0.3)
Debug.print "Just Right"
Call Putpin (25,0) 'red light on
Call Putpin (26,0)'green light on
Delay (0.5)
Call Putpin (25,1) 'red light off
Call Putpin (26,1) 'green light on
Loop
End Sub
Sub Tofar()
Do
Debug.print "To Far"

Call Putpin (25,0) 'red light on
Delay (0.5)
Call Putpin(25,1) ' red light off

value= GetADC(13) ' gets value from sensor
if value > 150 Then
Call SensorIn
Sleep(0.3)
End If

Sleep (0.3)
Loop
End Sub Sub Toclose()
Do
debug.print "To Close"
Call Putpin (26,0) 'green light on
Delay (0.3)
Call Putpin (26,1) 'green light off
value = GetADC(13) ' gets value from sensor
if value <450 Then
Call SensorIn
End If
Sleep(0.3)
Loop
End Sub

another cool add on I did to this later was adding the CStr(getADC
(13)) after the debug.print

like this

debug.print "To Close - "; CStr(GETADC(13))
-knitsu --- In basicx@basi..., "ribron32" <ribron32@y...> wrote:
> Below is some sample code I am trying to use. It takes a ADC value
> from a shapr GP2D12 IR and converts it to ,"CM". What I'd like to
do
> is to implement a conditinal like ... If ( Range < 150 ) then
> BlinkGreenLED
> But I keep getting setax errors. I know it's something I'm doing
> wrong, but I just don't know what.... Thank in advance _Duane
>
> Option Explicit
>
> Public Sub Main()
> Dim value as single
>
> Dim Voltage As byte > Dim Range As Single
> Dim Success As Boolean
>
> Debug.Print
> Debug.Print "Sharp IR object detector"
> Debug.Print
>
> Do
> Call GetRange(Range, Success)
>
> Debug.Print "Range = ";
>
> If (Success) Then
>
> Debug.Print CStr( CInt(Range) ); " cm"
> Value = Range
> If ( Range <= 150 ) then
> call BlinkGreenLED > Else
> ' Out of range.
> Debug.Print " ***"
> End If
>
> Delay 0.3
> Loop
>
> End Sub >
> Public Sub GetRange( _
> ByRef Range As Single, _
> ByRef Success As Boolean)
>
> Dim Voltage As Single
> Const MinVolt As Single = 0.4
> Const MaxVolt As Single = 2.8
>
> Const InputPin As Byte = 16
>
> Call GetADC(InputPin, Voltage)
>
> ' Convert to voltage.
> Voltage = Voltage * 5.0
>
> Range = VoltageToRange(Voltage)
>
> ' Check for legal voltage.
> If (Voltage >= MinVolt) And (Voltage <= MaxVolt) Then
> Success = True
> Else
> Success = False
> End If
>
> End Sub > Public Function VoltageToRange( _
> ByVal V As Single) As Single
>
> ' Returns distance in units of cm.
>
> Const A As Single = 0.0082712905
> Const B As Single = 939.57652
> Const C As Single = -3.3978697
> Const D As Single = 17.339222
>
> ' Curve fit.
> VoltageToRange = (A + B * V) / (1! + C * V + D * V * V)
>
> End Function >
> ' Led Flash routine. ' > Private Sub BlinkRedLED()
>
> Const RedLED As Byte = 25
> Const LEDon As Byte = 0
> Const LEDoff As Byte = 1
>
> ' Red pulse.
> Call PutPin(RedLED, LEDon)
> Call Delay(0.10)
> Call PutPin(RedLED, LEDoff)
> Call Delay(0.10)
>
> End Sub > Private Sub BlinkGreenLED()
>
> Const GreenLED As Byte = 26
> Const LEDon As Byte = 0
> Const LEDoff As Byte = 1
> ' Green pulse.
> Call PutPin(GreenLED, LEDon)
> Call Delay(0.10)
> Call PutPin(GreenLED, LEDoff)
> Call Delay(0.10)
> End Sub