Visual Basic 6 and Sensors

 

This is a short tutorial on a number of "methods" of polling phidget analog and digital sensors.

Let's look at a scenario where a 888IFKit has a temperature sensor attached to analog input 0, a IR distance sensor to analog input 1, and a push button switch to digital input 0. It is assumed the reference to the phidget library has been done.

Setting up the Phidget (Dim, Set, Call)

The following lines enable the 888. We will call the instance of the InterfaceKit, kit

General Declarations

Dim WithEvents kit As PhidgetInterfaceKit

Form...Load

Private Sub Form_Load()

Set kit = New PhidgetInterfaceKit
Call kit.Open

End Sub

Method 1 - Using a timer

In this method a timer is enabled and the interval set to poll the sensors every 100 milliseconds (10 times a second). A subroutine named scansensor is created to contain the code. We will assume text boxs have been created to display either the value or state of the sensor.

Public Sub scansensor()

Temp = (kit.SensorValue(0) * 0.2222) - 61.111
Text1 = Temp

Distance = 4800 / (kit.SensorValue(1) - 20)
Text 2 = Distance

If kit.InputState(0) = True Then
Text3 = "Switch Closed"
Else
Text3 = "Switch Open"
End If

End Sub

The timer would then be coded with the following:

Private Sub Timer1_Timer()

scansensor

End Sub

Method 2 - Using the Phidget "events"

When you include the WithEvents key word in the DIM statement, Visual Basic uses the phidget library to add the events associated with the type of Phidget that is declared in General Declarations.

If you look at the small panes at the top of the code window, on the left you should find kit and on the right OnSensorChange (if you don't see kit you probably left out the WithEvents keyword).

An associated sub procedure should be displayed :

Private Sub kit_OnSensorChange(ByVal Index As Long, ByVal SensorValue As Long)

End Sub

There are 2 important key words in this sub procedure. Index refers to the 8 analog inputs 0 to 7. SensorValue is the numerical value of the sensor signal between 0 and 1000. Here is comparable code which does the same thing as method 1 but without a timer.

Code for this subprocedure would be as follows:

Private Sub kit_OnSensorChange(ByVal Index As Long, ByVal SensorValue As Long)

If Index = 0 Then
  Temp = (kit.SensorValue * 0.2222) - 61.111
  Text1 = Temp
End If

If Index = 1 Then
  Distance = 4800 / (kit.SensorValue - 20)
  Text 2 = Distance
End If

End Sub

 Note: The kit.SensorValue does not have a bracketed number as in the scansensor subprocedure.

There is a second way to code this subprocedure using the Select Case construct. Look at the following code:

Private Sub kit_OnSensorChange(ByVal Index As Long, ByVal SensorValue As Long)

Select Case Index

Case 0
Temp = (kit.SensorValue * 0.2222) - 61.111
Text1 = Temp

Case 1
Distance = 4800 / (kit.SensorValue - 20)
Text 2 = Distance

End Select


End Sub

Note again the word Index and how it is used here. This method yields neat code that is easily read.

Now for the digital Input. The event associated with digital inputs is called OnInputChange. Again, if you look in the small panes at the top of the code window you will find:

The associated subprocedure is shown next:

Private Sub kit_OnInputChange(ByVal Index As Long, ByVal NewState As Boolean)

End Sub

Again, Index refers to the 8 digital Input ports 0-7. The variable NewState refers to whether the port is ON or OFF and is a Boolean variable which can have 2 states, On or Off, True or False.

Here is sample code using both constructs, IF...Then and Select Case. 

Private Sub kit_OnInputChange(ByVal Index As Long, ByVal NewState As Boolean)

If Index = 0 Then

  If NewState = True Then
    Text3 = "Switch Closed"
  Else
    Text3 = "Switch Open"
  End If
End If

End Sub

And using the Select Case construct:

Private Sub kit_OnInputChange(ByVal Index As Long, ByVal NewState As Boolean)

Select Case Index

Case 0

  If NewState = True Then
    Text3 = "Switch Closed"
  Else
    Text3 = "Switch Open"
  End If

End Select


End Sub