'Judd Communications Depth Sensor with
'Digital Output option, programming example
'Wiring
'Red      +12V
'Black     G
'Clear     G
'Green     C1
'White     C4
'Brown     Not used
'-----------------------------------------------

'\\\\\\\\\\\\\\\\\\\\\\\\\ DECLARATIONS /////////////////////////

Public Battery
Public DSTempF
Public DSTime
Public DSDistance
Public DSOffset
Public DSDepth
Public DSRetries
Public DSOutString As String
Public DSOutVal(4)



'\\\\\\\\\\\\\\\\\\\\\\\\ OUTPUT SECTION ////////////////////////

DataTable(Table101,true,-1)
  OpenInterval
  DataInterval(0,1,Min,10)
  Sample(1, Battery, FP2)
  Sample(1, DSTempF, FP2)
  Sample(1, DSTime, FP2)
  Sample(1, DSDepth, FP2)
  Sample(1, DSRetries, FP2)
EndTable

'\\\\\\\\\\\\\\\\\\\\\\\\\ SUBROUTINES //////////////////////////

'\\\\\\\\\\\\\\\\\\\\\\\\\\\ PROGRAM ////////////////////////////

BeginProg

  Scan(5,Sec, 3, 0)
    SequentialMode
    Battery(Battery)
    'Turn Depth Sensor ON using Control port 1
    PortSet(C1, 1)
    'Wait 3 seconds for ASCII sentence from sensor
    'Data is input to Control port 4. Control port 3
    'is used by the SerialIn instruction as a TX line.
    'Nothing is connected to Control port 3
    SerialOpen (ComC3,1200,0,0,10000)
    SerialIn (DSOutString,ComC3,300,13,100)
    SerialClose (ComC3)
    'Turn Depth Sensor OFF
    PortSet(C1, 0)
    SplitStr (DSOutVal(),DSOutString,CHR(9),4,0)

    'Convert Air Temp from Kelvin x 2, to Farenheit
    'From K to C
    DSTempF = DSOutVal(1)
    DSTempF = DSTempF * 0.5
    DSTempF = DSTempF + -273
    'From C to F
    DSTempF = DSTempF * 1.8
    DSTempF = DSTempF + 32

    'Get DSTime
    DSTime = DSOutVal(2)

    'Convert Distance measured from millimeters to inches
    DSDistance = DSOutVal(3)
    DSDistance = DSDistance * 0.0394
    'Derive Depth by Subtracting Distance from Sensor Height
    'Sensor Height can be set by on the fly using Loggernet to
    'assign a value to DSOffset. This allows for offset tweaks
    'without reloading the program. 
    'Or replace DSOffset on the following line with the sensor height.
    'DSDepth = 120 - DSDistance    
    DSDepth = DSOffset - DSDistance
    
    'Get number of Retries
    DSRetries = DSOutVal(4)
    '------------------------------------------------
    'Log sensor output once an hour
    CallTable Table101
  NextScan
EndProg
