Examples | Shop
 
BX-24 with Infra-Red length measurement
 
This application demonstrates use of the Basic-X 24 microcontroller in conjunction with the GP2D12 IR sensor. “If else loops” are used to give a distance in front of the GP2D12 that does not fit a linear response; the “if” and “if else loops” give a avreage function for voltage to distance conversation.
 
Connections:
BX-24
GP2D12
Pin 23 - GND
Pin 21 - VCC
Pin 14 - Vo (Signal)
   
  External power supply
Pin 24 - +5 V
Pin 4 - GND

 

'**************************************************************************
'**************************************************************************
'BX-24 with Infra-Red length measurement
'Description: This application uses if else loops to give a distance in front
'of a GP2D12 Infra-red device that does not fit a linear response, the if and
'if else loops give an average function for voltage to distance conversion.
'*GP2D12 needs a separate power supply
'Created: 14/04/05 Revision: 1.0
'Written by: TR Control Solutions (Simon & Tobias)
'
'**************************************************************************
'**************************************************************************
Dim Voltage As integer 'set variable names
Dim VoltageA As single
Dim Distance As single

Const ADC As Byte = 14 'set constants and addresses
Const GreenLED As Byte = 26
Const RedLED As Byte = 25
Const LEDon As Byte = 0
Const LEDoff As Byte = 1

Public Sub Main() 'start of program

Voltage = 0 'initialise variables
VoltageA = 0.0
Distance = 0.0

Do 'do loop allows continual running and update of distance

Voltage = getADC(ADC) 'read in voltage of sensor output

Call delay(0.5)

If voltage < 100 then ' if the distance or too far, saves processing time
Call PutPin(GreenLED, LEDoff)
Call Delay(0.04)
Call PutPin(RedLED, LEDon)

Else
Call PutPin(RedLED, LEDoff)
Call Delay(0.07)
Call PutPin(GreenLED, LEDon)
Call DistanceConverter () 'calculates the distance

If Distance < 70.0 then 'this is to do with unreliable response of sensor at short and long distances
If Distance > 11.5 then
Debug.Print Cstr(Distance) 'output for the distance to the debug screen on pc
End If
Else
Call PutPin(GreenLED, LEDoff)
Call Delay(0.04)
Call PutPin(RedLED, LEDon)
End If
End If

Loop 'end of do loop making program non-terminal
End Sub
'***************************************************************************

Sub DistanceConverter()

VoltageA = Csng(Voltage) 'converts integer to single for calculation purpose
VoltageA = VoltageA / 204.6 'converting adc result in bits to a real voltage

If VoltageA < 2.45 then

Distance = 10.0

If VoltageA > 1.4 then 'following statements return a piece wise result for distance

Distance = VoltageA - 3.5
Distance = Distance / (-0.105)

ElseIf VoltageA > 1.0 then
Distance = VoltageA - 2.2
Distance = Distance / (-0.04)

ElseIf VoltageA > 0.6 then
Distance = VoltageA - 1.6
Distance = Distance / (-0.02)

ElseIf voltageA > 0.45 then
Distance = VoltageA - 0.85
Distance = Distance / (-0.005)
End If

Else
Call PutPin(GreenLED, LEDoff)
Call Delay(0.04)
Call PutPin(RedLED, LEDon)
End If
End Sub 'returns to main.
'**************************************************************************
'**************************************************************************


Examples | Shop